mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
LibPDF: Add new error construction functions
These should make it easier to create specific errors, specially when wanting to create a formatted message.
This commit is contained in:
parent
db9fa7ff07
commit
e8f1a2ef02
Notes:
sideshowbarker
2024-07-17 04:41:05 +09:00
Author: https://github.com/rtobar Commit: https://github.com/SerenityOS/serenity/commit/e8f1a2ef02 Pull-request: https://github.com/SerenityOS/serenity/pull/17446 Reviewed-by: https://github.com/linusg
1 changed files with 30 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
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<typename... Parameters> \
|
||||
static Error name##_error(CheckedFormatString<Parameters...>&& 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<String> maybe_string)
|
||||
{
|
||||
if (maybe_string.is_error())
|
||||
return Error { type, String {} };
|
||||
return Error { type, maybe_string.release_value() };
|
||||
}
|
||||
};
|
||||
|
||||
class Errors {
|
||||
|
|
Loading…
Reference in a new issue