|
1 | 1 | Errors C++
|
2 | 2 | =============
|
3 | 3 |
|
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`_. |
5 | 9 |
|
6 | 10 | .. _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 |
7 | 103 |
|
8 | 104 | API Docs
|
9 | 105 | --------
|
|
0 commit comments