Botan 3.4.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
9#include <botan/internal/fmt.h>
10
11namespace Botan {
12
13std::string to_string(ErrorType type) {
14 switch(type) {
16 return "Unknown";
18 return "SystemError";
20 return "NotImplemented";
22 return "OutOfMemory";
24 return "InternalError";
26 return "IoError";
28 return "InvalidObjectState";
30 return "KeyNotSet";
32 return "InvalidArgument";
34 return "InvalidKeyLength";
36 return "InvalidNonceLength";
38 return "LookupError";
40 return "EncodingFailure";
42 return "DecodingFailure";
44 return "TLSError";
46 return "HttpError";
48 return "InvalidTag";
50 return "RoughtimeError";
52 return "CommonCryptoError";
54 return "Pkcs11Error";
56 return "TPMError";
58 return "DatabaseError";
60 return "ZlibError";
62 return "Bzip2Error";
64 return "LzmaError";
65 }
66
67 // No default case in above switch so compiler warns
68 return "Unrecognized Botan error";
69}
70
71Exception::Exception(std::string_view msg) : m_msg(msg) {}
72
73Exception::Exception(std::string_view msg, const std::exception& e) : m_msg(fmt("{} failed with {}", msg, e.what())) {}
74
75Exception::Exception(const char* prefix, std::string_view msg) : m_msg(fmt("{} {}", prefix, msg)) {}
76
77Invalid_Argument::Invalid_Argument(std::string_view msg) : Exception(msg) {}
78
79Invalid_Argument::Invalid_Argument(std::string_view msg, std::string_view where) :
80 Exception(fmt("{} in {}", msg, where)) {}
81
82Invalid_Argument::Invalid_Argument(std::string_view msg, const std::exception& e) : Exception(msg, e) {}
83
84namespace {
85
86std::string format_lookup_error(std::string_view type, std::string_view algo, std::string_view provider) {
87 if(provider.empty()) {
88 return fmt("Unavailable {} {}", type, algo);
89 } else {
90 return fmt("Unavailable {} {} for provider {}", type, algo, provider);
91 }
92}
93
94} // namespace
95
96Lookup_Error::Lookup_Error(std::string_view type, std::string_view algo, std::string_view provider) :
97 Exception(format_lookup_error(type, algo, provider)) {}
98
99Internal_Error::Internal_Error(std::string_view err) : Exception("Internal error:", err) {}
100
101Unknown_PK_Field_Name::Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name) :
102 Invalid_Argument(fmt("Unknown field '{}' for algorithm {}", field_name, algo_name)) {}
103
104Invalid_Key_Length::Invalid_Key_Length(std::string_view name, size_t length) :
105 Invalid_Argument(fmt("{} cannot accept a key of length {}", name, length)) {}
106
107Invalid_IV_Length::Invalid_IV_Length(std::string_view mode, size_t bad_len) :
108 Invalid_Argument(fmt("IV length {} is invalid for {}", bad_len, mode)) {}
109
110Key_Not_Set::Key_Not_Set(std::string_view algo) : Invalid_State(fmt("Key not set in {}", algo)) {}
111
112PRNG_Unseeded::PRNG_Unseeded(std::string_view algo) : Invalid_State(fmt("PRNG {} not seeded", algo)) {}
113
115 Lookup_Error(fmt("Could not find any algorithm named '{}'", name)) {}
116
117Provider_Not_Found::Provider_Not_Found(std::string_view algo, std::string_view provider) :
118 Lookup_Error(fmt("Could not find provider '{}' for algorithm '{}'", provider, algo)) {}
119
121 Invalid_Argument(fmt("Invalid algorithm name: '{}'", name)) {}
122
123Encoding_Error::Encoding_Error(std::string_view name) : Exception("Encoding error:", name) {}
124
126
127Decoding_Error::Decoding_Error(std::string_view category, std::string_view err) :
128 Exception(fmt("{}: {}", category, err)) {}
129
130Decoding_Error::Decoding_Error(std::string_view msg, const std::exception& e) : Exception(msg, e) {}
131
133 Exception("Invalid authentication tag:", msg) {}
134
135Stream_IO_Error::Stream_IO_Error(std::string_view err) : Exception("I/O error:", err) {}
136
137System_Error::System_Error(std::string_view msg, int err_code) :
138 Exception(fmt("{} error code {}", msg, err_code)), m_error_code(err_code) {}
139
140Not_Implemented::Not_Implemented(std::string_view err) : Exception("Not implemented", err) {}
141
142} // namespace Botan
Algorithm_Not_Found(std::string_view name)
Definition exceptn.cpp:114
Decoding_Error(std::string_view name)
Definition exceptn.cpp:125
Encoding_Error(std::string_view name)
Definition exceptn.cpp:123
Exception(std::string_view msg)
Definition exceptn.cpp:71
Internal_Error(std::string_view err)
Definition exceptn.cpp:99
Invalid_Algorithm_Name(std::string_view name)
Definition exceptn.cpp:120
Invalid_Argument(std::string_view msg)
Definition exceptn.cpp:77
Invalid_Authentication_Tag(std::string_view msg)
Definition exceptn.cpp:132
Invalid_IV_Length(std::string_view mode, size_t bad_len)
Definition exceptn.cpp:107
Invalid_Key_Length(std::string_view name, size_t length)
Definition exceptn.cpp:104
Key_Not_Set(std::string_view algo)
Definition exceptn.cpp:110
Lookup_Error(std::string_view err)
Definition exceptn.h:237
Not_Implemented(std::string_view err)
Definition exceptn.cpp:140
PRNG_Unseeded(std::string_view algo)
Definition exceptn.cpp:112
Provider_Not_Found(std::string_view algo, std::string_view provider)
Definition exceptn.cpp:117
Stream_IO_Error(std::string_view err)
Definition exceptn.cpp:135
System_Error(std::string_view msg)
Definition exceptn.h:306
Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name)
Definition exceptn.cpp:101
std::string name
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ErrorType
Definition exceptn.h:20
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13