Skip to content

Commit 569c197

Browse files
authored
docs: improve guides in readme and documentation (#120)
* docs: update project description in readme and docs * docs: add key features section in readme and docs * docs: add integration section in readme and docs * docs: add usage section in readme and docs
1 parent a06b4d7 commit 569c197

File tree

2 files changed

+181
-2
lines changed

2 files changed

+181
-2
lines changed

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,90 @@
44
[![test status](https://img.shields.io/github/actions/workflow/status/threeal/errors-cpp/test.yaml?branch=main&&label=test)](https://github.com/threeal/errors-cpp/actions/workflows/test.yaml)
55
[![deploy status](https://img.shields.io/github/actions/workflow/status/threeal/errors-cpp/deploy.yaml?branch=main&label=deploy)](https://github.com/threeal/errors-cpp/actions/workflows/deploy.yaml)
66

7-
A C++ package that provides utilities for error handling.
7+
Error C++ is a [C++](https://isocpp.org/) package that provides utilities for error handling.
8+
This package mainly consists of the `errors::Error` class, representing an object that may contain an error.
9+
10+
This package serves as an alternative to error handling using [try-catch exceptions](https://en.cppreference.com/w/cpp/language/try_catch), commonly found in C++ code.
11+
It facilitates error handling by returning values, following the style of [Go's error handling](https://go.dev/blog/error-handling-and-go).
12+
13+
## Key Features
14+
15+
This package provides the following key features:
16+
17+
- Error handling in the style of Go by returning an `errors::Error`.
18+
- Support for creating errors in the style of [fmtlib](https://github.com/fmtlib/fmt) using `errors::format`.
19+
- Direct printing of `errors::Error` using C++ streams and fmtlib.
20+
21+
## Integration
22+
23+
To integrate this package into your C++ project, you can build and install it locally using [CMake](https://cmake.org/):
24+
25+
```sh
26+
cmake -B build .
27+
cmake --build build --config Release
28+
cmake --install build
29+
```
30+
31+
Once installed, you can use it in your CMake project as the `Errors` package:
32+
33+
```cmake
34+
find_package(Errors 1.0.0 REQUIRED CONFIG)
35+
36+
add_executable(foo foo.cpp)
37+
target_link_libraries(foo PRIVATE errors::errors)
38+
```
39+
40+
Alternatively, you can also integrate this package using [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake):
41+
42+
```cmake
43+
cpmaddpackage(gh:threeal/errors-cpp@1.0.0)
44+
45+
add_executable(foo foo.cpp)
46+
target_link_libraries(foo PRIVATE errors::errors)
47+
```
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.
891

992
## License
1093

docs/index.rst

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,105 @@
11
Errors C++
22
=============
33

4-
A `C++`_ package that provides utilities for error handling.
4+
Error C++ is a `C++`_ package that provides utilities for error handling.
5+
This package mainly consists of the `errors::Error` class, representing an object that may contain an error.
6+
7+
This package serves as an alternative to error handling using `try-catch exceptions`_, commonly found in C++ code.
8+
It facilitates error handling by returning values, following the style of `Go's error handling`_.
59

610
.. _C++: https://isocpp.org
11+
.. _try-catch exceptions: https://en.cppreference.com/w/cpp/language/try_catch
12+
.. _Go's error handling: https://go.dev/blog/error-handling-and-go
13+
14+
Key Features
15+
------------
16+
17+
This package provides the following key features:
18+
19+
- Error handling in the style of Go by returning an `errors::Error`.
20+
- Support for creating errors in the style of `fmtlib`_ using `errors::format`.
21+
- Direct printing of `errors::Error` using C++ streams and fmtlib.
22+
23+
.. _fmtlib: https://github.com/fmtlib/fmt
24+
25+
Integration
26+
-----------
27+
28+
To integrate this package into your C++ project, you can build and install it locally using `CMake`_:
29+
30+
.. code-block:: sh
31+
32+
cmake -B build .
33+
cmake --build build --config Release
34+
cmake --install build
35+
36+
Once installed, you can use it in your CMake project as the `Errors` package:
37+
38+
.. code-block:: cmake
39+
40+
find_package(Errors 1.0.0 REQUIRED CONFIG)
41+
42+
add_executable(foo foo.cpp)
43+
target_link_libraries(foo PRIVATE errors::errors)
44+
45+
Alternatively, you can also integrate this package using `CPM.cmake`_:
46+
47+
.. code-block:: cmake
48+
49+
cpmaddpackage(gh:threeal/errors-cpp@1.0.0)
50+
51+
add_executable(foo foo.cpp)
52+
target_link_libraries(foo PRIVATE errors::errors)
53+
54+
.. _CMake: https://cmake.org/
55+
.. _CPM.cmake: https://github.com/cpm-cmake/CPM.cmake
56+
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
7103

8104
API Docs
9105
--------

0 commit comments

Comments
 (0)