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
Errors C++ is a C++ package that provides utilities for error handling.
This package mainly consists of the errors::Error class, representing an object that may contain an error.
This package serves as an alternative to error handling using try-catch exceptions, commonly found in C++ code.
It facilitates error handling by returning values, following the style of Go's error handling.
Key Features
This package provides the following key features:
Error handling in the style of Go by returning an errors::Error.
Support for creating errors in the style of fmtlib using errors::format.
Direct printing of errors::Error using C++ streams and fmtlib.
Integration
To integrate this package into your C++ project, you can build and install it locally using CMake:
This package contains an errors::Error class, which represents an error object.
Functions that may produce errors should return this object so that the error can be handled properly.
errors::Error read_file(constchar* filepath);
intmain() {
constauto err = read_file(filepath);
if (err) {
// Handle the error.
}
// Continue processing if no error.
}
For functions returning errors::Error, use errors::nil function to signify no error or return an error object created from the errors::make function.
errors::Error read_file(constchar* filepath) {
std::ifstream file(filepath);
if (!file.is_open()) {
returnerrors::make("failed to open file");
}
// Process with no error.returnerrors::nil();
}
Alternatively, an error object can also be created with a formatted message in the style of fmtlib using errors::format function.
if (!file.is_open()) {
returnerrors::format("failed to open '{}'", filepath);
}
For more details and examples, refer to the examples directory.
License
This project is licensed under the terms of the MIT License.