]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
bbc27441 AJ |
3 | * |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
774c051c | 9 | #ifndef SQUID__TEXTEXCEPTION_H |
10 | #define SQUID__TEXTEXCEPTION_H | |
11 | ||
ebaabe74 | 12 | #include "base/Here.h" |
774c051c | 13 | |
205f2345 | 14 | #include <stdexcept> |
774c051c | 15 | |
e99fa721 EB |
16 | class SBuf; |
17 | ||
ebaabe74 AR |
18 | /// an std::runtime_error with thrower location info |
19 | class TextException: public std::runtime_error | |
774c051c | 20 | { |
21 | ||
22 | public: | |
ebaabe74 AR |
23 | TextException(const char *message, const SourceLocation &location): |
24 | std::runtime_error(message), | |
25 | where(location) | |
26 | {} | |
774c051c | 27 | |
ebaabe74 | 28 | TextException(SBuf message, const SourceLocation &location); |
b3c9f64a | 29 | |
ebaabe74 AR |
30 | TextException(const TextException &) = default; |
31 | TextException(TextException &&) = default; | |
32 | TextException& operator=(const TextException &) = default; | |
774c051c | 33 | |
ebaabe74 AR |
34 | /* std::runtime_error API */ |
35 | virtual ~TextException() throw() override; | |
36 | virtual const char *what() const throw() override; | |
77442705 | 37 | |
ebaabe74 AR |
38 | /// same-location exceptions have the same ID |
39 | SourceLocationId id() const { return where.id(); } | |
774c051c | 40 | |
ebaabe74 AR |
41 | /// dumps the exception text into the stream |
42 | std::ostream &print(std::ostream &) const; | |
64b66b76 | 43 | |
ebaabe74 AR |
44 | /// code location related to the exception; usually the thrower location |
45 | SourceLocation where; | |
b3c9f64a | 46 | |
ebaabe74 AR |
47 | // TODO: Add support for arbitrary (re)thrower-supplied details: |
48 | // std::tuple<Details...> details; | |
774c051c | 49 | }; |
50 | ||
ebaabe74 AR |
51 | /// prints active (i.e., thrown but not yet handled) exception |
52 | std::ostream &CurrentException(std::ostream &); | |
53 | ||
83b053a0 CT |
54 | /// efficiently prints TextException |
55 | std::ostream &operator <<(std::ostream &, const TextException &); | |
56 | ||
ebaabe74 AR |
57 | /// legacy convenience macro; it is not difficult to type Here() now |
58 | #define TexcHere(msg) TextException((msg), Here()) | |
59 | ||
60 | /// Like assert() but throws an exception instead of aborting the process | |
2bde9c7c AR |
61 | /// and allows the caller to customize the exception message and location. |
62 | /// \param description string literal describing the condition; what MUST happen | |
63 | #define Must3(condition, description, location) \ | |
ebaabe74 AR |
64 | do { \ |
65 | if (!(condition)) { \ | |
2bde9c7c | 66 | const TextException Must_ex_(("check failed: " description), (location)); \ |
ebaabe74 AR |
67 | debugs(0, 3, Must_ex_.what()); \ |
68 | throw Must_ex_; \ | |
69 | } \ | |
70 | } while (/*CONSTCOND*/ false) | |
71 | ||
72 | /// Like assert() but throws an exception instead of aborting the process. | |
2bde9c7c | 73 | #define Must(condition) Must3((condition), #condition, Here()) |
ebaabe74 AR |
74 | |
75 | /// Reports and swallows all exceptions to prevent compiler warnings and runtime | |
76 | /// errors related to throwing class destructors. Should be used for most dtors. | |
77 | #define SWALLOW_EXCEPTIONS(code) \ | |
78 | try { \ | |
79 | code \ | |
80 | } catch (...) { \ | |
ccfbe8f4 AR |
81 | debugs(0, DBG_IMPORTANT, "BUG: ignoring exception;" << \ |
82 | Debug::Extra << "bug location: " << Here() << \ | |
83 | Debug::Extra << "ignored exception: " << CurrentException); \ | |
64b66b76 | 84 | } |
774c051c | 85 | |
86 | #endif /* SQUID__TEXTEXCEPTION_H */ | |
f53969cc | 87 |