AFLOW
 
Loading...
Searching...
No Matches
aurostd_xerror.cpp
Go to the documentation of this file.
1//****************************************************************************
2// * *
3// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
4// * Aflow MARCO ESTERS - Duke University 2018-2021 *
5// * *
6//****************************************************************************
7// Class to handle AFLOW exceptions.
8//
9// Since AFLOW should return 0 when it terminates normally, 0 cannot be used
10// as an error code. See README_AFLOW_EXCEPTIONS.TXT for details and examples.
11// If exceptions are added, please update the README as well!
12
13#ifndef _AUROSTD_XERROR_CPP_
14#define _AUROSTD_XERROR_CPP_
15
16#include "aurostd_xerror.h"
17
18#include <ostream>
19#include <sstream>
20#include <string>
21
22#include "aurostd.h"
23
24namespace aurostd {
25 // List of error types and errors ////////////////////////////////////////////
26#define _AFLOW_NUM_ERR_TYPES_ 7 // Number of error types
27
28 string xerror_PID; // SC20200508
29
30 static const std::string error_types[_AFLOW_NUM_ERR_TYPES_] = {"", "Input Error", "File Error", "Value Error", "Index Error", "Runtime Error", "Allocation Error"};
31
32 // Errors associated with those types
33 static const std::string errors[_AFLOW_NUM_ERR_TYPES_][9] = {
34 { "Generic error", "Illegal error code", "", "", "", "", "", "", ""},
35 { "unknown flag", "missing flag", "input ambiguous", "illegal parameter", "number of parameters", "", "", "", ""},
36 { "file not found", "wrong format", "file corrupt", "", "", "", "", "", ""},
37 { "illegal value", "out of range", "", "", "", "", "", "", ""},
38 { "illegal value", "out of bounds", "mismatch", "", "", "", "", "", ""},
39 { "not initialized", "SQL error", "busy", "external command not found", "external command failed", "HTTP error", "", "", ""}, // CO20200531
40 {"could not allocate", "insufficient memory", "", "", "", "", "", "", ""}
41 };
42
43 // Constructors////////////////////////////////////////////////////////////////
44 xerror::xerror(const std::string& filename, const std::string& function, const std::string& msg, int code) {
45 buildException(filename, function, msg, code);
46 }
47
48 xerror::xerror(const std::string& filename, const std::string& function, const std::stringstream& msg, int code) {
49 buildException(filename, function, msg.str(), code);
50 }
51
52 xerror::xerror(const std::string& filename, const std::stringstream& function, const std::string& msg, int code) {
53 buildException(filename, function.str(), msg, code);
54 }
55
56 xerror::xerror(const std::string& filename, const std::stringstream& function, const std::stringstream& msg, int code) {
57 buildException(filename, function.str(), msg.str(), code);
58 }
59
60 // buildExeption///////////////////////////////////////////////////////////////
61 // builds the xerror object.
62 void xerror::buildException(const std::string& filename, const std::string& fname, const std::string& msg, const int& code) {
63 file_name = filename;
64 function_name = fname;
65 error_message = msg;
66 error_code = code;
69 if (!codeValid()) {
70 error_code = 2;
71 error_type = 0;
72 error_number = 2;
73 }
74 }
75
76 // codeValid///////////////////////////////////////////////////////////////////
77 // Checks if a valid error code was provided
78 bool xerror::codeValid() const {
79 if (error_code < 0) {
80 return false;
81 }
83 return false;
84 }
85 if (error_type == 0 && error_number == 0) {
86 return false; // "Error code" 0 is reserved
87 }
88 if (error_number > 0) { // an error value of 0 is the generic error, so it's always valid
89 if (errors[error_type][error_number - 1].empty()) {
90 return false;
91 }
92 }
93 return true;
94 }
95
96 /******************* Functions to build the message string *******************/
97 // buildMessageString/////////////////////////////////////////////////////////
99 std::stringstream msgstr;
100 msgstr << xerror_PID; // SC20200508
101 msgstr << "ERROR " << error_code << " in ";
102 msgstr << whereFunction() << ": "; // function name
103 msgstr << error_string() << std::endl; //<< " - "; // error type //CO20181226
104 if (error_code == 2) {
105 // msgstr << xerror_PID; //SC20200508
106 msgstr << "There was an error, but the supplied error code is invalid. Please contact the developers. ";
107 msgstr << "Supplied error message: ";
108 }
109 msgstr << what(); // detailed error message
110 return msgstr.str();
111 }
112
113 std::string xerror::whereFunction() { // CO20191201
114 return function_name;
115 }
116
117 std::string xerror::whereFileName() { // CO20191201
118 return file_name;
119 }
120
121 std::string xerror::error_string() const {
122 std::stringstream errorstr;
123 if (error_type > 0) {
124 errorstr << error_types[error_type];
125 errorstr << " (";
126 if (error_number == 0) { // reserve 0 for errors that cannot be specified otherwise
127 errorstr << "generic";
128 } else {
129 errorstr << errors[error_type][error_number - 1];
130 }
131 errorstr << ")";
132 } else {
133 errorstr << errors[0][error_number - 1];
134 }
135 return errorstr.str();
136 }
137
138 std::string xerror::what() {
139 return error_message;
140 }
141
142 int xerror::whatCode() const {
143 return error_code;
144 }
145} // namespace aurostd
146#endif
147
148//****************************************************************************
149// * *
150// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
151// * Aflow MARCO ESTERS - Duke University 2018-2021 *
152// * *
153//****************************************************************************
#define _AFLOW_NUM_ERR_TYPES_
std::string what()
std::string error_string() const
std::string file_name
std::string buildMessageString()
std::string function_name
bool codeValid() const
std::string whereFileName()
void buildException(const std::string &, const std::string &, const std::string &, const int &)
std::string whereFunction()
xerror(const std::string &, const std::string &, const std::string &, int=1)
std::string error_message
string xerror_PID
static const std::string error_types[_AFLOW_NUM_ERR_TYPES_]
static const std::string errors[_AFLOW_NUM_ERR_TYPES_][9]