NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
error.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2017 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "error.hpp"
20#include "../utils/tools.hpp"
21#include "../strings/stringdatastructures.hpp"
22#include "../../kernel.hpp"
23#include "../maths/matdatastructures.hpp"
24
25size_t SyntaxError::invalid_position = std::string::npos;
26int SyntaxError::invalid_index = INT_MIN;
28static std::string sLastErrorMessage;
30
31
32
42ErrorType getErrorType(std::exception_ptr e_ptr)
43{
44 sLastErrorMessage.clear();
45
46 try
47 {
48 rethrow_exception(e_ptr);
49 }
51 {
52 // Parser exception
55 }
56 catch (const std::bad_alloc& e)
57 {
58 sLastErrorMessage = _lang.get("ERR_STD_BA_HEAD") + "\n" + e.what();
60 }
61 catch (const std::exception& e)
62 {
63 // C++ Standard exception
64 sLastErrorMessage = _lang.get("ERR_STD_INTERNAL_HEAD") + "\n" + e.what();
66 }
67 catch (SyntaxError& e)
68 {
69 // Internal exception
70 if (e.errorcode == SyntaxError::PROCESS_ABORTED_BY_USER)
71 {
72 // the user pressed ESC
73 return (nLastErrorType = TYPE_ABORT);
74 }
75 else
76 {
77 if (e.getToken().length() && (e.errorcode == SyntaxError::PROCEDURE_THROW || e.errorcode == SyntaxError::LOOP_THROW))
78 {
79 sLastErrorMessage = e.getToken();
81 }
82 else if (e.errorcode == SyntaxError::ASSERTION_ERROR)
83 {
84 sLastErrorMessage = _lang.get("ERR_NR_" + toString((int)e.errorcode) + "_0_*", e.getToken(),
85 toString(e.getIndices()[0]), toString(e.getIndices()[1]),
86 toString(e.getIndices()[2]), toString(e.getIndices()[3]));
89 }
90 else
91 {
92 sLastErrorMessage = _lang.get("ERR_NR_" + toString((int)e.errorcode) + "_0_*", e.getToken(),
93 toString(e.getIndices()[0]), toString(e.getIndices()[1]),
94 toString(e.getIndices()[2]), toString(e.getIndices()[3]));
95
96 // This error message does not exist
97 if (sLastErrorMessage.substr(0, 7) == "ERR_NR_")
98 sLastErrorMessage = _lang.get("ERR_GENERIC_0", toString((int)e.errorcode));
99
101 }
102 }
103 }
104 catch (...)
105 {
106 sLastErrorMessage = _lang.get("ERR_CATCHALL_HEAD");
108 }
109
110 return (nLastErrorType = TYPE_NOERROR);
111}
112
113
122{
123 return sLastErrorMessage;
124}
125
126
135{
136 return nLastErrorType;
137}
138
139
149{
150 switch (e)
151 {
152 case TYPE_NOERROR:
153 return "none";
154 case TYPE_ABORT:
155 return "abort";
156 case TYPE_MATHERROR:
157 return "expression";
158 case TYPE_SYNTAXERROR:
159 return "error";
161 return "assertion";
162 case TYPE_CUSTOMERROR:
163 return "thrown";
165 return "internal";
167 return "critical";
169 return "generic";
170 }
171
172 return "none";
173}
174
175
184{
185 stats.failed();
187}
188
189
197{
198 if (assertionMode)
199 {
200 assertionMode = false;
201 sAssertedExpression.clear();
202 }
203}
204
205
215{
216 stats.reset();
217}
218
219
228void Assertion::enable(const std::string& sExpr)
229{
230 sAssertedExpression = sExpr;
231 assertionMode = true;
232}
233
234
245{
246 // Only do something, if the assertion mode is
247 // active
248 if (assertionMode)
249 {
250 for (int i = 0; i < nNum; i++)
251 {
252 // If a single value is zero,
253 // throw the assertion error
254 if (v[i] == 0.0)
256 }
257
259 }
260}
261
262
272{
273 // Only do something, if the assertion mode is
274 // active
275 if (assertionMode)
276 {
277 for (const mu::value_type& val : _mMatrix.data())
278 {
279 // If a single value is zero,
280 // throw the assertion error
281 if (val == 0.0)
283 }
284
286 }
287}
288
289
299{
300 // Only do something, if the assertion mode is
301 // active and the strings are not only logicals
302 if (assertionMode && !strRes.bOnlyLogicals)
303 {
304 for (size_t i = 0; i < strRes.vResult.size(); i++)
305 {
306 if (strRes.vResult[i] == "\"\"")
308 else if (strRes.vNoStringVal[i])
309 {
311
312 if (NumeReKernel::getInstance()->getParser().Eval() == 0.0)
314 }
315 }
316
318 }
319}
320
321
329{
330 return stats;
331}
332
333
334
335
336
This class handles assertions and throws the corresponding exception, if the assertion fails....
Definition: error.hpp:548
void enable(const std::string &sExpr)
Enables the assertion handler using the passed expression.
Definition: error.cpp:228
void reset()
Resets the assertion handler.
Definition: error.cpp:196
AssertionStats getStats() const
Returns the current tests stats.
Definition: error.cpp:328
void assertionFail()
This member function is a wrapper around the assertion error.
Definition: error.cpp:183
AssertionStats stats
Definition: error.hpp:553
bool assertionMode
Definition: error.hpp:551
void resetStats()
Resets the internal statistic variables for accumulating the total number of executed and the number ...
Definition: error.cpp:214
std::string sAssertedExpression
Definition: error.hpp:550
void checkAssertion(mu::value_type *v, int nNum)
Checks the return value of a muParser evaluated result.
Definition: error.cpp:244
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
Definition: language.cpp:292
This class defines a dynamic size 2D matrix with a single 1D internal buffer. If the internal buffer ...
std::vector< mu::value_type > & data()
Get a reference to the internal data structure.
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
Definition: kernel.hpp:221
mu::Parser & getParser()
Definition: kernel.hpp:281
Common exception class for all exceptions thrown in NumeRe.
Definition: error.hpp:32
@ LOOP_THROW
Definition: error.hpp:152
@ ASSERTION_ERROR
Definition: error.hpp:49
@ PROCEDURE_THROW
Definition: error.hpp:199
@ PROCESS_ABORTED_BY_USER
Definition: error.hpp:202
static int invalid_index
Definition: error.hpp:236
static size_t invalid_position
Definition: error.hpp:235
void SetExpr(StringView a_sExpr)
Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of...
Error class of the parser.
const string_type & GetMsg() const
Returns the message string for this error.
Language _lang
Definition: kernel.cpp:39
static std::string sLastErrorMessage
Definition: error.cpp:28
ErrorType getErrorType(std::exception_ptr e_ptr)
This function obtains the error type of a catched exception and sets the last error message.
Definition: error.cpp:42
std::string errorTypeToString(ErrorType e)
Return the error type converted to a human readable string.
Definition: error.cpp:148
Assertion _assertionHandler
Definition: error.cpp:27
std::string getLastErrorMessage()
Return the last error message, which was catched by the getErrorType() function.
Definition: error.cpp:121
ErrorType getLastErrorType()
Return the last error type, which was catched by the getErrorType() function.
Definition: error.cpp:134
static ErrorType nLastErrorType
Definition: error.cpp:29
ErrorType
Defines the possible error types, which can be thrown in this application.
Definition: error.hpp:469
@ TYPE_GENERICERROR
Definition: error.hpp:478
@ TYPE_INTERNALERROR
Definition: error.hpp:476
@ TYPE_NOERROR
Definition: error.hpp:470
@ TYPE_ASSERTIONERROR
Definition: error.hpp:474
@ TYPE_SYNTAXERROR
Definition: error.hpp:473
@ TYPE_MATHERROR
Definition: error.hpp:472
@ TYPE_CRITICALERROR
Definition: error.hpp:477
@ TYPE_CUSTOMERROR
Definition: error.hpp:475
@ TYPE_ABORT
Definition: error.hpp:471
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
This structure accumulates the statistics for the assertion handler.
Definition: error.hpp:496
void failed()
Assertion failed.
Definition: error.hpp:531
void succeeded()
Assertion was successful.
Definition: error.hpp:520
void reset()
Reset the statistics.
Definition: error.hpp:508
This structure contains all possible return values of the central string parser in single combined st...
std::vector< bool > vNoStringVal
std::string toString(int)
Converts an integer to a string without the Settings bloat.
Match findCommand(StringView sCmd, const std::string &sCommand)
This function is very important for the command handler.
Definition: tools.cpp:1275