|
| 1 | +#ifndef KAITAI_EXCEPTIONS_H |
| 2 | +#define KAITAI_EXCEPTIONS_H |
| 3 | + |
| 4 | +#include <kaitai/kaitaistream.h> |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <stdexcept> |
| 8 | + |
| 9 | +// We need to use "noexcept" in virtual destructor of our exceptions |
| 10 | +// subclasses. Different compilers have different ideas on how to |
| 11 | +// achieve that: C++98 compilers prefer `throw()`, C++11 and later |
| 12 | +// use `noexcept`. We define KS_NOEXCEPT macro for that. |
| 13 | + |
| 14 | +#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900) |
| 15 | +#define KS_NOEXCEPT noexcept |
| 16 | +#else |
| 17 | +#define KS_NOEXCEPT throw() |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace kaitai { |
| 21 | + |
| 22 | +/** |
| 23 | + * Common ancestor for all errors related to `bytes_to_str` operation. Also used |
| 24 | + * to signal misc non-specific `bytes_to_str` failures. |
| 25 | + */ |
| 26 | +class bytes_to_str_error: public std::runtime_error { |
| 27 | +public: |
| 28 | + bytes_to_str_error(const std::string what): |
| 29 | + std::runtime_error(std::string("bytes_to_str error: ") + what) {} |
| 30 | + |
| 31 | + virtual ~bytes_to_str_error() KS_NOEXCEPT {}; |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * Exception to signal that `bytes_to_str` operation was requested to use some encoding |
| 36 | + * that is not available in given runtime environment. |
| 37 | + */ |
| 38 | +class unknown_encoding: public bytes_to_str_error { |
| 39 | +public: |
| 40 | + unknown_encoding(const std::string enc_name): |
| 41 | + bytes_to_str_error(std::string("unknown encoding: `") + enc_name + std::string("`")) {} |
| 42 | + |
| 43 | + virtual ~unknown_encoding() KS_NOEXCEPT {}; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Exception to signal that `bytes_to_str` operation failed to decode given byte sequence. |
| 48 | + */ |
| 49 | +class illegal_seq_in_encoding: public bytes_to_str_error { |
| 50 | +public: |
| 51 | + illegal_seq_in_encoding(const std::string what): |
| 52 | + bytes_to_str_error("illegal sequence: " + what) {} |
| 53 | + |
| 54 | + virtual ~illegal_seq_in_encoding() KS_NOEXCEPT {}; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * Common ancestor for all error originating from Kaitai Struct usage. |
| 59 | + * Stores KSY source path, pointing to an element supposedly guilty of |
| 60 | + * an error. |
| 61 | + */ |
| 62 | +class kstruct_error: public std::runtime_error { |
| 63 | +public: |
| 64 | + kstruct_error(const std::string what, const std::string src_path): |
| 65 | + std::runtime_error(src_path + ": " + what), |
| 66 | + m_src_path(src_path) |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + virtual ~kstruct_error() KS_NOEXCEPT {}; |
| 71 | + |
| 72 | +protected: |
| 73 | + const std::string m_src_path; |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Error that occurs when default endianness should be decided with |
| 78 | + * a switch, but nothing matches (although using endianness expression |
| 79 | + * implies that there should be some positive result). |
| 80 | + */ |
| 81 | +class undecided_endianness_error: public kstruct_error { |
| 82 | +public: |
| 83 | + undecided_endianness_error(const std::string src_path): |
| 84 | + kstruct_error("unable to decide on endianness for a type", src_path) |
| 85 | + { |
| 86 | + } |
| 87 | + |
| 88 | + virtual ~undecided_endianness_error() KS_NOEXCEPT {}; |
| 89 | +}; |
| 90 | + |
| 91 | +/** |
| 92 | + * Common ancestor for all validation failures. Stores pointer to |
| 93 | + * KaitaiStream IO object which was involved in an error. |
| 94 | + */ |
| 95 | +class validation_failed_error: public kstruct_error { |
| 96 | +public: |
| 97 | + validation_failed_error(const std::string what, kstream* io, const std::string src_path): |
| 98 | + kstruct_error("at pos " + kstream::to_string(io->pos()) + ": validation failed: " + what, src_path), |
| 99 | + m_io(io) |
| 100 | + { |
| 101 | + } |
| 102 | + |
| 103 | +// "at pos #{io.pos}: validation failed: #{msg}" |
| 104 | + |
| 105 | + virtual ~validation_failed_error() KS_NOEXCEPT {}; |
| 106 | + |
| 107 | +protected: |
| 108 | + kstream* m_io; |
| 109 | +}; |
| 110 | + |
| 111 | +/** |
| 112 | + * Signals validation failure: we required "actual" value to be equal to |
| 113 | + * "expected", but it turned out that it's not. |
| 114 | + */ |
| 115 | +template<typename T> |
| 116 | +class validation_not_equal_error: public validation_failed_error { |
| 117 | +public: |
| 118 | + validation_not_equal_error(const T& expected, const T& actual, kstream* io, const std::string src_path): |
| 119 | + validation_failed_error("not equal", io, src_path), |
| 120 | + m_expected(expected), |
| 121 | + m_actual(actual) |
| 122 | + { |
| 123 | + } |
| 124 | + |
| 125 | + // "not equal, expected #{expected.inspect}, but got #{actual.inspect}" |
| 126 | + |
| 127 | + virtual ~validation_not_equal_error() KS_NOEXCEPT {}; |
| 128 | + |
| 129 | +protected: |
| 130 | + const T& m_expected; |
| 131 | + const T& m_actual; |
| 132 | +}; |
| 133 | + |
| 134 | +/** |
| 135 | + * Signals validation failure: we required "actual" value to be greater |
| 136 | + * than or equal to "min", but it turned out that it's not. |
| 137 | + */ |
| 138 | +template<typename T> |
| 139 | +class validation_less_than_error: public validation_failed_error { |
| 140 | +public: |
| 141 | + validation_less_than_error(const T& min, const T& actual, kstream* io, const std::string src_path): |
| 142 | + validation_failed_error("not in range", io, src_path), |
| 143 | + m_min(min), |
| 144 | + m_actual(actual) |
| 145 | + { |
| 146 | + } |
| 147 | + |
| 148 | + // "not in range, min #{min.inspect}, but got #{actual.inspect}" |
| 149 | + |
| 150 | + virtual ~validation_less_than_error() KS_NOEXCEPT {}; |
| 151 | + |
| 152 | +protected: |
| 153 | + const T& m_min; |
| 154 | + const T& m_actual; |
| 155 | +}; |
| 156 | + |
| 157 | +/** |
| 158 | + * Signals validation failure: we required "actual" value to be less |
| 159 | + * than or equal to "max", but it turned out that it's not. |
| 160 | + */ |
| 161 | +template<typename T> |
| 162 | +class validation_greater_than_error: public validation_failed_error { |
| 163 | +public: |
| 164 | + validation_greater_than_error(const T& max, const T& actual, kstream* io, const std::string src_path): |
| 165 | + validation_failed_error("not in range", io, src_path), |
| 166 | + m_max(max), |
| 167 | + m_actual(actual) |
| 168 | + { |
| 169 | + } |
| 170 | + |
| 171 | + // "not in range, max #{max.inspect}, but got #{actual.inspect}" |
| 172 | + |
| 173 | + virtual ~validation_greater_than_error() KS_NOEXCEPT {}; |
| 174 | + |
| 175 | +protected: |
| 176 | + const T& m_max; |
| 177 | + const T& m_actual; |
| 178 | +}; |
| 179 | + |
| 180 | +/** |
| 181 | + * Signals validation failure: we required "actual" value to be from |
| 182 | + * the list, but it turned out that it's not. |
| 183 | + */ |
| 184 | +template<typename T> |
| 185 | +class validation_not_any_of_error: public validation_failed_error { |
| 186 | +public: |
| 187 | + validation_not_any_of_error(const T& actual, kstream* io, const std::string src_path): |
| 188 | + validation_failed_error("not any of the list", io, src_path), |
| 189 | + m_actual(actual) |
| 190 | + { |
| 191 | + } |
| 192 | + |
| 193 | + // "not any of the list, got #{actual.inspect}" |
| 194 | + |
| 195 | + virtual ~validation_not_any_of_error() KS_NOEXCEPT {}; |
| 196 | + |
| 197 | +protected: |
| 198 | + const T& m_actual; |
| 199 | +}; |
| 200 | + |
| 201 | +/** |
| 202 | + * Signals validation failure: we required "actual" value to match |
| 203 | + * the expression, but it turned out that it doesn't. |
| 204 | + */ |
| 205 | +template<typename T> |
| 206 | +class validation_expr_error: public validation_failed_error { |
| 207 | +public: |
| 208 | + validation_expr_error(const T& actual, kstream* io, const std::string src_path): |
| 209 | + validation_failed_error("not matching the expression", io, src_path), |
| 210 | + m_actual(actual) |
| 211 | + { |
| 212 | + } |
| 213 | + |
| 214 | + // "not matching the expression, got #{actual.inspect}" |
| 215 | + |
| 216 | + virtual ~validation_expr_error() KS_NOEXCEPT {}; |
| 217 | + |
| 218 | +protected: |
| 219 | + const T& m_actual; |
| 220 | +}; |
| 221 | + |
| 222 | +} |
| 223 | + |
| 224 | +#endif |
0 commit comments