Skip to content

Commit a9fc786

Browse files
committed
docs: add usage section in readme and docs
1 parent 7884e95 commit a9fc786

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,49 @@ add_executable(foo foo.cpp)
4646
target_link_libraries(foo PRIVATE errors::errors)
4747
```
4848

49+
## Usage
50+
51+
This package contains an `errors::Error` class, which represents an error object.
52+
Functions that may produce errors should return this object so that the error can be handled properly.
53+
54+
```cpp
55+
errors::Error read_file(const char* filepath);
56+
57+
int main() {
58+
const auto err = read_file(filepath);
59+
if (err) {
60+
// Handle the error.
61+
}
62+
63+
// Continue processing if no error.
64+
}
65+
```
66+
67+
For functions returning `errors::Error`, use `errors::nil` function to signify no error or return an error object created from the `errors::make` function.
68+
69+
```cpp
70+
errors::Error read_file(const char* filepath) {
71+
std::ifstream file(filepath);
72+
if (!file.is_open()) {
73+
return errors::make("failed to open file");
74+
}
75+
76+
// Process with no error.
77+
78+
return errors::nil();
79+
}
80+
```
81+
82+
Alternatively, an error object can also be created with a formatted message in the style of fmtlib using `errors::format` function.
83+
84+
```cpp
85+
if (!file.is_open()) {
86+
return errors::format("failed to open '{}'", filepath);
87+
}
88+
```
89+
90+
For more details and examples, refer to the [examples](./examples) directory.
91+
4992
## License
5093

5194
This project is licensed under the terms of the [MIT License](./LICENSE).

docs/index.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,53 @@ Alternatively, you can integrate this package using `CPM.cmake`_:
5454
.. _CMake: https://cmake.org/
5555
.. _CPM.cmake: https://github.com/cpm-cmake/CPM.cmake
5656

57+
58+
Usage
59+
-----
60+
61+
This package contains an `errors::Error` class, which represents an error object.
62+
Functions that may produce errors should return this object so that the error can be handled properly.
63+
64+
.. code-block:: cpp
65+
66+
errors::Error read_file(const char* filepath);
67+
68+
int main() {
69+
const auto err = read_file(filepath);
70+
if (err) {
71+
// Handle the error.
72+
}
73+
74+
// Continue processing if no error.
75+
}
76+
77+
For functions returning `errors::Error`, use `errors::nil` function to signify no error or return an error object created from the `errors::make` function.
78+
79+
.. code-block:: cpp
80+
81+
errors::Error read_file(const char* filepath) {
82+
std::ifstream file(filepath);
83+
if (!file.is_open()) {
84+
return errors::make("failed to open file");
85+
}
86+
87+
// Process with no error.
88+
89+
return errors::nil();
90+
}
91+
92+
Alternatively, an error object can also be created with a formatted message in the style of fmtlib using `errors::format` function.
93+
94+
.. code-block:: cpp
95+
96+
if (!file.is_open()) {
97+
return errors::format("failed to open '{}'", filepath);
98+
}
99+
100+
For more details and examples, refer to the `examples`_ directory.
101+
102+
.. _examples: https://github.com/threeal/errors-cpp/tree/main/examples
103+
57104
API Docs
58105
--------
59106

0 commit comments

Comments
 (0)