/src/serenity/Userland/Libraries/LibPDF/Error.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteString.h> |
10 | | #include <AK/String.h> |
11 | | #include <AK/Vector.h> |
12 | | |
13 | | namespace PDF { |
14 | | |
15 | | class [[nodiscard]] Error { |
16 | | public: |
17 | | enum class Type { |
18 | | Parse, |
19 | | Internal, |
20 | | MalformedPDF, |
21 | | RenderingUnsupported |
22 | | }; |
23 | | |
24 | | Error(AK::Error error) |
25 | 0 | : m_type(Type::Internal) |
26 | 0 | , m_message(ByteString::formatted("Internal error while processing PDF file: {}", error.string_literal())) |
27 | 0 | { |
28 | 0 | } |
29 | | |
30 | | Error(Type type, ByteString const& message) |
31 | 15 | : Error(type, String::from_byte_string(message).release_value_but_fixme_should_propagate_errors()) |
32 | 15 | { |
33 | 15 | } |
34 | | |
35 | | Error(Type type, String const& message) |
36 | 15 | : m_type(type) |
37 | 15 | { |
38 | 15 | switch (type) { |
39 | 15 | case Type::Parse: |
40 | 15 | m_message = ByteString::formatted("Failed to parse PDF file: {}", message); |
41 | 15 | break; |
42 | 0 | case Type::Internal: |
43 | 0 | m_message = ByteString::formatted("Internal error while processing PDF file: {}", message); |
44 | 0 | break; |
45 | 0 | case Type::MalformedPDF: |
46 | 0 | m_message = ByteString::formatted("Malformed PDF file: {}", message); |
47 | 0 | break; |
48 | 0 | case Type::RenderingUnsupported: |
49 | 0 | m_message = ByteString::formatted("Rendering of feature not supported: {}", message); |
50 | 0 | break; |
51 | 15 | } |
52 | 15 | } |
53 | | |
54 | 0 | Type type() const { return m_type; } |
55 | 0 | ByteString const& message() const { return m_message; } |
56 | | |
57 | | #define DEFINE_STATIC_ERROR_FUNCTIONS(name, type) \ |
58 | | static Error name##_error(StringView message) \ |
59 | 0 | { \ |
60 | 0 | return maybe_with_string(Type::type, String::from_utf8(message)); \ |
61 | 0 | } \ Unexecuted instantiation: PDF::Error::parse_error(AK::StringView) Unexecuted instantiation: PDF::Error::internal_error(AK::StringView) Unexecuted instantiation: PDF::Error::malformed_error(AK::StringView) Unexecuted instantiation: PDF::Error::rendering_unsupported_error(AK::StringView) |
62 | | \ |
63 | | template<typename... Parameters> \ |
64 | | static Error name##_error(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters) \ |
65 | 0 | { \ |
66 | 0 | return maybe_with_string(Type::type, String::formatted(move(fmtstr), parameters...)); \ |
67 | 0 | } Unexecuted instantiation: PDF::Error PDF::Error::malformed_error<>(AK::Format::Detail::CheckedFormatString<>&&) Unexecuted instantiation: PDF::Error PDF::Error::rendering_unsupported_error<>(AK::Format::Detail::CheckedFormatString<>&&) |
68 | | |
69 | | DEFINE_STATIC_ERROR_FUNCTIONS(parse, Parse) |
70 | | DEFINE_STATIC_ERROR_FUNCTIONS(internal, Internal) |
71 | | DEFINE_STATIC_ERROR_FUNCTIONS(malformed, MalformedPDF) |
72 | | DEFINE_STATIC_ERROR_FUNCTIONS(rendering_unsupported, RenderingUnsupported) |
73 | | |
74 | | private: |
75 | | Type m_type; |
76 | | ByteString m_message; |
77 | | |
78 | | static Error maybe_with_string(Type type, ErrorOr<String> maybe_string) |
79 | 0 | { |
80 | 0 | if (maybe_string.is_error()) |
81 | 0 | return Error { type, String {} }; |
82 | 0 | return Error { type, maybe_string.release_value() }; |
83 | 0 | } |
84 | | }; |
85 | | |
86 | | class Errors { |
87 | | |
88 | | public: |
89 | | Errors() = default; |
90 | | Errors(Error&& error) |
91 | 0 | { |
92 | 0 | m_errors.empend(move(error)); |
93 | 0 | } |
94 | | |
95 | | Vector<Error> const& errors() const |
96 | 0 | { |
97 | 0 | return m_errors; |
98 | 0 | } |
99 | | |
100 | | void add_error(Error&& error) |
101 | 0 | { |
102 | 0 | m_errors.empend(move(error)); |
103 | 0 | } |
104 | | |
105 | | private: |
106 | | Vector<Error> m_errors; |
107 | | }; |
108 | | |
109 | | template<typename T> |
110 | | using PDFErrorOr = ErrorOr<T, Error>; |
111 | | |
112 | | template<typename T> |
113 | | using PDFErrorsOr = ErrorOr<T, Errors>; |
114 | | |
115 | | } |