Porytiles
Loading...
Searching...
No Matches
porytiles2::FormattableError Class Referencefinal

General-purpose error implementation with formatted message support. More...

#include <error.hpp>

Inheritance diagram for porytiles2::FormattableError:
[legend]
Collaboration diagram for porytiles2::FormattableError:
[legend]

Public Member Functions

 FormattableError ()=default
 Constructs an empty FormattableError with no message.
 
 FormattableError (std::string text)
 Constructs a FormattableError with a plain text message.
 
 FormattableError (std::string text, std::vector< FormatParam > params)
 Constructs a FormattableError with a format string and styled parameters.
 
template<typename FirstParam , typename... RestParams>
requires ( !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> && std::is_same_v<std::decay_t<FirstParam>, FormatParam> && (std::is_same_v<std::decay_t<RestParams>, FormatParam> && ...))
 FormattableError (std::string text, FirstParam &&first, RestParams &&...rest)
 Constructs a FormattableError with a format string and variadic styled parameters.
 
 FormattableError (std::vector< std::string > lines)
 Constructs a FormattableError with multiple plain text lines.
 
 FormattableError (std::vector< std::string > lines, std::vector< std::vector< FormatParam > > params)
 Constructs a FormattableError with multiple formatted lines.
 
std::vector< std::string > details (const TextFormatter &formatter) const override
 Returns the formatted error message lines with appropriate styling.
 
bool has_details () const
 Checks whether this FormattableError contains any message content.
 
std::unique_ptr< Errorclone () const override
 Creates a polymorphic copy of this FormattableError.
 
- Public Member Functions inherited from porytiles2::Error
virtual ~Error ()=default
 
virtual std::string join (const TextFormatter &formatter, const std::string &delimiter="\n") const
 Joins the error details into a single string with a specified delimiter.
 

Detailed Description

General-purpose error implementation with formatted message support.

FormattableError is a concrete Error implementation designed for common error scenarios where creating a specialized error type would be unnecessary overhead. It supports both simple string messages and formatted messages with styled parameters using TextFormatter and FormatParam.

Key features:

  • Simple construction with a plain string message
  • Format string support with styled parameter substitution using fmtlib syntax
  • Automatic TTY-aware styling through TextFormatter integration
  • Suitable for ad-hoc error reporting without defining custom error types

Example usage:

++
// Simple string error
return FormattableError{"file not found"};
// Formatted error with styled parameters
return FormattableError{"{}: tileset '{}' does not exist",
FormatParam{"error", Style::red | Style::bold}, FormatParam{name, Style::bold}};
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:117
@ bold
Bold text formatting.
@ red
Red text color.

When to use FormattableError vs specialized error types:

  • Use FormattableError for straightforward error messages that don't require custom behavior
  • Use specialized Error subclasses when errors need additional context, state, or special formatting logic

Definition at line 117 of file error.hpp.

Constructor & Destructor Documentation

◆ FormattableError() [1/6]

porytiles2::FormattableError::FormattableError ( )
default

Constructs an empty FormattableError with no message.

Creates a FormattableError with no text content. This is primarily used for error chain passthrough scenarios where the current layer doesn't need to add additional error context. Empty FormattableErrors can be detected using the has_details() method and are typically filtered out during error chain visualization.

◆ FormattableError() [2/6]

porytiles2::FormattableError::FormattableError ( std::string  text)
inlineexplicit

Constructs a FormattableError with a plain text message.

Creates a FormattableError containing a simple string message with no parameter formatting. This constructor is used for straightforward error messages that don't require styled parameters. The message is stored as a single-line error.

Parameters
textThe error message text

Definition at line 139 of file error.hpp.

◆ FormattableError() [3/6]

porytiles2::FormattableError::FormattableError ( std::string  text,
std::vector< FormatParam params 
)
inlineexplicit

Constructs a FormattableError with a format string and styled parameters.

Creates a FormattableError that uses fmtlib-style formatting to substitute styled parameters into the message. The text parameter should contain {} placeholders that will be replaced with the styled text from the params vector when details() is called. The message is stored as a single-line error.

Parameters
textThe format string with {} placeholders
paramsVector of FormatParams to substitute into the format string

Definition at line 157 of file error.hpp.

◆ FormattableError() [4/6]

template<typename FirstParam , typename... RestParams>
requires ( !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> && std::is_same_v<std::decay_t<FirstParam>, FormatParam> && (std::is_same_v<std::decay_t<RestParams>, FormatParam> && ...))
porytiles2::FormattableError::FormattableError ( std::string  text,
FirstParam &&  first,
RestParams &&...  rest 
)
inlineexplicit

Constructs a FormattableError with a format string and variadic styled parameters.

Convenience constructor that allows passing FormatParams directly as arguments instead of wrapping them in a std::vector. This provides more natural syntax for error construction with a known number of parameters. The message is stored as a single-line error.

Example:

++
FormattableError{"expected {} but got {}", FormatParam{expected, Style::green}, FormatParam{actual, Style::red}}
@ green
Green text color.
Template Parameters
FirstParamType of the first parameter
RestParamsTypes of remaining parameters
Parameters
textThe format string with {} placeholders
firstFirst FormatParam argument
restRemaining FormatParam arguments to substitute into the format string

Definition at line 189 of file error.hpp.

◆ FormattableError() [5/6]

porytiles2::FormattableError::FormattableError ( std::vector< std::string >  lines)
inlineexplicit

Constructs a FormattableError with multiple plain text lines.

Creates a FormattableError containing multiple lines of text with no parameter formatting. This constructor is used for multi-line error messages that don't require styled parameters.

Parameters
linesVector of error message lines

Definition at line 209 of file error.hpp.

◆ FormattableError() [6/6]

porytiles2::FormattableError::FormattableError ( std::vector< std::string >  lines,
std::vector< std::vector< FormatParam > >  params 
)
inlineexplicit

Constructs a FormattableError with multiple formatted lines.

Creates a FormattableError with multiple lines, where each line can have its own styled parameters. The lines vector should contain format strings with {} placeholders, and the params vector should contain a corresponding vector of FormatParams for each line.

If params is shorter than lines, the extra lines will have no parameters. If params is longer than lines, the extra parameter vectors will be ignored.

Parameters
linesVector of format strings, one per line
paramsVector of parameter vectors, one per line

Definition at line 225 of file error.hpp.

Member Function Documentation

◆ clone()

std::unique_ptr< Error > porytiles2::FormattableError::clone ( ) const
inlineoverridevirtual

Creates a polymorphic copy of this FormattableError.

Implements the Error clone pattern by creating a new FormattableError with the same text and parameters. This allows FormattableError instances to be copied when building ChainableResult error chains.

Returns
A unique_ptr to a newly allocated copy of this error

Implements porytiles2::Error.

Definition at line 289 of file error.hpp.

◆ details()

std::vector< std::string > porytiles2::FormattableError::details ( const TextFormatter formatter) const
inlineoverridevirtual

Returns the formatted error message lines with appropriate styling.

Generates the error message lines by formatting each line independently. For lines without parameters, the plain text is returned. For lines with parameters, the text is formatted with styled parameters using the provided TextFormatter. The formatter determines whether to apply ANSI styling codes or return plain text based on the output context.

Parameters
formatterThe TextFormatter to use for applying styles
Returns
A vector of formatted error message lines with styling applied as appropriate

Implements porytiles2::Error.

Definition at line 242 of file error.hpp.

◆ has_details()

bool porytiles2::FormattableError::has_details ( ) const
inline

Checks whether this FormattableError contains any message content.

Returns true if the error contains at least one non-empty line, false otherwise. This method is used to distinguish between errors that carry meaningful information and empty errors created for passthrough purposes. Empty errors (created with the default constructor, empty string, or only empty lines) are typically filtered out during error chain visualization in UserDiagnostics::fatal().

Returns
True if the error contains at least one non-empty line, false if the error is empty

Definition at line 270 of file error.hpp.


The documentation for this class was generated from the following file: