Adjusts the defaultMessage format string.

This commit is contained in:
Williham Williham Totland 2020-10-05 09:26:25 +02:00
parent 7a8347f1ab
commit d0c5fa042c
7 changed files with 24 additions and 40 deletions

View File

@ -103,7 +103,6 @@ public:
static std::string format(const std::string& msg_format, const KeywordLocation& loc); static std::string format(const std::string& msg_format, const KeywordLocation& loc);
static std::string formatException(const std::exception& e, const KeywordLocation& loc);
private: private:
// The location member is here for debugging; depending on the msg_fmt // The location member is here for debugging; depending on the msg_fmt
@ -113,6 +112,7 @@ private:
std::string m_what; std::string m_what;
static std::string formatException(const std::exception& e, const KeywordLocation& loc);
static std::string formatSingle(const std::string& reason, const KeywordLocation&); static std::string formatSingle(const std::string& reason, const KeywordLocation&);
static std::string formatMultiple(const std::string& reason, const std::vector<KeywordLocation>&); static std::string formatMultiple(const std::string& reason, const std::vector<KeywordLocation>&);
}; };

View File

@ -41,7 +41,7 @@ std::string formatImpl(const std::string& msg_format, const KeywordLocation& loc
} }
std::string OpmInputError::formatException(const std::exception& e, const KeywordLocation& loc) { std::string OpmInputError::formatException(const std::exception& e, const KeywordLocation& loc) {
const std::string defaultMessage { R"(Problem parsing keyword {keyword} const std::string defaultMessage { R"(Problem with keyword {keyword}
In {file} line {line}. In {file} line {line}.
Internal error: {})" } ; Internal error: {})" } ;
@ -60,9 +60,9 @@ std::string OpmInputError::format(const std::string& msg_format, const KeywordLo
} }
std::string OpmInputError::formatSingle(const std::string& reason, const KeywordLocation& location) { std::string OpmInputError::formatSingle(const std::string& reason, const KeywordLocation& location) {
const std::string defaultMessage { R"(Problem parsing keyword {keyword} const std::string defaultMessage { R"(Problem with keyword {keyword}
In {file} line {line} In {file} line {line}
Parse error: {})" } ; {})" } ;
return formatImpl(defaultMessage, location, reason); return formatImpl(defaultMessage, location, reason);
} }
@ -80,8 +80,8 @@ std::string OpmInputError::formatMultiple(const std::string& reason, const std::
std::transform(locations.begin(), locations.end(), std::back_inserter(locationStrings), &locationStringLine); std::transform(locations.begin(), locations.end(), std::back_inserter(locationStrings), &locationStringLine);
const std::string messages { std::accumulate(locationStrings.begin(), locationStrings.end(), std::string {}) } ; const std::string messages { std::accumulate(locationStrings.begin(), locationStrings.end(), std::string {}) } ;
return fmt::format(R"(Problem parsing keywords {} return fmt::format(R"(Problem with keywords {}
Parse error: {})", messages, reason); {})", messages, reason);
} }
} }

View File

@ -1767,8 +1767,13 @@ namespace {
} catch (const OpmInputError&) { } catch (const OpmInputError&) {
throw; throw;
} catch (const std::exception& e) { } catch (const std::exception& e) {
OpmLog::error(OpmInputError::formatException(e, handlerContext.keyword.location())); const OpmInputError opm_error { e, handlerContext.keyword.location() } ;
OpmLog::error(opm_error.what());
// Ideally, we'd be doing this, but the ramifications are not clear p.t.
// std::throw_with_nested(opm_error);
// So we're doing this instead
throw; throw;
} }

View File

@ -162,10 +162,8 @@ namespace Opm {
// make sure the error object does not terminate the application // make sure the error object does not terminate the application
// when it goes out of scope. // when it goes out of scope.
errors.clear(); errors.clear();
if (location)
throw OpmInputError(msg_fmt, *location); throw OpmInputError(msg_fmt, location.value_or(KeywordLocation{}));
else
throw OpmInputError(msg_fmt, {});
} }
if (action == InputError::EXIT1) { if (action == InputError::EXIT1) {

View File

@ -934,10 +934,11 @@ bool parseState( ParserState& parserState, const Parser& parser ) {
same exception without updating the what() message of the same exception without updating the what() message of the
exception. exception.
*/ */
const OpmInputError opm_error { e, rawKeyword->location() } ;
OpmLog::error(OpmInputError::formatException(e, rawKeyword->location())); OpmLog::error(opm_error.what());
throw; std::throw_with_nested(opm_error);
} }
} else { } else {
const std::string msg = "The keyword " + rawKeyword->getKeywordName() + " is not recognized - ignored"; const std::string msg = "The keyword " + rawKeyword->getKeywordName() + " is not recognized - ignored";

View File

@ -2125,7 +2125,7 @@ FIELD
section in order to provoke the exception; if at some stage the opm parser section in order to provoke the exception; if at some stage the opm parser
treats section stricter this test might fail due to that reason instead. treats section stricter this test might fail due to that reason instead.
*/ */
BOOST_CHECK_THROW( Parser{}.parseString(deck_string), std::invalid_argument); BOOST_CHECK_THROW( Parser{}.parseString(deck_string), OpmInputError);
} }
BOOST_AUTO_TEST_CASE(ParseRSConstT) { BOOST_AUTO_TEST_CASE(ParseRSConstT) {

View File

@ -49,28 +49,8 @@ BOOST_AUTO_TEST_CASE(positional) {
BOOST_CHECK_EQUAL(formatted, expected); BOOST_CHECK_EQUAL(formatted, expected);
} }
BOOST_AUTO_TEST_CASE(exception) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP
In FILENAME.DAT line 42.
Internal error: Runtime Error)" };
const std::string formatted { Opm::OpmInputError::formatException(std::runtime_error("Runtime Error"), location) } ;
BOOST_CHECK_EQUAL(formatted, expected);
}
BOOST_AUTO_TEST_CASE(exception_reason) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP
In FILENAME.DAT line 42.
Internal error: Runtime Error)" };
const std::string formatted { Opm::OpmInputError::formatException(std::runtime_error("Runtime Error"), location) } ;
BOOST_CHECK_EQUAL(formatted, expected);
}
BOOST_AUTO_TEST_CASE(exception_init) { BOOST_AUTO_TEST_CASE(exception_init) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP const std::string expected { R"(Problem with keyword MXUNSUPP
In FILENAME.DAT line 42. In FILENAME.DAT line 42.
Internal error: Runtime Error)" }; Internal error: Runtime Error)" };
@ -80,7 +60,7 @@ Internal error: Runtime Error)" };
} }
BOOST_AUTO_TEST_CASE(exception_nest) { BOOST_AUTO_TEST_CASE(exception_nest) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP const std::string expected { R"(Problem with keyword MXUNSUPP
In FILENAME.DAT line 42. In FILENAME.DAT line 42.
Internal error: Runtime Error)" }; Internal error: Runtime Error)" };
@ -98,9 +78,9 @@ Internal error: Runtime Error)" };
const Opm::KeywordLocation location2 { "MZUNSUPP", "FILENAME.DAT", 45 } ; const Opm::KeywordLocation location2 { "MZUNSUPP", "FILENAME.DAT", 45 } ;
BOOST_AUTO_TEST_CASE(exception_multi_1) { BOOST_AUTO_TEST_CASE(exception_multi_1) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP const std::string expected { R"(Problem with keyword MXUNSUPP
In FILENAME.DAT line 42 In FILENAME.DAT line 42
Parse error: Runtime Error)" } ; Runtime Error)" } ;
const std::string formatted { Opm::OpmInputError("Runtime Error", location).what() } ; const std::string formatted { Opm::OpmInputError("Runtime Error", location).what() } ;
@ -108,10 +88,10 @@ Parse error: Runtime Error)" } ;
} }
BOOST_AUTO_TEST_CASE(exception_multi_2) { BOOST_AUTO_TEST_CASE(exception_multi_2) {
const std::string expected { R"(Problem parsing keywords const std::string expected { R"(Problem with keywords
MXUNSUPP in FILENAME.DAT, line 42 MXUNSUPP in FILENAME.DAT, line 42
MZUNSUPP in FILENAME.DAT, line 45 MZUNSUPP in FILENAME.DAT, line 45
Parse error: Runtime Error)" } ; Runtime Error)" } ;
const std::string formatted { Opm::OpmInputError("Runtime Error", location, location2).what() } ; const std::string formatted { Opm::OpmInputError("Runtime Error", location, location2).what() } ;