API Reference
This page summarizes the public types exposed by the Vix Error module. For the design and usage model, start with the previous pages in this section. This reference is meant for quick lookup while writing or reviewing code.
Headers
#include <vix/error/ErrorCode.hpp>
#include <vix/error/ErrorCategory.hpp>
#include <vix/error/Error.hpp>
#include <vix/error/Result.hpp>
#include <vix/error/Exception.hpp>2
3
4
5
Namespace
All public types are declared in the vix::error namespace.
namespace vix::error
{
}2
3
ErrorCode
enum class ErrorCode : std::uint32_t
{
Ok = 0,
Unknown,
InvalidArgument,
InvalidState,
NotFound,
AlreadyExists,
Timeout,
Cancelled,
PermissionDenied,
Unauthorized,
Forbidden,
NotSupported,
ResourceExhausted,
OutOfMemory,
IoError,
FilesystemError,
NetworkError,
ProtocolError,
ParseError,
ValidationError,
ConfigError,
InternalError,
NotImplemented,
ExternalError
};2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ErrorCode is the canonical code used to describe what went wrong. Ok represents success. Every other value represents a failure.
ErrorCategory
class ErrorCategory
{
public:
constexpr explicit ErrorCategory(std::string_view name) noexcept;
[[nodiscard]] constexpr std::string_view name() const noexcept;
[[nodiscard]] constexpr bool operator==(const ErrorCategory& other) const noexcept;
[[nodiscard]] constexpr bool operator!=(const ErrorCategory& other) const noexcept;
static constexpr ErrorCategory generic() noexcept;
static constexpr ErrorCategory system() noexcept;
static constexpr ErrorCategory io() noexcept;
static constexpr ErrorCategory network() noexcept;
static constexpr ErrorCategory validation() noexcept;
static constexpr ErrorCategory config() noexcept;
static constexpr ErrorCategory security() noexcept;
static constexpr ErrorCategory internal() noexcept;
};2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ErrorCategory stores a lightweight category name. Built-in categories are returned by static factory methods.
Constructor
constexpr explicit ErrorCategory(std::string_view name) noexcept;Creates a category from a name. The name is stored as a std::string_view, so it should refer to stable storage.
auto category = vix::error::ErrorCategory("registry");name
[[nodiscard]] constexpr std::string_view name() const noexcept;Returns the category name.
auto name = vix::error::ErrorCategory::io().name();Built-in categories
ErrorCategory::generic();
ErrorCategory::system();
ErrorCategory::io();
ErrorCategory::network();
ErrorCategory::validation();
ErrorCategory::config();
ErrorCategory::security();
ErrorCategory::internal();2
3
4
5
6
7
8
Error
class Error
{
public:
Error() = default;
explicit Error(ErrorCode code) noexcept;
Error(ErrorCode code, std::string message);
Error(ErrorCode code, ErrorCategory category, std::string message);
[[nodiscard]] constexpr ErrorCode code() const noexcept;
[[nodiscard]] constexpr ErrorCategory category() const noexcept;
[[nodiscard]] std::string_view message() const noexcept;
[[nodiscard]] const char* message_c_str() const noexcept;
[[nodiscard]] constexpr bool ok() const noexcept;
[[nodiscard]] constexpr bool has_error() const noexcept;
[[nodiscard]] explicit constexpr operator bool() const noexcept;
[[nodiscard]] bool operator==(const Error& other) const noexcept;
[[nodiscard]] bool operator!=(const Error& other) const noexcept;
};2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Error stores a code, a category, and a message. A default-constructed Error represents success and uses ErrorCode::Ok.
Constructors
Error() = default;Creates a success-state error object.
vix::error::Error err;
err.ok(); // true
err.has_error(); // false2
3
4
explicit Error(ErrorCode code) noexcept;Creates an error with a code. The category defaults to ErrorCategory::generic().
vix::error::Error err(vix::error::ErrorCode::Unknown);Error(ErrorCode code, std::string message);Creates an error with a code and message. The category defaults to ErrorCategory::generic().
vix::error::Error err(
vix::error::ErrorCode::InvalidArgument,
"invalid input"
);2
3
4
Error(ErrorCode code, ErrorCategory category, std::string message);Creates an error with a code, category, and message.
vix::error::Error err(
vix::error::ErrorCode::ValidationError,
vix::error::ErrorCategory::validation(),
"module name cannot be empty"
);2
3
4
5
code
[[nodiscard]] constexpr ErrorCode code() const noexcept;Returns the stored error code.
category
[[nodiscard]] constexpr ErrorCategory category() const noexcept;Returns the stored error category.
message
[[nodiscard]] std::string_view message() const noexcept;Returns the stored human-readable message as a std::string_view.
message_c_str
[[nodiscard]] const char* message_c_str() const noexcept;Returns the stored message as a null-terminated C string. This is mainly useful for interoperability with APIs such as std::exception::what().
ok
[[nodiscard]] constexpr bool ok() const noexcept;Returns true when the error represents success.
has_error
[[nodiscard]] constexpr bool has_error() const noexcept;Returns true when the error represents a failure.
Boolean conversion
[[nodiscard]] explicit constexpr operator bool() const noexcept;Returns true when the object contains an error.
if (err) {
// err represents a failure
}2
3
Equality
[[nodiscard]] bool operator==(const Error& other) const noexcept;
[[nodiscard]] bool operator!=(const Error& other) const noexcept;2
Two errors are equal when their code, category, and message are equal.
Result
template <typename T>
class Result
{
public:
using value_type = T;
using error_type = Error;
Result(const T& value);
Result(T&& value);
Result(const Error& error);
Result(Error&& error);
[[nodiscard]] bool ok() const noexcept;
[[nodiscard]] bool has_error() const noexcept;
[[nodiscard]] explicit operator bool() const noexcept;
[[nodiscard]] T& value() &;
[[nodiscard]] const T& value() const &;
[[nodiscard]] T&& value() &&;
[[nodiscard]] Error& error() &;
[[nodiscard]] const Error& error() const &;
[[nodiscard]] Error&& error() &&;
template <typename F>
[[nodiscard]] auto map(F&& fn) const
-> Result<std::invoke_result_t<F, T>>;
template <typename F>
[[nodiscard]] auto and_then(F&& fn) const
-> decltype(fn(std::declval<T>()));
};2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Result<T> stores either a success value of type T or an Error. It is the preferred return type for Vix APIs that produce a value and can fail.
The primary template does not support reference types, Result<Error>, or Result<void>.
Constructors
Result(const T& value);
Result(T&& value);2
Creates a successful result.
vix::error::Result<int> result(42);Result(const Error& error);
Result(Error&& error);2
Creates a failed result. The provided Error must represent a real failure. Passing a success-state Error throws std::invalid_argument.
vix::error::Result<int> result(
vix::error::Error(
vix::error::ErrorCode::InvalidArgument,
vix::error::ErrorCategory::validation(),
"division by zero"
)
);2
3
4
5
6
7
ok
[[nodiscard]] bool ok() const noexcept;Returns true when the result contains a value.
has_error
[[nodiscard]] bool has_error() const noexcept;Returns true when the result contains an error.
Boolean conversion
[[nodiscard]] explicit operator bool() const noexcept;Returns true when the result contains a value.
if (result) {
// result contains a value
}2
3
value
[[nodiscard]] T& value() &;
[[nodiscard]] const T& value() const &;
[[nodiscard]] T&& value() &&;2
3
Returns the stored success value. Call this only when ok() is true.
if (result) {
int value = result.value();
}2
3
error
[[nodiscard]] Error& error() &;
[[nodiscard]] const Error& error() const &;
[[nodiscard]] Error&& error() &&;2
3
Returns the stored error. Call this only when has_error() is true.
if (!result) {
const auto& err = result.error();
}2
3
map
template <typename F>
[[nodiscard]] auto map(F&& fn) const
-> Result<std::invoke_result_t<F, T>>;2
3
Transforms the success value and propagates the error unchanged.
auto doubled = result.map([](int value) {
return value * 2;
});2
3
Use map() when the next operation cannot fail and only transforms the value.
and_then
template <typename F>
[[nodiscard]] auto and_then(F&& fn) const
-> decltype(fn(std::declval<T>()));2
3
Chains another operation that returns a Result.
auto result = parse_port("8080")
.and_then([](int port) {
return validate_port_range(port);
});2
3
4
Use and_then() when the next operation can also fail.
Exception
class Exception : public std::exception
{
public:
explicit Exception(Error error) noexcept;
Exception(ErrorCode code, std::string message) noexcept;
Exception(ErrorCode code, ErrorCategory category, std::string message) noexcept;
const char* what() const noexcept override;
[[nodiscard]] const Error& error() const noexcept;
};2
3
4
5
6
7
8
9
10
11
12
Exception wraps an Error inside a standard C++ exception. It is useful when structured Vix errors need to cross exception-based code paths.
Constructors
explicit Exception(Error error) noexcept;Creates an exception from an existing Error.
throw vix::error::Exception(
vix::error::Error(
vix::error::ErrorCode::IoError,
vix::error::ErrorCategory::io(),
"failed to open file"
)
);2
3
4
5
6
7
Exception(ErrorCode code, std::string message) noexcept;Creates an exception from a code and message. The category defaults to ErrorCategory::generic().
throw vix::error::Exception(
vix::error::ErrorCode::InvalidState,
"runtime has already stopped"
);2
3
4
Exception(ErrorCode code, ErrorCategory category, std::string message) noexcept;Creates an exception from a code, category, and message.
throw vix::error::Exception(
vix::error::ErrorCode::IoError,
vix::error::ErrorCategory::io(),
"failed to open file"
);2
3
4
5
what
const char* what() const noexcept override;Returns the wrapped error message as a C string.
error
[[nodiscard]] const Error& error() const noexcept;Returns the wrapped Error object.
try {
load_project();
} catch (const vix::error::Exception& ex) {
const auto& err = ex.error();
}2
3
4
5
Minimal example
#include <vix/error/Error.hpp>
#include <vix/error/Result.hpp>
vix::error::Result<int> divide(int a, int b)
{
if (b == 0) {
return vix::error::Error(
vix::error::ErrorCode::InvalidArgument,
vix::error::ErrorCategory::validation(),
"division by zero"
);
}
return a / b;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
auto result = divide(10, 2);
if (!result) {
const auto& err = result.error();
return;
}
int value = result.value();2
3
4
5
6
7
8
This completes the Error module reference.
