Skip to content

Commit 3196ec4

Browse files
authored
docs: update API documentation (#139)
* docs(api): separate `errors::make` and `errors::nil` from `errors::Error` * docs(api): link classes and functions to the API docs * docs(api): update the API documentation of `errors::Error` class * docs(api): update the API documentation of `errors::Error::message` method * docs(api): update the API documentation of `errors::Error::operator bool` method * docs(api): update the API documentation of `errors::operator<<` method * docs(api): update the API documentation of `errors::make` function * docs(api): update the API documentation of `errors::nil` function * docs(api): update the API documentation of `errors::format` function
1 parent 067fb66 commit 3196ec4

File tree

3 files changed

+90
-21
lines changed

3 files changed

+90
-21
lines changed

components/format/include/errors/format.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ namespace errors {
1212
* @param fmt A format string for the message.
1313
* @param args Format arguments.
1414
* @return A new error object.
15+
*
16+
* Use this function to create a new error object with a formatted message.
17+
* Refer to <a href="https://fmt.dev/latest/syntax.html">fmtlib's formatting syntax</a>
18+
* for more information on formatting a message for the error object.
19+
*
20+
* @code{.cpp}
21+
* const int code = 404;
22+
* const auto err = errors::format("HTTP error {}", code);
23+
*
24+
* // Print "error: HTTP error 404".
25+
* fmt::format("{}\n", err)
26+
* @endcode
1527
*/
1628
template <typename... T>
1729
Error format(fmt::format_string<T...> fmt, T&&... args);

docs/index.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Errors C++
22
=============
33

44
Errors 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.
5+
This package mainly consists of the :cpp:class:`errors::Error` class, representing an object that may contain an error.
66

77
This package serves as an alternative to error handling using `try-catch exceptions`_, commonly found in C++ code.
88
It facilitates error handling by returning values, following the style of `Go's error handling`_.
@@ -16,9 +16,9 @@ Key Features
1616

1717
This package provides the following key features:
1818

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.
19+
- Error handling in the style of Go by returning an :cpp:class:`errors::Error`.
20+
- Support for creating errors in the style of `fmtlib`_ using :cpp:func:`errors::format`.
21+
- Direct printing of :cpp:class:`errors::Error` using C++ streams and fmtlib.
2222

2323
.. _fmtlib: https://github.com/fmtlib/fmt
2424

@@ -58,7 +58,7 @@ Alternatively, you can also integrate this package using `CPM.cmake`_:
5858
Usage
5959
-----
6060

61-
This package contains an `errors::Error` class, which represents an error object.
61+
This package contains an :cpp:class:`errors::Error` class, which represents an error object.
6262
Functions that may produce errors should return this object so that the error can be handled properly.
6363

6464
.. code-block:: cpp
@@ -74,7 +74,7 @@ Functions that may produce errors should return this object so that the error ca
7474
// Continue processing if no error.
7575
}
7676
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.
77+
For functions returning :cpp:class:`errors::Error`, use :cpp:func:`errors::nil` function to signify no error or return an error object created from the :cpp:func:`errors::make` function.
7878

7979
.. code-block:: cpp
8080
@@ -89,7 +89,7 @@ For functions returning `errors::Error`, use `errors::nil` function to signify n
8989
return errors::nil();
9090
}
9191
92-
Alternatively, an error object can also be created with a formatted message in the style of fmtlib using `errors::format` function.
92+
Alternatively, an error object can also be created with a formatted message in the style of fmtlib using :cpp:func:`errors::format` function.
9393

9494
.. code-block:: cpp
9595
@@ -106,7 +106,13 @@ API Docs
106106

107107
.. doxygenclass:: errors::Error
108108
:project: errors
109-
:members:
109+
:members: message, operator bool, operator<<
110+
111+
.. doxygenfunction:: errors::make
112+
:project: errors
113+
114+
.. doxygenfunction:: errors::nil
115+
:project: errors
110116

111117
Format Component
112118
^^^^^^^^^^^^^^^^

include/errors/error.hpp

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ namespace errors {
1414

1515
/**
1616
* @brief Represents error information.
17+
*
18+
* Use this class as the return type for functions that may produce errors.
19+
* This class has two state: either it contains an error or not.
20+
* Use boolean operations to check whether an object contains an error or not.
21+
*
22+
* @code{.cpp}
23+
* errors::Error print_hex(const char* number_str) {
24+
* int number = std::atoi(number_str);
25+
* if (number_str[0] != '0' && number == 0) {
26+
* return errors::make("is not a number");
27+
* }
28+
*
29+
* std::cout << std::hex << number << std::endl;
30+
* return errors::nil();
31+
* }
32+
*
33+
* int main() {
34+
* // Print "7b".
35+
* auto err = print_hex("123");
36+
* assert(!err);
37+
*
38+
* // Error.
39+
* err = print_hex("not a number");
40+
* assert(err);
41+
* }
42+
* @endcode
1743
*/
1844
class [[nodiscard]] Error {
1945
private:
@@ -24,29 +50,31 @@ class [[nodiscard]] Error {
2450
public:
2551
/**
2652
* @brief Returns the error message.
53+
* @returns The error message.
54+
*
55+
* Returns the error message as a string view.
56+
* If the object does not contain an error, it will return the string "no error" instead.
2757
*
2858
* @code{.cpp}
2959
* const auto err = errors::make("unknown error");
60+
* const auto no_err = errors::nil();
3061
*
31-
* // Print "unknown error"
32-
* std::cout << err << std::endl;
62+
* // Print "unknown error, no error".
63+
* std::cout << err.message() << ", " << no_err.message() << std::endl;
3364
* @endcode
3465
*/
3566
std::string_view message() const;
3667

3768
/**
38-
* @brief Checks whether the object contains an error.
69+
* @brief Checks whether it contains an error or not.
3970
* @return `true` if it contains an error, otherwise `false`.
4071
*
4172
* @code{.cpp}
4273
* const auto err = errors::make("unknown error");
74+
* assert(err);
4375
*
44-
* // Print "contains error".
45-
* if (err) {
46-
* std::cout << "contains error" << std::endl;
47-
* } else {
48-
* std::cout << "no error" << std::endl;
49-
* }
76+
* const auto no_err = errors::nil();
77+
* assert(!no_err);
5078
* @endcode
5179
*/
5280
explicit operator bool() const;
@@ -61,14 +89,13 @@ class [[nodiscard]] Error {
6189
* @param err The error object.
6290
* @return The output stream.
6391
*
64-
* This operator allows an error object to be printed to the output stream
65-
* using the << operator. The error message will be written to the output
66-
* stream.
92+
* This operator allows an error object to be printed to the output stream using the << operator.
93+
* The error message will be written to the output stream.
6794
*
6895
* @code{.cpp}
6996
* const auto err = errors::make("unknown error");
7097
*
71-
* // Print "error: unknown error"
98+
* // Print "error: unknown error".
7299
* std::cout << err << std::endl;
73100
* @endcode
74101
*/
@@ -79,12 +106,36 @@ class [[nodiscard]] Error {
79106
* @brief Creates a new error object with the given message.
80107
* @param msg The error message.
81108
* @return A new error object.
109+
*
110+
* Use this function to create a new error object with the given message.
111+
* Be aware that creating a new error object with an empty message will treat the object as containing an error.
112+
* Use `errors::nil` instead if you want to create an empty error object.
113+
*
114+
* @code{.cpp}
115+
* auto err = errors::make("unknown error");
116+
* assert(err);
117+
*
118+
* err = errors::make("");
119+
* assert(err);
120+
* @endcode
82121
*/
83122
Error make(std::string_view msg);
84123

85124
/**
86125
* @brief Gets a constant reference of an empty error.
87126
* @return A constant reference of an empty error.
127+
*
128+
* Use this function to initialize a new empty error object.
129+
* If copied onto another error object, it will set that object to be treated as not containing an error.
130+
*
131+
* @code{.cpp}
132+
* const auto no_err = errors::nil();
133+
* assert(!no_err);
134+
*
135+
* auto err = errors::make("unknown error");
136+
* err = errors::nil();
137+
* assert(!err);
138+
* @endcode
88139
*/
89140
const Error& nil();
90141

0 commit comments

Comments
 (0)