Error.h 3.5 KB

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