Error

Usage

1throw Error(ErrorCategory::Syntax, u8"my message");
1auto location = ...
2throw Error(ErrorCategory::Syntax, u8"my message", location);
1try {
2    // ...
3} catch (const std::system_error &error) {
4    throw Error(ErrorCategory::IO, u8"System error", error.code());
5}
1Location location = ...
2NamePath namePath = ...
3std::filesystem::path filePath = ...
4std::error_code errorCode = ...
5throw Error(ErrorCategory::Syntax, u8"my message", location, namePath, filePath, errorCode);
1try {
2    // ... parse document, etc ...
3} catch (const Error &error) {
4    std::cerr << error.toText().toCharString() << "\n";
5    exit(1);
6}

Interface

class Error : public std::exception

The exception for all errors.

Tested:

ErrorTest

Public Functions

template<typename Msg>
inline Error(const ErrorCategory category, Msg &&message) noexcept

Create a new error with the given message.

Parameters:
  • category – The error category.

  • message – The string with the message.

template<typename Msg, typename ...Args>
inline Error(const ErrorCategory category, Msg &&message, Args&&... args) noexcept

Create a new error with the given message, location, and file path.

Parameters:
  • category – The error category.

  • message – The string with the message.

  • args – Optional arguments for the error message.

Error(const Error&) noexcept = default

Default copy constructor.

Error(Error&&) noexcept = default

Default move constructor.

~Error() noexcept override = default

Default destructor.

Error &operator=(const Error&) noexcept = default

Default copy assignment.

Error &operator=(Error&&) noexcept = default

Default move assignment.

inline ErrorCategory category() const noexcept

Get the error category.

Returns:

The error category.

inline const String &message() const noexcept

Access the message.

Returns:

The message text.

inline Location location() const noexcept

Access the location.

Returns:

The location, or an undefined location if none was set.

inline NamePath namePath() const noexcept

Access the name-path.

Returns:

The name-path, or an empty name-path if none was set.

inline std::filesystem::path filePath() const noexcept

Access the file path.

Returns:

The file path, or an empty file path if none was set.

inline std::error_code errorCode() const noexcept

Access the system-error code.

Returns:

The system-error code, or an undefined one if none was set.

Error withLocation(const Location &location) const noexcept

Create a copy with the given location added or replaced.

Returns:

A copy of this error, with the location added or replaced.

String toText() const noexcept

Convert this error into a text representation.

This method creates a text representation of this error message that is safe to be used in logs, console-outputs, or user-interfaces. Exceedingly long paths and texts are trimmed and control characters are escaped.

Returns:

The string representation of this error.

const char *what() const noexcept override

Return a null-terminated string describing the error (std::exception interface).

Returns:

A C-string with the error message; suitable for logging or exception handling.

class ErrorCategory

The category of an error.

Tested:

ErrorCategoryTest

Public Types

enum Enum

The underlying enum type representing distinct categories of errors.

Values:

enumerator IO

A problem occurred while reading data from an I/O stream.

enumerator Encoding

The document contains a problem with UTF-8 encoding.

enumerator UnexpectedEnd

The document ended unexpectedly.

enumerator Character

The document contains a control character that is not allowed.

enumerator Syntax

The document has a syntax error.

enumerator LimitExceeded

The size of a name, text, or buffer exceeds the permitted limit.

enumerator NameConflict

The same name has already been defined earlier in the document.

enumerator Indentation

The indentation of a continued line does not match the previous line.

enumerator Unsupported

The requested feature/version is not supported by this parser.

enumerator Signature

The document’s signature was rejected.

enumerator Access

The document was rejected due to an access check.

enumerator Validation

The document did not meet one of the validation rules.

enumerator Internal

The parser encountered an unexpected internal error.

enumerator ValueNotFound

A value with a given name-path couldn’t be found.

enumerator TypeMismatch

A value exists but has the wrong type for a conversion.

Public Functions

constexpr ErrorCategory() = default

Create an internal error category.

inline constexpr ErrorCategory(Enum value) noexcept

Create a new error category.

Parameters:

value – The error category enum.

~ErrorCategory() = default

Default destructor.

ErrorCategory(const ErrorCategory&) = default

Default copy constructor.

ErrorCategory &operator=(const ErrorCategory&) = default

Default copy assignment.

inline ErrorCategory &operator=(Enum value) noexcept

Assign a new enum value to this error category.

Parameters:

value – The enum value to assign.

Returns:

Reference to this error category.

inline explicit constexpr operator Enum() const noexcept

Convert to the underlying enum value.

Returns:

The enum representation of this error category.

inline explicit operator int() const noexcept

Convert to the integer error code.

Returns:

The numeric code of this error category.

const String &toText() const noexcept

Get the text representation of this error category.

Returns:

A text representation of this error category.

int toCode() const noexcept

Get the code for this error category.

Returns:

The error code.