LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
ErrorManager.h
Go to the documentation of this file.
1 
7 #ifndef LLU_ERRORLOG_ERRORMANAGER_H
8 #define LLU_ERRORLOG_ERRORMANAGER_H
9 
10 #include <algorithm>
11 #include <initializer_list>
12 #include <string>
13 #include <unordered_map>
14 #include <utility>
15 #include <vector>
16 
18 #include "LLU/LibraryData.h"
19 
20 namespace LLU {
21 
30  class ErrorManager {
31  public:
33  using ErrorStringData = std::pair<std::string, std::string>;
34 
35  public:
39  ErrorManager() = delete;
40 
45  static void registerPacletErrors(const std::vector<ErrorStringData>& errors);
46 
60  template<typename... T>
61  [[noreturn]] static void throwException(const std::string& errorName, T&&... args);
62 
75  template<typename... T>
76  [[noreturn]] static void throwException(WolframLibraryData libData, const std::string& errorName, T&&... args);
77 
88  template<class Error, typename... Args>
89  [[noreturn]] static void throwCustomException(const std::string& errorName, Args&&... args);
90 
107  template<typename... T>
108  [[noreturn]] static void throwExceptionWithDebugInfo(const std::string& errorName, const std::string& debugInfo, T&&... args);
109 
125  template<typename... T>
126  [[noreturn]] static void
127  throwExceptionWithDebugInfo(WolframLibraryData libData, const std::string& errorName, const std::string& debugInfo, T&&... args);
128 
135  static void setSendParametersImmediately(bool newValue) {
136  sendParametersImmediately = newValue;
137  }
138 
144  return sendParametersImmediately;
145  }
146 
154  static void sendRegisteredErrorsViaWSTP(WSLINK mlp);
155 
156  private:
158  using ErrorMap = std::unordered_map<std::string, const LibraryLinkError>;
159 
160  private:
165  static void set(const ErrorStringData& errorData);
166 
172  static const LibraryLinkError& findError(int errorId);
173 
179  static const LibraryLinkError& findError(const std::string& errorName);
180 
181  /***
182  * @brief Initialization of static error map
183  * @param initList - list of errors used internally by LLU
184  * @return reference to static error map
185  */
186  static ErrorMap registerLLUErrors(std::initializer_list<ErrorStringData> initList);
187 
189  static ErrorMap& errors();
190 
192  static int& nextErrorId();
193 
195  static bool sendParametersImmediately;
196  };
197 
198  template<typename... T>
199  [[noreturn]] void ErrorManager::throwException(const std::string& errorName, T&&... args) {
200  throwException(LibraryData::uncheckedAPI(), errorName, std::forward<T>(args)...);
201  }
202 
203  template<typename... T>
204  [[noreturn]] void ErrorManager::throwException(WolframLibraryData libData, const std::string& errorName, T&&... args) {
205  throwExceptionWithDebugInfo(libData, errorName, "", std::forward<T>(args)...);
206  }
207 
208  template<class Error, typename... Args>
209  [[noreturn]] void ErrorManager::throwCustomException(const std::string& errorName, Args&&... args) {
210  throw Error(findError(errorName), std::forward<Args>(args)...);
211  }
212 
213  template<typename... T>
214  [[noreturn]] void ErrorManager::throwExceptionWithDebugInfo(const std::string& errorName, const std::string& debugInfo, T&&... args) {
215  throwExceptionWithDebugInfo(LibraryData::uncheckedAPI(), errorName, debugInfo, std::forward<T>(args)...);
216  }
217 
218  template<typename... T>
219  [[noreturn]] void
220  ErrorManager::throwExceptionWithDebugInfo(WolframLibraryData libData, const std::string& errorName, const std::string& debugInfo, T&&... args) {
221  auto e = findError(errorName);
222  e.setDebugInfo(debugInfo);
223  if (libData && sizeof...(args) > 0) {
224  e.setMessageParameters(libData, std::forward<T>(args)...);
225  if (sendParametersImmediately) {
226  e.sendParameters(libData);
227  }
228  }
229  throw std::move(e);
230  }
231 
232 } /* namespace LLU */
233 
234 #endif // LLU_ERRORLOG_ERRORMANAGER_H
LLU::ErrorManager::throwExceptionWithDebugInfo
static void throwExceptionWithDebugInfo(const std::string &errorName, const std::string &debugInfo, T &&... args)
Throw exception with given name and additional information that might be helpful in debugging.
Definition: ErrorManager.h:214
LLU::ErrorManager::setSendParametersImmediately
static void setSendParametersImmediately(bool newValue)
Sets new value for the sendParametersImmediately flag.
Definition: ErrorManager.h:135
LLU::ErrorManager::getSendParametersImmediately
static bool getSendParametersImmediately()
Get the current value of sendParametersImmediately flag.
Definition: ErrorManager.h:143
LLU::ErrorManager::registerPacletErrors
static void registerPacletErrors(const std::vector< ErrorStringData > &errors)
Function used to register paclet-specific errors.
Definition: ErrorManager.cpp:146
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LibraryData.h
LLU::LibraryData::uncheckedAPI
static WolframLibraryData uncheckedAPI() noexcept
Get currently owned WolframLibraryData, even if it is a nullptr.
Definition: LibraryData.cpp:29
LLU::ErrorManager::sendRegisteredErrorsViaWSTP
static void sendRegisteredErrorsViaWSTP(WSLINK mlp)
Function used to send all registered errors to top-level Mathematica code.
Definition: ErrorManager.cpp:182
LLU::ErrorManager::ErrorManager
ErrorManager()=delete
Default constructor is deleted since ErrorManager is supposed to be completely static.
LLU::LibraryLinkError
Class representing an exception in paclet code.
Definition: LibraryLinkError.h:32
LibraryLinkError.h
Error class and error codes used by LibraryLink Utilities classes.
LLU::ErrorManager::throwException
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
LLU::ErrorManager::throwCustomException
static void throwCustomException(const std::string &errorName, Args &&... args)
Throw exception of given class that carries the error with given name.
Definition: ErrorManager.h:209
LLU::ErrorManager::ErrorStringData
std::pair< std::string, std::string > ErrorStringData
A type representing registered error in the form of 2 strings: short error name and longer error desc...
Definition: ErrorManager.h:33
LLU::ErrorManager
"Static" class responsible for error registration and throwing
Definition: ErrorManager.h:30