/proc/self/cwd/envoy/common/exception.h
Line | Count | Source (jump to first uncovered line) |
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 | 20.3k | #define throwEnvoyExceptionOrPanic(x) throw EnvoyException(x) |
21 | 48.2k | #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 | 75.5k | EnvoyException(const std::string& message) : std::runtime_error(message) {} |
30 | | }; |
31 | | |
32 | | #define THROW_IF_NOT_OK(status_fn) \ |
33 | 18.6k | { \ |
34 | 18.6k | const absl::Status status = status_fn; \ |
35 | 18.6k | if (!status.ok()) { \ |
36 | 156 | throwEnvoyExceptionOrPanic(std::string(status.message())); \ |
37 | 156 | } \ |
38 | 18.6k | } |
39 | | |
40 | | // Simple macro to handle bridging functions which return absl::StatusOr, and |
41 | | // functions which throw errors. |
42 | | // |
43 | | // The completely unnecessary throw action argument is just so 'throw' appears |
44 | | // at the call site, so format checks about use of exceptions are triggered. |
45 | | #ifdef ENVOY_DISABLE_EXCEPTIONS |
46 | | #define THROW_IF_STATUS_NOT_OK(variable, throw_action) \ |
47 | | if (!variable.status().ok()) { \ |
48 | | PANIC(std::string(variable.status().message())); \ |
49 | | } |
50 | | #else |
51 | | #define THROW_IF_STATUS_NOT_OK(variable, throw_action) \ |
52 | 26.7k | if (!variable.status().ok()) { \ |
53 | 207 | throw_action EnvoyException(std::string(variable.status().message())); \ |
54 | 207 | } |
55 | | #endif |
56 | | |
57 | | #define RETURN_IF_STATUS_NOT_OK(variable) \ |
58 | 3.26k | if (!variable.status().ok()) { \ |
59 | 0 | return variable.status(); \ |
60 | 0 | } |
61 | | } // namespace Envoy |