From e8f1a2ef025c6ba0e74697fd5c996dd67341c602 Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Tue, 31 Jan 2023 23:36:35 +0800 Subject: [PATCH] LibPDF: Add new error construction functions These should make it easier to create specific errors, specially when wanting to create a formatted message. --- Userland/Libraries/LibPDF/Error.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Userland/Libraries/LibPDF/Error.h b/Userland/Libraries/LibPDF/Error.h index adcf7f51efa..99dbb06b6fc 100644 --- a/Userland/Libraries/LibPDF/Error.h +++ b/Userland/Libraries/LibPDF/Error.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include namespace PDF { @@ -27,6 +28,11 @@ public: } Error(Type type, DeprecatedString const& message) + : Error(type, String::from_deprecated_string(message).release_value_but_fixme_should_propagate_errors()) + { + } + + Error(Type type, String const& message) : m_type(type) { switch (type) { @@ -48,9 +54,33 @@ public: Type type() const { return m_type; } DeprecatedString const& message() const { return m_message; } +#define DEFINE_STATIC_ERROR_FUNCTIONS(name, type) \ + static Error name##_error(StringView message) \ + { \ + return maybe_with_string(Type::type, String::from_utf8(message)); \ + } \ + \ + template \ + static Error name##_error(CheckedFormatString&& fmtstr, Parameters const&... parameters) \ + { \ + return maybe_with_string(Type::type, String::formatted(move(fmtstr), parameters...)); \ + } + + DEFINE_STATIC_ERROR_FUNCTIONS(parse, Parse) + DEFINE_STATIC_ERROR_FUNCTIONS(internal, Internal) + DEFINE_STATIC_ERROR_FUNCTIONS(malformed, MalformedPDF) + DEFINE_STATIC_ERROR_FUNCTIONS(rendering_unsupported, RenderingUnsupported) + private: Type m_type; DeprecatedString m_message; + + static Error maybe_with_string(Type type, ErrorOr maybe_string) + { + if (maybe_string.is_error()) + return Error { type, String {} }; + return Error { type, maybe_string.release_value() }; + } }; class Errors {