Virtru SDK for C++  2.8.0
Virtru C++ SDK library - Create, Read, and Manage TDF3 Files
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tdf_exception.h
Go to the documentation of this file.
1 /*
2 * Copyright 2019 Virtru Corporation
3 *
4 * SPDX - License Identifier: BSD-3-Clause-Clear
5 *
6 */
7 //
8 // TDF SDK
9 //
10 // Created by Sujan Reddy on 2019/03/02.
11 //
12 
13 #ifndef PROJECT_EXCEPTION_H
14 #define PROJECT_EXCEPTION_H
15 
16 #include <stdexcept>
17 #include <string>
18 #include <sstream>
19 #include <map>
20 #include "tdf_error_codes.h"
21 #include "logger.h"
22 
23 namespace virtru {
24 
25  using namespace std::string_literals;
26 
28  #define EXPAND( x ) x
29  #define GET_MACRO(_1,_2,NAME,...) NAME
30  #define ThrowException(...) EXPAND(EXPAND(GET_MACRO(__VA_ARGS__, ThrowExceptionCode, ThrowExceptionNoCode))(__VA_ARGS__))
31  #define ThrowExceptionNoCode(message) virtru::_ThrowVirtruException(message, __SOURCE_FILENAME__, __LINE__)
32  #define ThrowExceptionCode(message, code) virtru::_ThrowVirtruException(message, __SOURCE_FILENAME__, __LINE__, code)
33 
34 
35  class Exception : public std::runtime_error {
36  public:
37  explicit Exception(const std::string &what, int code = VIRTRU_GENERAL_ERROR) :
38  std::runtime_error{"[Error code: "s + std::to_string(code) + "] " + what},
39  m_code{code} {}
40 
41  int code() const noexcept {
42  return m_code;
43  }
44 
45  private:
46  int m_code;
47  };
48 
54  inline void _ThrowVirtruException(std::string &&errorStringPrefix, const char *fileName, unsigned int lineNumber, int code = VIRTRU_GENERAL_ERROR) {
55  std::ostringstream os;
56  os << " [" << fileName << ":" << lineNumber << "] ";
57 
58  //only log line number for DEBUG and TRACE
59  if(IsLogLevelDebug() || IsLogLevelTrace()){
60  throw Exception { os.str() + move (errorStringPrefix), code};
61  }
62  else{
63  throw Exception { move (errorStringPrefix), code};
64  }
65 
66 
67  }
68 } // namespace virtru
69 
70 #endif //PROJECT_EXCEPTION_H
71 
72 
int code() const noexcept
Definition: tdf_exception.h:41
Definition: tdf_exception.h:35
int m_code
Definition: tdf_exception.h:46
Exception(const std::string &what, int code=VIRTRU_GENERAL_ERROR)
Definition: tdf_exception.h:37
void _ThrowVirtruException(std::string &&errorStringPrefix, const char *fileName, unsigned int lineNumber, int code=VIRTRU_GENERAL_ERROR)
Definition: tdf_exception.h:54