Botan 3.0.0
Crypto and TLS for C&
exceptn.cpp
Go to the documentation of this file.
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#include <botan/internal/fmt.h>
9
10namespace Botan {
11
12std::string to_string(ErrorType type)
13 {
14 switch(type)
15 {
17 return "Unknown";
19 return "SystemError";
21 return "NotImplemented";
23 return "OutOfMemory";
25 return "InternalError";
27 return "IoError";
29 return "InvalidObjectState";
31 return "KeyNotSet";
33 return "InvalidArgument";
35 return "InvalidKeyLength";
37 return "InvalidNonceLength";
39 return "LookupError";
41 return "EncodingFailure";
43 return "DecodingFailure";
45 return "TLSError";
47 return "HttpError";
49 return "InvalidTag";
51 return "RoughtimeError";
53 return "CommonCryptoError";
55 return "Pkcs11Error";
57 return "TPMError";
59 return "DatabaseError";
61 return "ZlibError";
63 return "Bzip2Error" ;
65 return "LzmaError";
66 }
67
68 // No default case in above switch so compiler warns
69 return "Unrecognized Botan error";
70 }
71
72Exception::Exception(std::string_view msg) : m_msg(msg)
73 {}
74
75Exception::Exception(std::string_view msg, const std::exception& e) :
76 m_msg(fmt("{} failed with {}", msg, e.what()))
77 {}
78
79Exception::Exception(const char* prefix, std::string_view msg) :
80 m_msg(fmt("{} {}", prefix, msg))
81 {}
82
84 Exception(msg)
85 {}
86
87Invalid_Argument::Invalid_Argument(std::string_view msg, std::string_view where) :
88 Exception(fmt("{} in {}", msg, where))
89 {}
90
91Invalid_Argument::Invalid_Argument(std::string_view msg, const std::exception& e) :
92 Exception(msg, e) {}
93
94namespace {
95
96std::string format_lookup_error(std::string_view type,
97 std::string_view algo,
98 std::string_view provider)
99 {
100 if(provider.empty())
101 return fmt("Unavailable {} {}", type, algo);
102 else
103 return fmt("Unavailable {} {} for provider {}", type, algo, provider);
104 }
105
106}
107
108Lookup_Error::Lookup_Error(std::string_view type,
109 std::string_view algo,
110 std::string_view provider) :
111 Exception(format_lookup_error(type, algo, provider))
112 {}
113
114Internal_Error::Internal_Error(std::string_view err) :
115 Exception("Internal error:", err)
116 {}
117
119 std::string_view field_name) :
120 Invalid_Argument(fmt("Unknown field '{}' for algorithm {}", field_name, algo_name))
121 {}
122
123Invalid_Key_Length::Invalid_Key_Length(std::string_view name, size_t length) :
124 Invalid_Argument(fmt("{} cannot accept a key of length {}", name, length))
125 {}
126
127Invalid_IV_Length::Invalid_IV_Length(std::string_view mode, size_t bad_len) :
128 Invalid_Argument(fmt("IV length {} is invalid for {}", bad_len, mode))
129 {}
130
131Key_Not_Set::Key_Not_Set(std::string_view algo) :
132 Invalid_State(fmt("Key not set in {}", algo))
133 {}
134
135PRNG_Unseeded::PRNG_Unseeded(std::string_view algo) :
136 Invalid_State(fmt("PRNG {} not seeded", algo))
137 {}
138
140 Lookup_Error(fmt("Could not find any algorithm named '{}'", name))
141 {}
142
143Provider_Not_Found::Provider_Not_Found(std::string_view algo, std::string_view provider) :
144 Lookup_Error(fmt("Could not find provider '{}' for algorithm '{}'", provider, algo))
145 {}
146
148 Invalid_Argument(fmt("Invalid algorithm name: '{}'", name))
149 {}
150
152 Exception("Encoding error:", name)
153 {}
154
157 {}
158
159Decoding_Error::Decoding_Error(std::string_view category, std::string_view err) :
160 Exception(fmt("{}: {}", category, err))
161 {}
162
163Decoding_Error::Decoding_Error(std::string_view msg, const std::exception& e) :
164 Exception(msg, e)
165 {}
166
168 Exception("Invalid authentication tag:", msg)
169 {}
170
171Stream_IO_Error::Stream_IO_Error(std::string_view err) :
172 Exception("I/O error:", err)
173 {}
174
175System_Error::System_Error(std::string_view msg, int err_code) :
176 Exception(fmt("{} error code {}", msg, err_code)),
177 m_error_code(err_code)
178 {}
179
180Not_Implemented::Not_Implemented(std::string_view err) :
181 Exception("Not implemented", err)
182 {}
183
184}
Algorithm_Not_Found(std::string_view name)
Definition: exceptn.cpp:139
Decoding_Error(std::string_view name)
Definition: exceptn.cpp:155
Encoding_Error(std::string_view name)
Definition: exceptn.cpp:151
Exception(std::string_view msg)
Definition: exceptn.cpp:72
Internal_Error(std::string_view err)
Definition: exceptn.cpp:114
Invalid_Algorithm_Name(std::string_view name)
Definition: exceptn.cpp:147
Invalid_Argument(std::string_view msg)
Definition: exceptn.cpp:83
Invalid_Authentication_Tag(std::string_view msg)
Definition: exceptn.cpp:167
Invalid_IV_Length(std::string_view mode, size_t bad_len)
Definition: exceptn.cpp:127
Invalid_Key_Length(std::string_view name, size_t length)
Definition: exceptn.cpp:123
Key_Not_Set(std::string_view algo)
Definition: exceptn.cpp:131
Lookup_Error(std::string_view err)
Definition: exceptn.h:247
Not_Implemented(std::string_view err)
Definition: exceptn.cpp:180
PRNG_Unseeded(std::string_view algo)
Definition: exceptn.cpp:135
Provider_Not_Found(std::string_view algo, std::string_view provider)
Definition: exceptn.cpp:143
Stream_IO_Error(std::string_view err)
Definition: exceptn.cpp:171
System_Error(std::string_view msg)
Definition: exceptn.h:323
Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name)
Definition: exceptn.cpp:118
std::string name
Definition: alg_id.cpp:12
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60
ErrorType
Definition: exceptn.h:20
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:12