Extends OpmInputError to be initialisable from an existing exception.

This commit is contained in:
Williham Williham Totland
2020-09-28 13:16:45 +02:00
parent 742c90cabc
commit 9a66acbb5d
3 changed files with 18 additions and 3 deletions
+8 -2
View File
@@ -21,6 +21,7 @@
#include <stdexcept>
#include <string>
#include <utility>
#include <fmt/format.h>
@@ -69,6 +70,11 @@ public:
location { loc }
{}
OpmInputError(const KeywordLocation& loc, const std::exception& e, const std::string& reason = "Internal error message") :
m_what { OpmInputError::formatException(loc, e, reason) },
location { loc }
{}
const char * what() const throw()
{
return this->m_what.c_str();
@@ -87,9 +93,9 @@ public:
static std::string formatException(const KeywordLocation& loc, const std::exception& e, const std::string& reason = "Internal error message") {
const std::string defaultMessage { R"(Problem parsing keyword {keyword}
In {file} line {line}.
{reason}: {error})" } ;
{}: {})" } ;
return format(defaultMessage, loc, fmt::arg("reason", reason), fmt::arg("error", e.what()));
return format(defaultMessage, loc, reason, e.what());
}
-1
View File
@@ -847,7 +847,6 @@ BOOST_AUTO_TEST_CASE(OPM_ERROR) {
BOOST_CHECK_EQUAL(error4.what(), "kw:100:kw");
BOOST_CHECK_EQUAL(error5.what(), "kw:100:file: error");
/*
This test is meant to emulate the typical parsing process, the blocks here
in the main test function represent main() in the simulator and the main
+10
View File
@@ -73,3 +73,13 @@ Reason: Runtime Error)" };
BOOST_CHECK_EQUAL(formatted, expected);
}
BOOST_AUTO_TEST_CASE(exception_init) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP
In FILENAME.DAT line 42.
Reason: Runtime Error)" };
const std::string formatted { Opm::OpmInputError(location, std::runtime_error("Runtime Error"), "Reason").what() } ;
BOOST_CHECK_EQUAL(formatted, expected);
}