/src/rnp/src/libsexpp/include/sexpp/sexp-error.h
Line | Count | Source |
1 | | /** |
2 | | * |
3 | | * Copyright 2021-2023 Ribose Inc. (https://www.ribose.com) |
4 | | * |
5 | | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
6 | | * this software and associated documentation files (the "Software"), to deal in |
7 | | * the Software without restriction, including without limitation the rights to |
8 | | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
9 | | * the Software, and to permit persons to whom the Software is furnished to do so, |
10 | | * subject to the following conditions: |
11 | | * |
12 | | * The above copyright notice and this permission notice shall be included in all |
13 | | * copies or substantial portions of the Software. |
14 | | * |
15 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
17 | | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
18 | | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
19 | | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
20 | | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 | | * |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | #include <cstdint> |
27 | | #include <exception> |
28 | | #include <iostream> |
29 | | #include <string> |
30 | | #include <cstdint> |
31 | | |
32 | | #include "sexp-public.h" |
33 | | |
34 | | namespace sexp { |
35 | | |
36 | | class SEXP_PUBLIC_SYMBOL sexp_exception_t : public std::exception { |
37 | | public: |
38 | | enum severity { error = 0, warning = 1 }; |
39 | | |
40 | | protected: |
41 | | static severity verbosity; |
42 | | static bool interactive; |
43 | | |
44 | | int position; // May be EOF aka -1 |
45 | | severity level; |
46 | | std::string message; |
47 | | |
48 | | public: |
49 | | sexp_exception_t(std::string error_message, |
50 | | severity error_level, |
51 | | int error_position, |
52 | | const char *prefix = "SEXP") |
53 | 2.81k | : position{error_position}, level{error_level}, |
54 | 2.81k | message{format(prefix, std::move(error_message), error_level, error_position)} {}; |
55 | | |
56 | | static std::string format(std::string prf, |
57 | | std::string message, |
58 | | severity level, |
59 | | int position); |
60 | | |
61 | 10.3k | static bool shall_throw(severity level) { return level == error || verbosity != error; }; |
62 | 0 | virtual const char *what(void) const throw() { return message.c_str(); }; |
63 | 0 | severity get_level(void) const { return level; }; |
64 | 0 | uint32_t get_position(void) const { return position; }; |
65 | 0 | static severity get_verbosity(void) { return verbosity; }; |
66 | 7.57k | static bool is_interactive(void) { return interactive; }; |
67 | 0 | static void set_verbosity(severity new_verbosity) { verbosity = new_verbosity; }; |
68 | 0 | static void set_interactive(bool new_interactive) { interactive = new_interactive; }; |
69 | | }; |
70 | | |
71 | | void SEXP_PUBLIC_SYMBOL |
72 | | sexp_error(sexp_exception_t::severity level, const char *msg, size_t c1, size_t c2, int pos); |
73 | | |
74 | | } // namespace sexp |