jaffarCommon
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "string.hpp"
9#include <stdarg.h>
10#include <stdexcept>
11#include <stdio.h>
12
13namespace jaffarCommon
14{
15
16namespace exceptions
17{
18
22#define JAFFAR_THROW_RUNTIME(...) jaffarCommon::exceptions::throwException("Runtime", __FILE__, __LINE__, __VA_ARGS__)
23
27#define JAFFAR_THROW_LOGIC(...) jaffarCommon::exceptions::throwException("Logic", __FILE__, __LINE__, __VA_ARGS__)
28
38__JAFFAR_COMMON__INLINE__ void throwException [[noreturn]] (const char* exceptionType, const char* fileName, const int lineNumber, const char* format, ...)
39{
40 char* outstr = 0;
41 va_list ap;
42 va_start(ap, format);
43 int ret = vsnprintf(nullptr, 0, format, ap);
44 va_end(ap);
45 if (ret < 0) throw std::invalid_argument("Failed processing exception reason");
46
47 outstr = (char*)malloc(ret + 1);
48 if (outstr == nullptr) throw std::invalid_argument("Failed processing exception reason");
49
50 va_start(ap, format);
51 ret = vsnprintf(outstr, ret + 1, format, ap);
52 va_end(ap);
53 if (ret < 0)
54 {
55 free(outstr);
56 throw std::invalid_argument("Failed processing exception reason");
57 }
58
59 std::string outString = outstr;
60 free(outstr);
61
62 char info[1024];
63
64 snprintf(info, sizeof(info) - 1, " + From %s:%d\n", fileName, lineNumber);
65 outString += info;
66
67 if (std::string(exceptionType) == "Logic") throw std::logic_error(outString.c_str());
68 if (std::string(exceptionType) == "Runtime") throw std::runtime_error(outString.c_str());
69 throw std::invalid_argument("Wrong exception type provided: " + std::string(exceptionType) + std::string(" Original error: ") + outString);
70}
71
72} // namespace exceptions
73
74} // namespace jaffarCommon
__JAFFAR_COMMON__INLINE__ void throwException(const char *exceptionType, const char *fileName, const int lineNumber, const char *format,...)
Definition exceptions.hpp:38
Contains common functions related to manipulating strings.