Botan  1.11.32
exceptn.h
Go to the documentation of this file.
1 /*
2 * Exceptions
3 * (C) 1999-2009 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_EXCEPTION_H__
9 #define BOTAN_EXCEPTION_H__
10 
11 #include <botan/types.h>
12 #include <botan/parsing.h>
13 #include <exception>
14 #include <string>
15 
16 namespace Botan {
17 
18 /**
19 * Base class for all exceptions thrown by the library
20 */
21 class BOTAN_DLL Exception : public std::exception
22  {
23  public:
24  explicit Exception(const std::string& msg) : m_msg(msg) {}
25  Exception(const char* prefix, const std::string& msg) : m_msg(std::string(prefix) + " " + msg) {}
26  const char* what() const BOTAN_NOEXCEPT override { return m_msg.c_str(); }
27  private:
28  std::string m_msg;
29  };
30 
31 /**
32 * An invalid argument
33 */
34 class BOTAN_DLL Invalid_Argument : public Exception
35  {
36  public:
37  explicit Invalid_Argument(const std::string& msg) :
38  Exception("Invalid argument", msg) {}
39 
40  explicit Invalid_Argument(const std::string& msg, const std::string& where) :
41  Exception("Invalid argument", msg + " in " + where) {}
42 };
43 
44 #define BOTAN_ARG_CHECK(expr) \
45  do { if(!(expr)) throw Invalid_Argument(#expr, BOTAN_CURRENT_FUNCTION); } while(0)
46 
47 /**
48 * Unsupported_Argument Exception
49 *
50 * An argument that is invalid because it is not supported by Botan.
51 * It might or might not be valid in another context like a standard.
52 */
53 struct BOTAN_DLL Unsupported_Argument : public Invalid_Argument
54  {
55  explicit Unsupported_Argument(const std::string& msg) : Invalid_Argument(msg) {}
56  };
57 
58 /**
59 * Invalid_State Exception
60 */
61 struct BOTAN_DLL Invalid_State : public Exception
62  {
63  explicit Invalid_State(const std::string& err) :
64  Exception(err)
65  {}
66  };
67 
68 /**
69 * Lookup_Error Exception
70 */
71 struct BOTAN_DLL Lookup_Error : public Exception
72  {
73  explicit Lookup_Error(const std::string& err) :
74  Exception(err)
75  {}
76  };
77 
78 /**
79 * Internal_Error Exception
80 */
81 struct BOTAN_DLL Internal_Error : public Exception
82  {
83  explicit Internal_Error(const std::string& err) :
84  Exception("Internal error: " + err)
85  {}
86  };
87 
88 /**
89 * Invalid_Key_Length Exception
90 */
91 struct BOTAN_DLL Invalid_Key_Length : public Invalid_Argument
92  {
93  Invalid_Key_Length(const std::string& name, size_t length) :
94  Invalid_Argument(name + " cannot accept a key of length " +
95  std::to_string(length))
96  {}
97  };
98 
99 /**
100 * Invalid_IV_Length Exception
101 */
102 struct BOTAN_DLL Invalid_IV_Length : public Invalid_Argument
103  {
104  Invalid_IV_Length(const std::string& mode, size_t bad_len) :
105  Invalid_Argument("IV length " + std::to_string(bad_len) +
106  " is invalid for " + mode)
107  {}
108  };
109 
110 /**
111 * PRNG_Unseeded Exception
112 */
113 struct BOTAN_DLL PRNG_Unseeded : public Invalid_State
114  {
115  explicit PRNG_Unseeded(const std::string& algo) :
116  Invalid_State("PRNG not seeded: " + algo)
117  {}
118  };
119 
120 /**
121 * Policy_Violation Exception
122 */
123 struct BOTAN_DLL Policy_Violation : public Invalid_State
124  {
125  explicit Policy_Violation(const std::string& err) :
126  Invalid_State("Policy violation: " + err)
127  {}
128  };
129 
130 /**
131 * Algorithm_Not_Found Exception
132 */
133 struct BOTAN_DLL Algorithm_Not_Found : public Lookup_Error
134  {
135  explicit Algorithm_Not_Found(const std::string& name) :
136  Lookup_Error("Could not find any algorithm named \"" + name + "\"")
137  {}
138  };
139 
140 /**
141 * No_Provider_Found Exception
142 */
143 struct BOTAN_DLL No_Provider_Found : public Exception
144  {
145  explicit No_Provider_Found(const std::string& name) :
146  Exception("Could not find any provider for algorithm named \"" + name + "\"")
147  {}
148  };
149 
150 /**
151 * Invalid_Algorithm_Name Exception
152 */
153 struct BOTAN_DLL Invalid_Algorithm_Name : public Invalid_Argument
154  {
155  explicit Invalid_Algorithm_Name(const std::string& name):
156  Invalid_Argument("Invalid algorithm name: " + name)
157  {}
158  };
159 
160 /**
161 * Encoding_Error Exception
162 */
163 struct BOTAN_DLL Encoding_Error : public Invalid_Argument
164  {
165  explicit Encoding_Error(const std::string& name) :
166  Invalid_Argument("Encoding error: " + name) {}
167  };
168 
169 /**
170 * Decoding_Error Exception
171 */
172 struct BOTAN_DLL Decoding_Error : public Invalid_Argument
173  {
174  explicit Decoding_Error(const std::string& name) :
175  Invalid_Argument("Decoding error: " + name) {}
176  };
177 
178 /**
179 * Integrity_Failure Exception
180 */
181 struct BOTAN_DLL Integrity_Failure : public Exception
182  {
183  explicit Integrity_Failure(const std::string& msg) :
184  Exception("Integrity failure: " + msg) {}
185  };
186 
187 /**
188 * Invalid_OID Exception
189 */
190 struct BOTAN_DLL Invalid_OID : public Decoding_Error
191  {
192  explicit Invalid_OID(const std::string& oid) :
193  Decoding_Error("Invalid ASN.1 OID: " + oid) {}
194  };
195 
196 /**
197 * Stream_IO_Error Exception
198 */
199 struct BOTAN_DLL Stream_IO_Error : public Exception
200  {
201  explicit Stream_IO_Error(const std::string& err) :
202  Exception("I/O error: " + err)
203  {}
204  };
205 
206 /**
207 * No_Filesystem_Access Exception
208 */
209 struct BOTAN_DLL No_Filesystem_Access : public Exception
210  {
211  No_Filesystem_Access() : Exception("No filesystem access enabled.") {}
212  };
213 
214 /**
215 * Self Test Failure Exception
216 */
217 struct BOTAN_DLL Self_Test_Failure : public Internal_Error
218  {
219  explicit Self_Test_Failure(const std::string& err) :
220  Internal_Error("Self test failed: " + err)
221  {}
222  };
223 
224 /**
225 * Not Implemented Exception
226 */
227 struct BOTAN_DLL Not_Implemented : public Exception
228  {
229  explicit Not_Implemented(const std::string& err) :
230  Exception("Not implemented", err)
231  {}
232  };
233 
234 }
235 
236 #endif
Invalid_IV_Length(const std::string &mode, size_t bad_len)
Definition: exceptn.h:104
std::string to_string(const secure_vector< byte > &bytes)
Definition: stl_util.h:25
Invalid_Key_Length(const std::string &name, size_t length)
Definition: exceptn.h:93
Exception(const std::string &msg)
Definition: exceptn.h:24
Invalid_Argument(const std::string &msg)
Definition: exceptn.h:37
Algorithm_Not_Found(const std::string &name)
Definition: exceptn.h:135
PRNG_Unseeded(const std::string &algo)
Definition: exceptn.h:115
Stream_IO_Error(const std::string &err)
Definition: exceptn.h:201
Definition: bigint.h:619
Exception(const char *prefix, const std::string &msg)
Definition: exceptn.h:25
No_Provider_Found(const std::string &name)
Definition: exceptn.h:145
Internal_Error(const std::string &err)
Definition: exceptn.h:83
Not_Implemented(const std::string &err)
Definition: exceptn.h:229
Integrity_Failure(const std::string &msg)
Definition: exceptn.h:183
Decoding_Error(const std::string &name)
Definition: exceptn.h:174
Unsupported_Argument(const std::string &msg)
Definition: exceptn.h:55
Invalid_Algorithm_Name(const std::string &name)
Definition: exceptn.h:155
Self_Test_Failure(const std::string &err)
Definition: exceptn.h:219
const char * what() const BOTAN_NOEXCEPT override
Definition: exceptn.h:26
Definition: alg_id.cpp:13
Invalid_State(const std::string &err)
Definition: exceptn.h:63
Invalid_Argument(const std::string &msg, const std::string &where)
Definition: exceptn.h:40
Lookup_Error(const std::string &err)
Definition: exceptn.h:73
Policy_Violation(const std::string &err)
Definition: exceptn.h:125
Encoding_Error(const std::string &name)
Definition: exceptn.h:165
Invalid_OID(const std::string &oid)
Definition: exceptn.h:192