Adds a standard-format exception message to OpmInputError.

This commit is contained in:
Williham Williham Totland 2020-10-02 14:35:04 +02:00
parent 65f45b60a8
commit 3af6fac35d
4 changed files with 58 additions and 16 deletions

View File

@ -91,8 +91,17 @@ public:
locations { loc }
{}
OpmInputError(const KeywordLocation& loc, const std::string& reason)
: m_what { OpmInputError::formatSingle(reason, loc) }
, locations { loc }
{}
OpmInputError(const std::vector<KeywordLocation>& locations, const std::string& reason)
: m_what { OpmInputError::formatMultiple(reason, locations) }
: m_what {
locations.size() == 1
? OpmInputError::formatSingle(reason, locations[0])
: OpmInputError::formatMultiple(reason, locations)
}
, locations { locations }
{}
@ -104,7 +113,6 @@ public:
static std::string format(const std::string& msg_format, const KeywordLocation& loc);
static std::string formatException(const KeywordLocation& loc, const std::exception& e);
static std::string formatMultiple(const std::string& reason, const std::vector<KeywordLocation>&);
private:
std::string m_what;
@ -113,6 +121,9 @@ private:
// passed in the constructor we might not have captured all the information
// in the location argument passed to the constructor.
std::vector<KeywordLocation> locations;
static std::string formatSingle(const std::string& reason, const KeywordLocation&);
static std::string formatMultiple(const std::string& reason, const std::vector<KeywordLocation>&);
};
}

View File

@ -51,14 +51,22 @@ std::string OpmInputError::format(const std::string& msg_format, const KeywordLo
namespace {
std::string formatSingle(const KeywordLocation& loc) {
std::string locationStringLine(const KeywordLocation& loc) {
return OpmInputError::format("\n {keyword} in {file}, line {line}", loc);
}
}
std::string OpmInputError::formatSingle(const std::string& reason, const KeywordLocation& location) {
const std::string defaultMessage { R"(Problem parsing keyword {{keyword}}
In {{file}} line {{line}}
Parse error: {})" } ;
return format(fmt::format(defaultMessage, reason), location);
}
std::string OpmInputError::formatMultiple(const std::string& reason, const std::vector<KeywordLocation>& locations) {
std::vector<std::string> locationStrings;
std::transform(locations.begin(), locations.end(), std::back_inserter(locationStrings), &formatSingle);
std::transform(locations.begin(), locations.end(), std::back_inserter(locationStrings), &locationStringLine);
const std::string messages { std::accumulate(locationStrings.begin(), locationStrings.end(), std::string {}) } ;
return fmt::format(R"(Problem parsing keywords {}

View File

@ -632,11 +632,11 @@ namespace Opm {
const auto& tableKeyword = deck.getKeyword(keywordName);
if (tableKeyword.size() > 2) {
const std::string msg {
const std::string reason {
"The Parser does currently NOT support the alternating record schema used in PLYSHLOG"
} ;
throw OpmInputError(msg, tableKeyword.location());
throw OpmInputError(tableKeyword.location(), reason);
}
for (size_t tableIdx = 0; tableIdx < tableKeyword.size(); tableIdx += 2) {
@ -669,13 +669,13 @@ namespace Opm {
if (m_plymwinjTables.find(table_number) == m_plymwinjTables.end()) {
m_plymwinjTables.insert(std::make_pair(table_number, std::move(table)));
} else {
const std::string msg {
const std::string reason {
"Duplicated table number " +
std::to_string(table_number) +
" for keyword PLYMWINJ found"
} ;
throw OpmInputError(msg, keyword.location());
throw OpmInputError(keyword.location(), reason);
}
}
}
@ -699,13 +699,13 @@ namespace Opm {
if (m_skprwatTables.find(table_number) == m_skprwatTables.end()) {
m_skprwatTables.insert(std::make_pair(table_number, std::move(table)));
} else {
const std::string msg {
const std::string reason {
"Duplicated table number " +
std::to_string(table_number) +
" for keyword SKPRWAT found"
} ;
throw OpmInputError(msg, keyword.location());
throw OpmInputError(keyword.location(), reason);
}
}
}
@ -729,13 +729,13 @@ namespace Opm {
if (m_skprpolyTables.find(table_number) == m_skprpolyTables.end()) {
m_skprpolyTables.insert(std::make_pair(table_number, std::move(table)));
} else {
const std::string msg {
const std::string reason {
"Duplicated table number " +
std::to_string(table_number) +
" for keyword SKPRPOLY found"
} ;
throw OpmInputError(msg, keyword.location());
throw OpmInputError(keyword.location(), reason);
}
}
}
@ -802,12 +802,12 @@ namespace Opm {
bool isDirectional = deck.hasKeyword<ParserKeywords::RKTRMDIR>();
if (isDirectional) {
const auto& keyword = deck.getKeyword<ParserKeywords::RKTRMDIR>();
const std::string msg {
const std::string reason {
"RKTRMDIR is in the deck. Flow does not support directional rock compaction mulipliers.\n"
"Make sure that your ROCKTAB table only has 3 columns)"
} ;
throw OpmInputError(msg, keyword.location());
throw OpmInputError(keyword.location(), reason);
}
bool useStressOption = false;
@ -818,9 +818,9 @@ namespace Opm {
useStressOption = (item.getTrimmedString(0) == "STRESS");
if (useStressOption) {
const std::string msg { "STRESS option is set in ROCKOPTS. Flow does not support stress option in rock compaction mulipliers" } ;
const std::string reason { "STRESS option is set in ROCKOPTS. Flow does not support stress option in rock compaction mulipliers" } ;
throw OpmInputError(msg, rockoptsKeyword.location());
throw OpmInputError(rockoptsKeyword.location(), reason);
}
}

View File

@ -94,3 +94,26 @@ Internal error: Runtime Error)" };
BOOST_CHECK_EQUAL(opm_error.what(), expected);
}
}
const Opm::KeywordLocation location2 { "MZUNSUPP", "FILENAME.DAT", 45 } ;
BOOST_AUTO_TEST_CASE(exception_multi_1) {
const std::string expected { R"(Problem parsing keyword MXUNSUPP
In FILENAME.DAT line 42
Parse error: Runtime Error)" } ;
const std::string formatted { Opm::OpmInputError({ location }, "Runtime Error").what() } ;
BOOST_CHECK_EQUAL(formatted, expected);
}
BOOST_AUTO_TEST_CASE(exception_multi_2) {
const std::string expected { R"(Problem parsing keywords
MXUNSUPP in FILENAME.DAT, line 42
MZUNSUPP in FILENAME.DAT, line 45
Parse error: Runtime Error)" } ;
const std::string formatted { Opm::OpmInputError({ location, location2 }, "Runtime Error").what() } ;
BOOST_CHECK_EQUAL(formatted, expected);
}