NRP Core  1.4.1
nrp_exceptions.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef NRP_EXCEPTIONS_H
23 #define NRP_EXCEPTIONS_H
24 
26 
27 #include <exception>
28 #include <iostream>
29 #include <string>
30 
32 
37  : public std::exception
38 {
45  template<class EXCEPTION>
46  static NRPException *nrpException(EXCEPTION &exception) noexcept
47  {
48  try
49  {
50  return dynamic_cast<NRPException*>(&exception);
51  }
52  catch(std::exception&)
53  {
54  return nullptr;
55  }
56  }
57 
58  public:
59  template<class EXCEPTION>
60  static void logOnce(EXCEPTION &exception, NRPLogger::spdlog_out_fcn_t spdlogCall = NRPLogger::critical)
61  {
62  NRPException *const logData = NRPException::nrpException(exception);
63  if(logData == nullptr)
64  std::invoke(spdlogCall, exception.what());
65  else if(logData->_msgLogged != true)
66  {
67  std::invoke(spdlogCall, exception.what());
68  logData->_msgLogged = true;
69  }
70  }
71 
72  template<class EXCEPTION = NRPExceptionNonRecoverable, class LOG_EXCEPTION_T>
73  static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall = NRPLogger::critical)
74  {
75  static_assert(std::is_nothrow_destructible_v<EXCEPTION> && std::is_constructible_v<EXCEPTION, const std::string&> ,"Parameter EXCEPTION must be constructible from std::string&");
76 
77  NRPException::logOnce(exception, spdlogCall);
78 
79  std::invoke(spdlogCall, msg);
80  if constexpr (std::is_nothrow_destructible_v<EXCEPTION> && std::is_constructible_v<EXCEPTION, const std::string&, bool>)
81  { return EXCEPTION(msg, true); }
82  else
83  { return EXCEPTION(msg); }
84 
85  }
86 
93  template<class EXCEPTION = NRPExceptionNonRecoverable>
94  static EXCEPTION logCreate(const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall = NRPLogger::critical)
95  {
96  static_assert((std::is_nothrow_destructible_v<EXCEPTION> && std::is_constructible_v<EXCEPTION, const std::string&>) || (std::is_same_v<EXCEPTION, void> && std::is_same_v<void, EXCEPTION>),"Parameter EXCEPTION must be constructible from std::string&");
97 
98  std::invoke(spdlogCall, msg);
99  if constexpr (std::is_nothrow_destructible_v<EXCEPTION> && std::is_constructible_v<EXCEPTION, const std::string&, bool>)
100  { return EXCEPTION(msg, true); }
101  else
102  { return EXCEPTION(msg); }
103  }
104 
105  template<class T>
106  explicit NRPException(T &&msg, bool msgLogged = false)
107  : _errMsg(std::forward<T>(msg)),
108  _msgLogged(msgLogged)
109  {}
110 
111  ~NRPException() override;
112 
113  const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override;
114 
115  protected:
116  std::string _errMsg = "";
117 
118  private:
119  bool _msgLogged = false;
120 };
121 
122 template<>
123 void NRPException::logCreate<void>(const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall);
124 
125 
130  : public NRPException
131 {
132  public:
133  template<class T>
134  explicit NRPExceptionNonRecoverable(T &&msg, bool msgLogged = false)
135  : NRPException(std::forward<T>(msg), msgLogged)
136  {}
137  ~NRPExceptionNonRecoverable() override;
138 };
139 
144  : public NRPException
145 {
146  public:
147  template<class T>
148  explicit NRPExceptionRecoverable(T &&msg, bool msgLogged = false)
149  : NRPException(std::forward<T>(msg), msgLogged)
150  {}
151  ~NRPExceptionRecoverable() override;
152 };
153 
154 
155 #endif // NRP_EXCEPTIONS_H
NRPException
Base NRPException class.
Definition: nrp_exceptions.h:36
nrp_logger.h
NRPException::what
const char * what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override
Definition: nrp_exceptions.cpp:28
NRPLogger::spdlog_out_fcn_t
void(&)(const std::string &) spdlog_out_fcn_t
Logging function type, is used by Exception.
Definition: nrp_logger.h:119
NRPExceptionNonRecoverable
Exception for non-recoverable errors.
Definition: nrp_exceptions.h:129
NRPLogger
NRP Logging functions.
Definition: nrp_logger.h:45
NRPException::NRPException
NRPException(T &&msg, bool msgLogged=false)
Definition: nrp_exceptions.h:106
NRPException::logCreate
static EXCEPTION logCreate(const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Logs the given message to the output, then returns EXCEPTION type.
Definition: nrp_exceptions.h:94
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
NRPExceptionRecoverable::NRPExceptionRecoverable
NRPExceptionRecoverable(T &&msg, bool msgLogged=false)
Definition: nrp_exceptions.h:148
NRPException::logOnce
static void logOnce(EXCEPTION &exception, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:60
NRPLogger::critical
static void critical(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for critical error level.
Definition: nrp_logger.h:171
NRPExceptionRecoverable
Exception for recoverable errors.
Definition: nrp_exceptions.h:143
NRPException::_errMsg
std::string _errMsg
Definition: nrp_exceptions.h:116
NRPExceptionNonRecoverable::NRPExceptionNonRecoverable
NRPExceptionNonRecoverable(T &&msg, bool msgLogged=false)
Definition: nrp_exceptions.h:134
NRPException::~NRPException
~NRPException() override