×


Custom Formatter Error Handling




format_error

This class is used as a compatible drop-in for the <format>'s' and fmtlib's respective format_error class; of which the main purpose to issue warnings and errors from the custom formatters listed below. Serenity's 'format_error' inherits from std::runtime_error.


class format_error: public std::runtime_error
{
  public:
    inline explicit format_error(const char* message): std::runtime_error(message) { }
    inline explicit format_error(const std::string& message): std::runtime_error(message) { }
    inline format_error(const format_error&)            = default;
    inline format_error& operator=(const format_error&) = default;
    inline format_error(format_error&&)                 = default;
    inline format_error& operator=(format_error&&)      = default;
    inline ~format_error() noexcept override            = default;
};

custom_error_messages

This static array is used to index into from the ReportCustomError() function to issue the warning or error and contains the following messages:


static constexpr std::array custom_error_messages = {
    "Unknown Formatting Error Occurred In Internal Serenity CustomFlag Formatting.",
    "Error In Internal CustomFlag Formatting For Serenity: Missing '%' before flag.",
    "Error In Internal CustomFlag Formatting For Serenity:  An Escaped Opening Bracket Was Detected - Missing Escaped Closing Bracket.",
    "Error In Internal CustomFlag Formatting For Serenity:  An Unsupported Flag Was Encountered.",
    "Error In Internal CustomFlag Formatting For Serenity:  Flag Specifier Token Found, But With No Flag Present.",
};

            
enum CustomErrors

This enum is what is used specifically for `ReportCustomError()` as well as what is used to map the error messages to the actual error.


enum class CustomErrors
{
    missing_enclosing_bracket,
    missing_flag_specifier,
    unknown_flag,
    token_with_no_flag,
    no_flag_mods,
};
                
ReportCustomError()

This function is essentially a wrapper that uses a `CustomErrors` enum value in order to index into the `custom_error_messages` array for error reporting.


[[noreturn]] constexpr void ReportCustomError(CustomErrors err) {
    using enum CustomErrors;
    switch( err ) {
            case missing_flag_specifier: throw format_error(custom_error_messages[ 1 ]); break;
            case missing_enclosing_bracket: throw format_error(custom_error_messages[ 2 ]); break;
            case unknown_flag: throw format_error(custom_error_messages[ 3 ]); break;
            case token_with_no_flag: throw format_error(custom_error_messages[ 4 ]); break;
            case no_flag_mods: throw format_error(custom_error_messages[ 5 ]); break;
            default: throw format_error(custom_error_messages[ 0 ]); break;
        }
}