My Project
Loading...
Searching...
No Matches
OpmInputError.hpp
1/*
2 Copyright 2020 Equinor ASA.
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_ERROR_HPP
21#define OPM_ERROR_HPP
22
23#include <opm/common/OpmLog/KeywordLocation.hpp>
24
25#include <stdexcept>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace Opm {
31
32// The OpmInputError is a custom exception class which can be used to signal
33// errors in input handling. The importance of the OpmInputError exception
34// is *not* the tecnical functionality it provides, but rather the
35// convention surrounding it, when and how it should be used.
36//
37// The OpmInputError should be used in situations which are "close to user
38// input", the root cause can either be incorrect user input or a
39// bug/limitation in opm. OpmInputError should only be used in situations
40// where we have a good understanding of the underlying issue, and can
41// provide a good error message.
42//
43// The local error handling should be complete when the OpmInputError is
44// instantiated, it should not be caught and rethrown in order to e.g. add
45// additional context or log messages. In order to avoid inadvertendly
46// catching this exception in a catch all block.
47
48class OpmInputError : public std::exception
49{
50public:
51 // The message string will be used as format string in the fmt::format()
52 // function as, and optional {} markers can be used to inject keyword,
53 // filename and line numbers into the final what() message. The
54 // placeholders may use one or more of the following named arguments
55 //
56 // {keyword} -> location.keyword
57 // {file} -> location.filename
58 // {line} -> location.lineno
59 //
60 // Furthermore, the message can contain any number of positional
61 // arguments to add further context to the message.
62 //
63 // KeywordLocation loc("KW", "file.inc", 100);
64 // OpmInputError("Error at line {line} in file {file} - keyword: {keyword} ignored", location);
65 // OpmInputError("Error at line {line} in file {file} - keyword: {keyword} has invalid argument {}", invalid_argument);
66
67 template <typename... Args>
68 OpmInputError(const std::string& reason,
69 const KeywordLocation& location,
70 Args&&... furtherLocations)
71 : locations { location, std::forward<Args>(furtherLocations)... }
72 , m_what {
73 (locations.size() == 1)
74 ? formatSingle(reason, locations.front())
75 : formatMultiple(reason, locations)
76 }
77 {}
78
79 // Allows for the initialisation of an OpmInputError from another exception.
80 //
81 // Usage:
82 //
83 // try {
84 // .
85 // .
86 // .
87 // } catch (const Opm::OpmInputError&) {
88 // throw;
89 // } catch (const std::exception& e) {
90 // std::throw_with_nested(Opm::OpmInputError(e, location));
91 // }
92 OpmInputError(const std::exception& error, const KeywordLocation& location)
93 : locations { location }
94 , m_what { formatException(error, locations.front()) }
95 {}
96
97 const char* what() const noexcept override
98 {
99 return this->m_what.c_str();
100 }
101
102 static std::string format(const std::string& msg_format, const KeywordLocation& loc);
103
104private:
105 // The location member is here for debugging; depending on the msg_fmt
106 // passed in the constructor we might not have captured all the
107 // information in the location argument passed to the constructor.
108 std::vector<KeywordLocation> locations;
109
110 std::string m_what;
111
112 static std::string formatException(const std::exception& e, const KeywordLocation& loc);
113 static std::string formatSingle(const std::string& reason, const KeywordLocation&);
114 static std::string formatMultiple(const std::string& reason, const std::vector<KeywordLocation>&);
115};
116
117} // namespace Opm
118
119#endif // OPM_ERROR_HPP
Definition KeywordLocation.hpp:27
Definition OpmInputError.hpp:49
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30