/src/botan/src/lib/utils/exceptn.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * (C) 2017 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #include <botan/exceptn.h> |
8 | | |
9 | | #include <botan/internal/fmt.h> |
10 | | |
11 | | namespace Botan { |
12 | | |
13 | 0 | std::string to_string(ErrorType type) { |
14 | 0 | switch(type) { |
15 | 0 | case ErrorType::Unknown: |
16 | 0 | return "Unknown"; |
17 | 0 | case ErrorType::SystemError: |
18 | 0 | return "SystemError"; |
19 | 0 | case ErrorType::NotImplemented: |
20 | 0 | return "NotImplemented"; |
21 | 0 | case ErrorType::OutOfMemory: |
22 | 0 | return "OutOfMemory"; |
23 | 0 | case ErrorType::InternalError: |
24 | 0 | return "InternalError"; |
25 | 0 | case ErrorType::IoError: |
26 | 0 | return "IoError"; |
27 | 0 | case ErrorType::InvalidObjectState: |
28 | 0 | return "InvalidObjectState"; |
29 | 0 | case ErrorType::KeyNotSet: |
30 | 0 | return "KeyNotSet"; |
31 | 0 | case ErrorType::InvalidArgument: |
32 | 0 | return "InvalidArgument"; |
33 | 0 | case ErrorType::InvalidKeyLength: |
34 | 0 | return "InvalidKeyLength"; |
35 | 0 | case ErrorType::InvalidNonceLength: |
36 | 0 | return "InvalidNonceLength"; |
37 | 0 | case ErrorType::LookupError: |
38 | 0 | return "LookupError"; |
39 | 0 | case ErrorType::EncodingFailure: |
40 | 0 | return "EncodingFailure"; |
41 | 0 | case ErrorType::DecodingFailure: |
42 | 0 | return "DecodingFailure"; |
43 | 0 | case ErrorType::TLSError: |
44 | 0 | return "TLSError"; |
45 | 0 | case ErrorType::HttpError: |
46 | 0 | return "HttpError"; |
47 | 0 | case ErrorType::InvalidTag: |
48 | 0 | return "InvalidTag"; |
49 | 0 | case ErrorType::RoughtimeError: |
50 | 0 | return "RoughtimeError"; |
51 | 0 | case ErrorType::CommonCryptoError: |
52 | 0 | return "CommonCryptoError"; |
53 | 0 | case ErrorType::Pkcs11Error: |
54 | 0 | return "Pkcs11Error"; |
55 | 0 | case ErrorType::TPMError: |
56 | 0 | return "TPMError"; |
57 | 0 | case ErrorType::DatabaseError: |
58 | 0 | return "DatabaseError"; |
59 | 0 | case ErrorType::ZlibError: |
60 | 0 | return "ZlibError"; |
61 | 0 | case ErrorType::Bzip2Error: |
62 | 0 | return "Bzip2Error"; |
63 | 0 | case ErrorType::LzmaError: |
64 | 0 | return "LzmaError"; |
65 | 0 | } |
66 | | |
67 | | // No default case in above switch so compiler warns |
68 | 0 | return "Unrecognized Botan error"; |
69 | 0 | } |
70 | | |
71 | 1.37k | Exception::Exception(std::string_view msg) : m_msg(msg) {} |
72 | | |
73 | 0 | Exception::Exception(std::string_view msg, const std::exception& e) : m_msg(fmt("{} failed with {}", msg, e.what())) {} |
74 | | |
75 | 0 | Exception::Exception(const char* prefix, std::string_view msg) : m_msg(fmt("{} {}", prefix, msg)) {} |
76 | | |
77 | 853 | Invalid_Argument::Invalid_Argument(std::string_view msg) : Exception(msg) {} |
78 | | |
79 | | Invalid_Argument::Invalid_Argument(std::string_view msg, std::string_view where) : |
80 | 0 | Exception(fmt("{} in {}", msg, where)) {} |
81 | | |
82 | 0 | Invalid_Argument::Invalid_Argument(std::string_view msg, const std::exception& e) : Exception(msg, e) {} |
83 | | |
84 | | namespace { |
85 | | |
86 | 0 | std::string format_lookup_error(std::string_view type, std::string_view algo, std::string_view provider) { |
87 | 0 | if(provider.empty()) { |
88 | 0 | return fmt("Unavailable {} {}", type, algo); |
89 | 0 | } else { |
90 | 0 | return fmt("Unavailable {} {} for provider {}", type, algo, provider); |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | } // namespace |
95 | | |
96 | | Lookup_Error::Lookup_Error(std::string_view type, std::string_view algo, std::string_view provider) : |
97 | 0 | Exception(format_lookup_error(type, algo, provider)) {} |
98 | | |
99 | 0 | Internal_Error::Internal_Error(std::string_view err) : Exception("Internal error:", err) {} |
100 | | |
101 | | Unknown_PK_Field_Name::Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name) : |
102 | 0 | Invalid_Argument(fmt("Unknown field '{}' for algorithm {}", field_name, algo_name)) {} |
103 | | |
104 | | Invalid_Key_Length::Invalid_Key_Length(std::string_view name, size_t length) : |
105 | 0 | Invalid_Argument(fmt("{} cannot accept a key of length {}", name, length)) {} |
106 | | |
107 | | Invalid_IV_Length::Invalid_IV_Length(std::string_view mode, size_t bad_len) : |
108 | 0 | Invalid_Argument(fmt("IV length {} is invalid for {}", bad_len, mode)) {} |
109 | | |
110 | 0 | Key_Not_Set::Key_Not_Set(std::string_view algo) : Invalid_State(fmt("Key not set in {}", algo)) {} |
111 | | |
112 | 0 | PRNG_Unseeded::PRNG_Unseeded(std::string_view algo) : Invalid_State(fmt("PRNG {} not seeded", algo)) {} |
113 | | |
114 | | Algorithm_Not_Found::Algorithm_Not_Found(std::string_view name) : |
115 | 0 | Lookup_Error(fmt("Could not find any algorithm named '{}'", name)) {} |
116 | | |
117 | | Provider_Not_Found::Provider_Not_Found(std::string_view algo, std::string_view provider) : |
118 | 0 | Lookup_Error(fmt("Could not find provider '{}' for algorithm '{}'", provider, algo)) {} |
119 | | |
120 | | Invalid_Algorithm_Name::Invalid_Algorithm_Name(std::string_view name) : |
121 | 0 | Invalid_Argument(fmt("Invalid algorithm name: '{}'", name)) {} |
122 | | |
123 | 0 | Encoding_Error::Encoding_Error(std::string_view name) : Exception("Encoding error:", name) {} |
124 | | |
125 | 0 | Decoding_Error::Decoding_Error(std::string_view name) : Exception(name) {} |
126 | | |
127 | | Decoding_Error::Decoding_Error(std::string_view category, std::string_view err) : |
128 | 0 | Exception(fmt("{}: {}", category, err)) {} |
129 | | |
130 | 0 | Decoding_Error::Decoding_Error(std::string_view msg, const std::exception& e) : Exception(msg, e) {} |
131 | | |
132 | | Invalid_Authentication_Tag::Invalid_Authentication_Tag(std::string_view msg) : |
133 | 0 | Exception("Invalid authentication tag:", msg) {} |
134 | | |
135 | 0 | Stream_IO_Error::Stream_IO_Error(std::string_view err) : Exception("I/O error:", err) {} |
136 | | |
137 | | System_Error::System_Error(std::string_view msg, int err_code) : |
138 | 0 | Exception(fmt("{} error code {}", msg, err_code)), m_error_code(err_code) {} |
139 | | |
140 | 0 | Not_Implemented::Not_Implemented(std::string_view err) : Exception("Not implemented", err) {} |
141 | | |
142 | | } // namespace Botan |