Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_CORE_EXCEPTION_HPP_
2#define IV_SRC_CORE_EXCEPTION_HPP_
3
4#include <cstdint>
5#include <fmt/core.h>
6#include <optional>
7#include <string_view>
8
9namespace iv::exception
10{
11
79
80/***
81 * @brief Convert an eExceptionType to string
82 * @param type
83 * @return
84 */
85std::string toString(eExceptionType type);
86
87class Exception : public std::exception
88{
89public:
90 explicit Exception(const std::string_view msg) : m_msg(msg)
91 {
92 }
93
94 Exception(const char *prefix, std::string_view msg) : m_msg(fmt::format("{} {}", prefix, msg))
95 {
96 }
97
98 Exception(std::string_view msg, const std::exception &e) : m_msg(fmt::format("{} failed with {}", msg, e.what()))
99 {
100 }
101
102 ~Exception() noexcept override = default;
103
104 [[nodiscard]] virtual int64_t errorCode() const noexcept
105 {
106 return 0;
107 }
108
109 [[nodiscard]] virtual eExceptionType exceptionType() const noexcept
110 {
112 }
113
114 [[nodiscard]] const char *what() const noexcept override
115 {
116 return m_msg.c_str();
117 }
118
119protected:
120 std::string m_msg;
121};
122
127{
128public:
129 explicit InvalidArgument(const std::string_view msg) : iv::exception::Exception(msg)
130 {
131 }
132
133 explicit InvalidArgument(std::string_view msg, std::string_view where)
134 : iv::exception::Exception(fmt::format("{} in {}", msg, where))
135 {
136 }
137
138 InvalidArgument(const std::string_view msg, const std::exception &e) : iv::exception::Exception(msg, e)
139 {
140 }
141
142 [[nodiscard]] eExceptionType exceptionType() const noexcept override
143 {
145 }
146};
147
152{
153public:
154 UnknownPKFieldName(std::string_view algo_name, std::string_view field_name)
155 : InvalidArgument(fmt::format("Unknown field '{}' for algorithm {}", field_name, algo_name))
156 {
157 }
158};
159
164{
165public:
166 InvalidKeyLength(std::string_view name, size_t length)
167 : InvalidArgument(fmt::format("{} cannot accept a key of length {}", name, length))
168 {
169 }
170
171 [[nodiscard]] eExceptionType exceptionType() const noexcept override
172 {
174 }
175};
176
181{
182public:
183 InvalidIVLength(std::string_view mode, size_t badLength)
184 : InvalidArgument(fmt::format("IV length {} is invalid for {}", badLength, mode))
185 {
186 }
187
188 [[nodiscard]] eExceptionType exceptionType() const noexcept override
189 {
191 }
192};
193
198{
199public:
200 explicit InvalidAlgorithmName(std::string_view name)
201 : InvalidArgument(fmt::format("Invalid algorithm name: '{}'", name))
202 {
203 }
204};
205
210{
211public:
212 explicit EncodingError(const std::string_view name) : Exception("Encoding error:", name)
213 {
214 }
215
216 [[nodiscard]] eExceptionType exceptionType() const noexcept override
217 {
219 }
220};
221
226{
227public:
228 explicit DecodingError(const std::string_view name) : Exception(name)
229 {
230 }
231
232 DecodingError(std::string_view category, std::string_view err) : Exception(fmt::format("{}: {}", category, err))
233 {
234 }
235
236 DecodingError(const std::string_view msg, const std::exception &e) : Exception(msg, e)
237 {
238 }
239
240 [[nodiscard]] eExceptionType exceptionType() const noexcept override
241 {
243 }
244};
245
251{
252public:
253 explicit InvalidState(const std::string_view err) : Exception(err)
254 {
255 }
256
257 [[nodiscard]] eExceptionType exceptionType() const noexcept override
258 {
260 }
261};
262
266class PRNGUnseeded final : public InvalidState
267{
268public:
269 explicit PRNGUnseeded(std::string_view algo) : InvalidState(fmt::format("PRNG {} not seeded", algo))
270 {
271 }
272};
273
278class KeyNotSet final : public InvalidState
279{
280public:
281 explicit KeyNotSet(std::string_view algo) : InvalidState(fmt::format("Key not set in {}", algo))
282 {
283 }
284
285 [[nodiscard]] eExceptionType exceptionType() const noexcept override
286 {
288 }
289};
290
295{
296public:
297 explicit LookupError(const std::string_view err) : Exception(err)
298 {
299 }
300
301 LookupError(std::string_view type, std::string_view algo, std::string_view provider = "");
302
303 [[nodiscard]] eExceptionType exceptionType() const noexcept override
304 {
306 }
307};
308
315class AlgorithmNotFound final : public LookupError
316{
317public:
318 explicit AlgorithmNotFound(std::string_view name)
319 : LookupError(fmt::format("Could not find any algorithm named '{}'", name))
320 {
321 }
322};
323
331class ProviderNotFound final : public LookupError
332{
333public:
334 ProviderNotFound(std::string_view algo, std::string_view provider)
335 : LookupError(fmt::format("Could not find provider '{}' for algorithm '{}'", provider, algo))
336 {
337 }
338};
339
347{
348public:
349 explicit InvalidAuthenticationTag(const std::string_view msg) : Exception("Invalid authentication tag:", msg)
350 {
351 }
352
353 [[nodiscard]] eExceptionType exceptionType() const noexcept override
354 {
356 }
357};
358
363{
364public:
365 explicit StreamIOError(const std::string_view err) : Exception("I/O error:", err)
366 {
367 }
368
369 [[nodiscard]] eExceptionType exceptionType() const noexcept override
370 {
372 }
373};
374
376{
377public:
378 explicit InvalidFileIntegrity(const std::string_view err) : Exception("Invalid file integrity: ", err)
379 {
380 }
381
382 [[nodiscard]] eExceptionType exceptionType() const noexcept override
383 {
385 }
386};
387
398{
399public:
400 explicit SystemError(const std::string_view msg) : Exception(msg), m_errorCode(0)
401 {
402 }
403
404 SystemError(std::string_view msg, int errCode)
405 : Exception(fmt::format("{} error code {}", msg, errCode)), m_errorCode(errCode)
406 {
407 }
408
409 [[nodiscard]] eExceptionType exceptionType() const noexcept override
410 {
412 }
413
414 [[nodiscard]] int64_t errorCode() const noexcept override
415 {
416 return m_errorCode;
417 }
418
419private:
420 int64_t m_errorCode;
421};
422
427{
428public:
429 explicit InternalError(const std::string_view err) : Exception("Internal error:", err)
430 {
431 }
432
433 [[nodiscard]] eExceptionType exceptionType() const noexcept override
434 {
436 }
437};
438
446{
447public:
448 NotImplemented(const std::string_view err, std::optional<std::string_view> file, std::optional<uint32_t> line)
449 : Exception(fmt::format("Not implemented: {}", err))
450 {
451 if (file.has_value() && line.has_value())
452 {
453 m_msg += fmt::format(" at {}:{}", file.value(), line.value());
454 }
455 }
456
457 [[nodiscard]] eExceptionType exceptionType() const noexcept override
458 {
460 }
461};
462
464{
465public:
466 IntegerOverflowDetected(std::string_view file, int line)
467 : Exception(fmt::format("Integer overflow detected at {}:{}", file, line))
468 {
469 }
470
471 [[nodiscard]] eExceptionType exceptionType() const noexcept override
472 {
474 }
475};
476
478{
479public:
480 explicit FolderNotFound(std::string_view folder) : Exception(fmt::format("Folder {} not found", folder))
481 {
482 }
483
484 FolderNotFound() : Exception("Folder not found")
485 {
486 }
487
488 [[nodiscard]] eExceptionType exceptionType() const noexcept override
489 {
491 }
492};
493
495{
496public:
497 explicit FileNotFound(std::string_view file) : Exception(fmt::format("File {} not found", file))
498 {
499 }
500
501 [[nodiscard]] eExceptionType exceptionType() const noexcept override
502 {
504 }
505};
506
508{
509public:
510 explicit FileNameNotAvailable(std::string_view file) : Exception(fmt::format("File name not available: {}", file))
511 {
512 }
513
514 [[nodiscard]] eExceptionType exceptionType() const noexcept override
515 {
517 }
518};
519
521{
522public:
523 explicit ImpossibleLabelingError(const std::string &msg) : Exception(msg)
524 {
525 }
526};
527
528template<typename E, typename... Args>
529inline void doThrowError(const char *file, int line, const char *func, Args... args)
530{
531 throw E(file, line, func, args...);
532}
533
538{
539public:
540 explicit MissingCase(const std::string_view msg) : Exception(msg)
541 {
542 //TODO: ¿Mensaje? //("Missing case for value " + to_string(value) + " in function " + func + " at file " + file);
543 }
544
545 explicit MissingCase(std::string_view msg, std::string_view where) : Exception(fmt::format("{} in {}", msg, where))
546 {
547 }
548
549 MissingCase(const std::string_view msg, const std::exception &e) : Exception(msg, e)
550 {
551 }
552
553 [[nodiscard]] eExceptionType exceptionType() const noexcept override
554 {
556 }
557};
558
560{
561public:
562 explicit LibZipError(std::string_view msg) : Exception(fmt::format("LibZip error: {}", msg))
563 {
564 }
565
566 [[nodiscard]] eExceptionType exceptionType() const noexcept override
567 {
569 }
570};
571
573{
574public:
575 explicit OutOfBoundsMemoryAccess(std::string_view msg)
576 : Exception(fmt::format("Out of bounds memory access: {}", msg))
577 {
578 }
579
580 [[nodiscard]] eExceptionType exceptionType() const noexcept override
581 {
583 }
584};
585}// namespace iv::exception
586
587#endif//IV_SRC_CORE_EXCEPTION_HPP_
Definition exception.hpp:316
AlgorithmNotFound(std::string_view name)
Definition exception.hpp:318
Definition exception.hpp:226
DecodingError(std::string_view category, std::string_view err)
Definition exception.hpp:232
DecodingError(const std::string_view msg, const std::exception &e)
Definition exception.hpp:236
DecodingError(const std::string_view name)
Definition exception.hpp:228
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:240
Definition exception.hpp:210
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:216
EncodingError(const std::string_view name)
Definition exception.hpp:212
Definition exception.hpp:88
virtual eExceptionType exceptionType() const noexcept
Definition exception.hpp:109
virtual int64_t errorCode() const noexcept
Definition exception.hpp:104
std::string m_msg
Definition exception.hpp:120
Exception(std::string_view msg, const std::exception &e)
Definition exception.hpp:98
Exception(const std::string_view msg)
Definition exception.hpp:90
Exception(const char *prefix, std::string_view msg)
Definition exception.hpp:94
const char * what() const noexcept override
Definition exception.hpp:114
~Exception() noexcept override=default
Definition exception.hpp:508
FileNameNotAvailable(std::string_view file)
Definition exception.hpp:510
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:514
Definition exception.hpp:495
FileNotFound(std::string_view file)
Definition exception.hpp:497
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:501
Definition exception.hpp:478
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:488
FolderNotFound()
Definition exception.hpp:484
FolderNotFound(std::string_view folder)
Definition exception.hpp:480
Definition exception.hpp:521
ImpossibleLabelingError(const std::string &msg)
Definition exception.hpp:523
Definition exception.hpp:464
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:471
IntegerOverflowDetected(std::string_view file, int line)
Definition exception.hpp:466
Definition exception.hpp:427
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:433
InternalError(const std::string_view err)
Definition exception.hpp:429
Definition exception.hpp:198
InvalidAlgorithmName(std::string_view name)
Definition exception.hpp:200
Definition exception.hpp:127
InvalidArgument(const std::string_view msg, const std::exception &e)
Definition exception.hpp:138
InvalidArgument(std::string_view msg, std::string_view where)
Definition exception.hpp:133
InvalidArgument(const std::string_view msg)
Definition exception.hpp:129
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:142
Definition exception.hpp:347
InvalidAuthenticationTag(const std::string_view msg)
Definition exception.hpp:349
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:353
Definition exception.hpp:376
InvalidFileIntegrity(const std::string_view err)
Definition exception.hpp:378
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:382
Definition exception.hpp:181
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:188
InvalidIVLength(std::string_view mode, size_t badLength)
Definition exception.hpp:183
Definition exception.hpp:164
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:171
InvalidKeyLength(std::string_view name, size_t length)
Definition exception.hpp:166
Definition exception.hpp:251
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:257
InvalidState(const std::string_view err)
Definition exception.hpp:253
Definition exception.hpp:279
KeyNotSet(std::string_view algo)
Definition exception.hpp:281
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:285
Definition exception.hpp:560
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:566
LibZipError(std::string_view msg)
Definition exception.hpp:562
Definition exception.hpp:295
LookupError(const std::string_view err)
Definition exception.hpp:297
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:303
Definition exception.hpp:538
MissingCase(std::string_view msg, std::string_view where)
Definition exception.hpp:545
MissingCase(const std::string_view msg)
Definition exception.hpp:540
MissingCase(const std::string_view msg, const std::exception &e)
Definition exception.hpp:549
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:553
Definition exception.hpp:446
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:457
NotImplemented(const std::string_view err, std::optional< std::string_view > file, std::optional< uint32_t > line)
Definition exception.hpp:448
Definition exception.hpp:573
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:580
OutOfBoundsMemoryAccess(std::string_view msg)
Definition exception.hpp:575
Definition exception.hpp:267
PRNGUnseeded(std::string_view algo)
Definition exception.hpp:269
Definition exception.hpp:332
ProviderNotFound(std::string_view algo, std::string_view provider)
Definition exception.hpp:334
Definition exception.hpp:363
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:369
StreamIOError(const std::string_view err)
Definition exception.hpp:365
Definition exception.hpp:398
eExceptionType exceptionType() const noexcept override
Definition exception.hpp:409
SystemError(std::string_view msg, int errCode)
Definition exception.hpp:404
int64_t m_errorCode
Definition exception.hpp:420
int64_t errorCode() const noexcept override
Definition exception.hpp:414
SystemError(const std::string_view msg)
Definition exception.hpp:400
Definition exception.hpp:152
UnknownPKFieldName(std::string_view algo_name, std::string_view field_name)
Definition exception.hpp:154
Definition exceptions.hpp:7
void doThrowError(const char *file, int line, const char *func, Args... args)
Definition exception.hpp:529
std::string toString(const iv::exception::eExceptionType type)
Definition exception.cpp:6
eExceptionType
Definition exception.hpp:13
Definition AlarmsManager.cpp:18