opm-common
Loading...
Searching...
No Matches
CriticalError.hpp File Reference

Provides error handling mechanisms for critical errors in the OPM framework. More...

#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>

Go to the source code of this file.

Classes

class  CriticalError
 A custom exception class that extends std::exception to handle critical errors. More...

Macros

#define OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR(...)
 Ends try-catch block and rethrows any caught exceptions as CriticalError.
#define OPM_TRY_THROW_AS_CRITICAL_ERROR(expr, ...)
 Tries to execute an expression and rethrows any caught exceptions as CriticalError.

Functions

std::string detail::makeCriticalErrorMessageErrorHint (const std::string &text)
std::string detail::makeCriticalErrorMessageErrorHint ()

Detailed Description

Provides error handling mechanisms for critical errors in the OPM framework.

This file contains the CriticalError exception class and macros for handling critical errors in a consistent way across the OPM codebase.

The key components are:

  • CriticalError class: A custom exception for critical errors
  • OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR macro: Catches and rethrows exceptions
  • OPM_TRY_THROW_AS_CRITICAL_ERROR macro: Wraps code execution with error handling

Both macros support an optional hint string that provides additional error context:

try {
riskyOperation();
}
OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR("Check input file permissions");
// Or using the try macro:
"Operation requires valid config");
#define OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR(...)
Ends try-catch block and rethrows any caught exceptions as CriticalError.
Definition CriticalError.hpp:136
#define OPM_TRY_THROW_AS_CRITICAL_ERROR(expr,...)
Tries to execute an expression and rethrows any caught exceptions as CriticalError.
Definition CriticalError.hpp:162

When an error occurs, the macros will:

  1. Catch the exception
  2. Create a detailed error message including:
    • File and line location
    • Original error message
    • Optional hint string if provided
  3. Wrap in a CriticalError while preserving the original exception

The hint string is especially useful for providing:

  • Troubleshooting guidance
  • Required preconditions
  • Suggested fixes
  • Context about the operation

Macro Definition Documentation

◆ OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR

#define OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR ( ...)
Value:
catch (const ::Opm::CriticalError&) \
{ \
throw; \
} \
catch (const std::exception& exp) \
{ \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "].\nOriginal error: " + exp.what() \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
} \
catch (...) \
{ \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "]. Unknown original error." \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
}

Ends try-catch block and rethrows any caught exceptions as CriticalError.

This macro is used to catch exceptions and rethrow them as CriticalError. It also provides a way to add an optional hint string to the error message.

Parameters
...An optional hint string to provide additional error context.

◆ OPM_TRY_THROW_AS_CRITICAL_ERROR

#define OPM_TRY_THROW_AS_CRITICAL_ERROR ( expr,
... )
Value:
try { \
expr; \
} catch (const ::Opm::CriticalError&) { \
throw; \
} catch (const std::exception& exp) { \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "].\nOriginal error: " + exp.what() \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
} catch (...) { \
const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
+ std::to_string(__LINE__) + "]. Unknown original error." \
+ ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
}

Tries to execute an expression and rethrows any caught exceptions as CriticalError.

Parameters
exprThe expression to execute.
...An optional hint string to provide additional error context.