You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43Lines changed: 43 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,49 @@ add_executable(foo foo.cpp)
46
46
target_link_libraries(foo PRIVATE errors::errors)
47
47
```
48
48
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
+
49
92
## License
50
93
51
94
This project is licensed under the terms of the [MIT License](./LICENSE).
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.
0 commit comments