NeoN
A framework for CFD software
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstdlib>
8#include <exception>
9#include <iostream>
10#include <string>
11#include <sstream>
12#include <iostream>
13
14#ifdef NF_WITH_MPI_SUPPORT
15#include <mpi.h>
16#endif
17
18// compiling with clang and cuda fails to
19// find source location
20// #include <source_location>
21// #include <experimental/source_location>
22
23// #include "logging.hpp"
24#include "info.hpp"
25
27{
28// Forward declaration: error.hpp must not include logging.hpp — that would form a
29// cycle (logging.hpp -> mpi/environment.hpp -> error.hpp). logFatal is defined in
30// core/logging.cpp; NF_ERROR_EXIT routes through it for rank-tagged output, a
31// stack trace, and a clean MPI_Abort.
32[[noreturn]] void logFatal(const std::string& message);
33}
34
35namespace NeoN
36{
37
44class NeoNException : public std::exception
45{
46public:
47
52 explicit NeoNException(const std::string& message) : message_(message) {}
53
58 const char* what() const noexcept override { return message_.c_str(); }
59
60private:
61
62 std::string message_;
63};
64
65} // namespace NeoN
66
74#define NF_ERROR_MESSAGE(message) \
75 "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n"
76
87// Builds the message into a temporary stringstream (not a named local, so this
88// is usable inside constexpr functions) and routes it through logFatal, which
89// tags the rank, prints a trace, MPI_Aborts, and exits.
90#define NF_ERROR_EXIT(message) \
91 NeoN::Logging::detail::logFatal((std::stringstream() << NF_ERROR_MESSAGE(message)).str())
92
103#define NF_THROW(message) \
104 throw NeoN::NeoNException((std::stringstream() << NF_ERROR_MESSAGE(std::string(message))).str())
105
118#define NF_ASSERT(condition, message) \
119 do \
120 { \
121 if (!(condition)) [[unlikely]] \
122 { \
123 NF_ERROR_EXIT( \
124 "Assertion `" #condition " in " << __FILE__ << ":" << __LINE__ \
125 << "` failed.\n " << message \
126 ); \
127 } \
128 } \
129 while (false)
130
144#define NF_ASSERT_THROW(condition, message) \
145 do \
146 { \
147 if (!(condition)) [[unlikely]] \
148 { \
149 NF_THROW("Assertion `" #condition "` failed.\n " << message); \
150 } \
151 } \
152 while (false)
153
154#ifdef NF_DEBUG
165#define NF_DEBUG_ASSERT(condition, message) NF_ASSERT(condition, message)
166
178#define NF_DEBUG_ASSERT_THROW(condition, message) NF_ASSERT_THROW(condition, message)
179#else
190#define NF_DEBUG_ASSERT(condition, message) ((void)0)
191
203#define NF_DEBUG_ASSERT_THROW(condition, message) ((void)0)
204#endif
205
218#define NF_ASSERT_EQUAL(a, b) NF_ASSERT(a == b, "Expected " << b << ", got " << a)
219
230#define NF_DEBUG_ASSERT_EQUAL(a, b) NF_DEBUG_ASSERT(a == b, "Expected " << b << ", got " << a)
231
245#define NF_ASSERT_EQUAL_THROW(a, b) NF_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
246
257#define NF_DEBUG_ASSERT_EQUAL_THROW(a, b) \
258 NF_DEBUG_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
Custom exception class for NeoN.
Definition error.hpp:45
const char * what() const noexcept override
Returns the error message associated with the exception.
Definition error.hpp:58
NeoNException(const std::string &message)
Constructs a NeoNException object with the given error message.
Definition error.hpp:52
void logFatal(const std::string &message)
Integer types used throughout NeoN.
Definition array.hpp:18