Tinker9 70bd052 (Thu Nov 9 12:11:35 2023 -0800)
Loading...
Searching...
No Matches
error.h
1#pragma once
2#include "tool/ioprint.h"
3#include "tool/iotext.h"
4#include <stdexcept>
5
6namespace tinker {
9
10template <class T>
11std::string translateErrorCode(T errcode);
12void printError();
13void printBacktrace(std::FILE* out = stderr);
14
16class FatalError : public std::exception
17{
18private:
19 std::string m_msg;
20
21public:
22 FatalError(const char* msg)
23 : m_msg(msg)
24 {}
25 FatalError(const std::string& msg)
26 : m_msg(msg)
27 {}
29 : m_msg(e.m_msg)
30 {}
31 const char* what() const noexcept override
32 {
33 return m_msg.c_str();
34 }
35};
36
38#define TINKER_THROW(msg) \
39 do { \
40 printBacktrace(); \
41 std::string ms_ = msg; \
42 std::string m_ = format("%s at %s:%d", ms_, __FILE__, __LINE__); \
43 throw FatalError(m_); \
44 } while (0)
45
46#define TINKER_ALWAYS_CHECK_RT_1_(call) \
47 do { \
48 auto res_ = call; \
49 if (res_ != 0) { \
50 printBacktrace(); \
51 std::string m_; \
52 std::string msg_ = translateErrorCode(res_); \
53 if (msg_ != "") \
54 m_ = format("Errno %d (%s) at %s:%d", res_, msg_, __FILE__, __LINE__); \
55 else \
56 m_ = format("Errno %d at %s:%d", res_, __FILE__, __LINE__); \
57 throw FatalError(m_); \
58 } \
59 } while (0)
60#define TINKER_ALWAYS_CHECK_RT_(...) TINKER_GET_2ND_ARG(__VA_ARGS__, TINKER_ALWAYS_CHECK_RT_1_)
61
64#ifndef TINKER_ALWAYS_CHECK_RT
65#define TINKER_ALWAYS_CHECK_RT 0
66#endif
67
73#if TINKER_DEBUG || TINKER_ALWAYS_CHECK_RT
74#define check_rt(...) TINKER_ALWAYS_CHECK_RT_(__VA_ARGS__)(__VA_ARGS__)
75#else
76#define check_rt(call, ...) call
77#endif
78
81#define always_check_rt(...) TINKER_ALWAYS_CHECK_RT_(__VA_ARGS__)(__VA_ARGS__)
82
84}
FatalError(const FatalError &e)
Definition: error.h:28
FatalError(const std::string &msg)
Definition: error.h:25
const char * what() const noexcept override
Definition: error.h:31
FatalError(const char *msg)
Definition: error.h:22
Errors and exceptions that we do not intend to fix or handle.
Definition: error.h:17
void printError()
Writes the current coordinates to a disk file prior to aborting on a serious error.
std::string translateErrorCode(T errcode)
Translates the error code to text.
void printBacktrace(std::FILE *out=stderr)
Prints the call stack to a FILE pointer.
Definition: testrt.h:9