30 #include "MeshKernel/Constants.hpp"
31 #include "MeshKernel/Definitions.hpp"
32 #include "MeshKernel/Formatting.hpp"
36 #include <source_location>
40 #include <string_view>
42 #define STRINGIFY(str) #str
43 #define TO_STR_LITERAL(str) STRINGIFY(str)
75 m_exit_code{exit_code}
81 [[nodiscard]] std::string_view
Name()
const {
return m_name; }
88 std::string_view m_name;
100 std::source_location
const& source_location = std::source_location::current())
101 : m_format_string(format_string),
102 m_source_location(source_location)
110 std::source_location
const& source_location = std::source_location::current())
111 : m_format_string(message),
112 m_source_location(source_location)
120 std::source_location
const& source_location = std::source_location::current())
121 : m_format_string(message),
122 m_source_location(source_location)
128 [[nodiscard]] std::string_view
String()
const {
return m_format_string; }
132 [[nodiscard]] std::source_location
const&
SourceLocation()
const {
return m_source_location; }
135 std::string_view m_format_string;
137 std::source_location m_source_location;
147 template <
typename... Args>
150 m_source_location(format_string.SourceLocation())
152 if (
sizeof...(args) == 0)
154 m_formatted_message = format_string.
String();
158 m_formatted_message = fmt_ns::vformat(format_string.
String(), fmt_ns::make_format_args(args...));
167 const char*
what() const noexcept
override
169 #if HAVE_SRC_LOC_IN_ERR_MSGS
170 m_what = fmt_ns::format(
"Exception of type '{}' in {} ({}:{}) {}: {}\n",
173 m_source_location.line(),
174 m_source_location.column(),
175 m_source_location.function_name(),
178 m_what = fmt_ns::format(
"Exception of type '{}': {}\n",
182 return m_what.c_str();
194 return {
"MeshKernelError", ExitCode::MeshKernelErrorCode};
201 std::string m_formatted_message;
203 mutable std::string m_what;
205 std::source_location m_source_location;
209 [[nodiscard]] std::string StrippedFilePath()
const
211 std::string path = m_source_location.file_name();
213 std::string path_to_erase(TO_STR_LITERAL(CMAKE_SRC_DIR));
215 std::replace(path_to_erase.begin(), path_to_erase.end(),
'/',
'\\');
217 if (
size_t pos = path.find(path_to_erase); pos != std::string::npos)
220 path.erase(pos, path_to_erase.length() + 1);
222 std::replace(path.begin(), path.end(),
'\\',
'/');
242 return {
"NotImplementedError", ExitCode::NotImplementedErrorCode};
258 return {
"AlgorithmError", ExitCode::AlgorithmErrorCode};
277 return {
"ConstraintError", ExitCode::ConstraintErrorCode};
290 template <
typename... Args>
296 m_mesh_index(mesh_index),
297 m_mesh_location(mesh_location)
314 return {
"MeshGeometryError", ExitCode::MeshGeometryErrorCode};
318 [[nodiscard]] std::string FormattedMessage()
const override
320 return fmt_ns::format(
"Error occurred at index {} (location: {}). {}",
322 LocationToString.at(m_mesh_location),
342 return {
"LinearAlgebraError", ExitCode::LinearAlgebraErrorCode};
358 return {
"RangeError", ExitCode::RangeErrorCode};
A class for throwing not implemented exceptions.
Definition: Exceptions.hpp:231
virtual std::string FormattedMessage() const
Returns the message.
Definition: Exceptions.hpp:198
virtual ErrorCategory Category() const
Returns the error category.
Definition: Exceptions.hpp:192
@ ConstraintErrorCode
Constraint error.
Definition: Exceptions.hpp:57
meshkernel::UInt MeshIndex() const
Returns the invalid index.
Definition: Exceptions.hpp:303
ExitCode Code() const
Return the exit code of the error category.
Definition: Exceptions.hpp:85
ErrorCategory(std::string_view name, ExitCode exit_code)
Class constructor.
Definition: Exceptions.hpp:72
MeshKernelError(FormatString const &format_string, Args &&... args)
Class constructor.
Definition: Exceptions.hpp:148
@ StdLibExceptionCode
Standrad library exception.
Definition: Exceptions.hpp:61
const char * what() const noexcept override
Returns the explanatory string of the error.
Definition: Exceptions.hpp:167
@ RangeErrorCode
Range error.
Definition: Exceptions.hpp:60
Location
Mesh locations enumeration.
Definition: Definitions.hpp:74
A class for throwing linear algebra exceptions.
Definition: Exceptions.hpp:331
@ AlgorithmErrorCode
Algorithm error.
Definition: Exceptions.hpp:56
@ UnknownExceptionCode
Unknown exception.
Definition: Exceptions.hpp:62
@ MeshGeometryErrorCode
Geometry error.
Definition: Exceptions.hpp:58
Contains the logic of the C++ static library.
Definition: AveragingInterpolation.hpp:36
A class for throwing mesh geometry errors.
Definition: Exceptions.hpp:282
MeshGeometryError(meshkernel::UInt mesh_index, Location mesh_location, FormatString const &format_string, Args &&... args)
Class constructor.
Definition: Exceptions.hpp:291
@ LinearAlgebraErrorCode
Linear algebra error.
Definition: Exceptions.hpp:59
std::uint32_t UInt
Integer type used when indexing mesh graph entities.
Definition: Definitions.hpp:38
@ MeshKernelErrorCode
MehKernel error.
Definition: Exceptions.hpp:54
A class for throwing algorithm exceptions.
Definition: Exceptions.hpp:247
ExitCode Code() const
Returns the exit code.
Definition: Exceptions.hpp:187
A class for throwing range error exceptions.
Definition: Exceptions.hpp:347
ExitCode
Enumeration of exit codes.
Definition: Exceptions.hpp:51
A class for throwing general MeshKernel exceptions.
Definition: Exceptions.hpp:141
Contains error category information.
Definition: Exceptions.hpp:66
@ NotImplementedErrorCode
Not implemented error.
Definition: Exceptions.hpp:55
virtual ~MeshKernelError()=default
Class destructor.
An exception class thrown when an attempt is made that violates a range constraint.
Definition: Exceptions.hpp:266
@ Success
Success.
Definition: Exceptions.hpp:53
Location MeshLocation() const
Returns the mesh location.
Definition: Exceptions.hpp:307
std::string_view Name() const
Returns the name of the error category.
Definition: Exceptions.hpp:81