LCOV - code coverage report
Current view: top level - envoy/common - exception.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 15 19 78.9 %
Date: 2024-01-05 06:35:25 Functions: 1 2 50.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <stdexcept>
       4             : #include <string>
       5             : 
       6             : #include "source/common/common/assert.h"
       7             : 
       8             : namespace Envoy {
       9             : 
      10             : // This is a workaround to allow an exceptionless Envoy Mobile build while we
      11             : // have not finished plumbing Satus/StatusOr<> based error handling, so
      12             : // hard-failing instead. See
      13             : // (https://github.com/envoyproxy/envoy-mobile/issues/176)
      14             : // for example error handling PRs.
      15             : // TODO(alyssawilk) finish up error handling and remove this.
      16             : #ifdef ENVOY_DISABLE_EXCEPTIONS
      17             : #define throwEnvoyExceptionOrPanic(x) PANIC(x)
      18             : #define throwExceptionOrPanic(x, y) PANIC(y)
      19             : #else
      20        7245 : #define throwEnvoyExceptionOrPanic(x) throw EnvoyException(x)
      21          22 : #define throwExceptionOrPanic(y, x) throw y(x)
      22             : #endif
      23             : 
      24             : /**
      25             :  * Base class for all envoy exceptions.
      26             :  */
      27             : class EnvoyException : public std::runtime_error {
      28             : public:
      29        7659 :   EnvoyException(const std::string& message) : std::runtime_error(message) {}
      30             : };
      31             : 
      32             : #define THROW_IF_NOT_OK_REF(status)                                                                \
      33        3666 :   do {                                                                                             \
      34        3666 :     if (!(status).ok()) {                                                                          \
      35          11 :       throwEnvoyExceptionOrPanic(std::string((status).message()));                                 \
      36          11 :     }                                                                                              \
      37        3666 :   } while (0)
      38             : 
      39             : #define THROW_IF_NOT_OK(status_fn)                                                                 \
      40        1012 :   do {                                                                                             \
      41        1012 :     const absl::Status status = (status_fn);                                                       \
      42        1012 :     THROW_IF_NOT_OK_REF(status);                                                                   \
      43        1012 :   } while (0)
      44             : 
      45             : // Simple macro to handle bridging functions which return absl::StatusOr, and
      46             : // functions which throw errors.
      47             : //
      48             : // The completely unnecessary throw_action argument was just so 'throw' appears
      49             : // at the call site, so format checks about use of exceptions would be triggered.
      50             : // This didn't work, so the variable is no longer used and is not duplicated in
      51             : // the macros above.
      52        2654 : #define THROW_IF_STATUS_NOT_OK(variable, throw_action) THROW_IF_NOT_OK_REF(variable.status());
      53             : 
      54             : #define RETURN_IF_STATUS_NOT_OK(variable)                                                          \
      55         454 :   if (!variable.status().ok()) {                                                                   \
      56           0 :     return variable.status();                                                                      \
      57           0 :   }
      58             : 
      59             : #define RETURN_IF_NOT_OK(variable)                                                                 \
      60          33 :   if (!variable.ok()) {                                                                            \
      61           0 :     return variable;                                                                               \
      62           0 :   }
      63             : } // namespace Envoy

Generated by: LCOV version 1.15