LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
LibraryLinkError.h
Go to the documentation of this file.
1 
8 #ifndef LLU_ERRORLOG_LIBRARYLINKERROR_H_
9 #define LLU_ERRORLOG_LIBRARYLINKERROR_H_
10 
11 #include <stdexcept>
12 #include <string>
13 
14 #include "LLU/WSTP/WSStream.hpp"
15 
23 namespace LLU {
24 
32  class LibraryLinkError : public std::runtime_error {
33  friend class ErrorManager;
34 
35  public:
37  using IdType = int;
38 
40  LibraryLinkError(const LibraryLinkError& e) noexcept;
41 
43  LibraryLinkError& operator=(const LibraryLinkError& e) noexcept;
44 
46  LibraryLinkError(LibraryLinkError&& e) noexcept;
47 
50 
52  ~LibraryLinkError() override;
53 
58  void setDebugInfo(const std::string& dbg) {
59  debugInfo = std::runtime_error {dbg};
60  }
61 
65  IdType id() const noexcept {
66  return errorId;
67  }
68 
72  IdType which() const noexcept {
73  return errorId;
74  }
75 
79  [[nodiscard]] std::string name() const noexcept {
80  return what();
81  }
82 
86  [[nodiscard]] std::string message() const noexcept {
87  return messageTemplate.what();
88  }
89 
93  [[nodiscard]] std::string debug() const noexcept {
94  return debugInfo.what();
95  }
96 
104  template<typename... T>
105  void setMessageParameters(WolframLibraryData libData, T&&... params);
106 
114  IdType sendParameters(WolframLibraryData libData, const std::string& WLSymbol = getExceptionDetailsSymbol()) const noexcept;
115 
120  static std::string getExceptionDetailsSymbol();
121 
126  static void setExceptionDetailsSymbolContext(std::string newContext);
127 
132  static const std::string& getExceptionDetailsSymbolContext();
133 
134  private:
142  LibraryLinkError(IdType which, const std::string& t, const std::string& msg)
143  : std::runtime_error(t), errorId(which), messageTemplate(msg.c_str()) {}
144 
150  static WSLINK openLoopback(WSENV env);
151 
153  static constexpr const char* exceptionDetailsSymbol = "$LastFailureParameters";
154 
156  static std::string exceptionDetailsSymbolContext;
157 
158  IdType errorId;
159  std::runtime_error messageTemplate;
160  std::runtime_error debugInfo {""};
161  WSLINK messageParams = nullptr;
162  };
163 
164  template<typename... T>
165  void LibraryLinkError::setMessageParameters(WolframLibraryData libData, T&&... params) {
166  messageParams = openLoopback(libData->getWSLINKEnvironment(libData));
167  if (!messageParams) {
168  return;
169  }
170  WSStream<WS::Encoding::UTF8> loopback {messageParams};
171  constexpr auto messageParamsCount = sizeof...(T);
172  loopback << WS::List(static_cast<int>(messageParamsCount));
173  Unused((loopback << ... << params));
174  }
175 } /* namespace LLU */
176 
177 #endif /* LLU_ERRORLOG_LIBRARYLINKERROR_H_ */
LLU::LibraryLinkError::setExceptionDetailsSymbolContext
static void setExceptionDetailsSymbolContext(std::string newContext)
Set custom context for the Wolfram Language symbol that will hold the details of last thrown exceptio...
Definition: LibraryLinkError.cpp:18
LLU::LibraryLinkError::setDebugInfo
void setDebugInfo(const std::string &dbg)
Set debug info.
Definition: LibraryLinkError.h:58
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::WSStream
Wrapper class over WSTP with a stream-like interface.
Definition: WSTP/Utilities.h:21
LLU::LibraryLinkError::operator=
LibraryLinkError & operator=(const LibraryLinkError &e) noexcept
Copy-assignment operator.
Definition: LibraryLinkError.cpp:53
LLU::LibraryLinkError::sendParameters
IdType sendParameters(WolframLibraryData libData, const std::string &WLSymbol=getExceptionDetailsSymbol()) const noexcept
Send parameters stored in the loopback link to top-level.
Definition: LibraryLinkError.cpp:84
LLU::LibraryLinkError::which
IdType which() const noexcept
Alias for id() to preserve backwards compatibility.
Definition: LibraryLinkError.h:72
LLU::LibraryLinkError::message
std::string message() const noexcept
Get the value of error code.
Definition: LibraryLinkError.h:86
WSStream.hpp
Header file for WSStream class.
LLU::LibraryLinkError
Class representing an exception in paclet code.
Definition: LibraryLinkError.h:32
LLU::LibraryLinkError::getExceptionDetailsSymbolContext
static const std::string & getExceptionDetailsSymbolContext()
Get current context of the symbol that will hold the details of last thrown exception.
Definition: LibraryLinkError.cpp:22
LLU::Unused
void Unused(Ts &&...)
Dummy function called on otherwise unused parameters to eliminate compiler warnings.
Definition: Utilities.hpp:115
LLU::LibraryLinkError::~LibraryLinkError
~LibraryLinkError() override
The destructor closes the link that was used to send message parameters, if any.
Definition: LibraryLinkError.cpp:78
LLU::LibraryLinkError::name
std::string name() const noexcept
Get the value of error code.
Definition: LibraryLinkError.h:79
LLU::LibraryLinkError::debug
std::string debug() const noexcept
Get debug info.
Definition: LibraryLinkError.h:93
LLU::LibraryLinkError::getExceptionDetailsSymbol
static std::string getExceptionDetailsSymbol()
Get symbol that will hold details of last thrown exception.
Definition: LibraryLinkError.cpp:26
LLU::LibraryLinkError::LibraryLinkError
LibraryLinkError(const LibraryLinkError &e) noexcept
Copy-constructor. If there are any messages parameters on the WSLINK, a deep copy is performed.
Definition: LibraryLinkError.cpp:39
LLU::LibraryLinkError::id
IdType id() const noexcept
Get the value of error code.
Definition: LibraryLinkError.h:65
LLU::LibraryLinkError::setMessageParameters
void setMessageParameters(WolframLibraryData libData, T &&... params)
Store arbitrary number of message parameters in a List expression on a loopback link.
Definition: LibraryLinkError.h:165
LLU::LibraryLinkError::IdType
int IdType
A type that holds error id numbers.
Definition: LibraryLinkError.h:37
LLU::ErrorManager
"Static" class responsible for error registration and throwing
Definition: ErrorManager.h:30