TextException.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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 
9 #ifndef SQUID_SRC_BASE_TEXTEXCEPTION_H
10 #define SQUID_SRC_BASE_TEXTEXCEPTION_H
11 
12 #include "base/Assure.h"
13 #include "base/Here.h"
14 
15 #include <stdexcept>
16 
17 class SBuf;
18 
20 class TextException: public std::runtime_error
21 {
22 
23 public:
24  TextException(const char *message, const SourceLocation &location):
25  std::runtime_error(message),
26  where(location)
27  {}
28 
29  TextException(SBuf message, const SourceLocation &location);
30 
31  TextException(const TextException &) = default;
32  TextException(TextException &&) = default;
33  TextException& operator=(const TextException &) = default;
34 
35  /* std::runtime_error API */
36  ~TextException() throw() override;
37  const char *what() const throw() override;
38 
40  SourceLocationId id() const { return where.id(); }
41 
43  std::ostream &print(std::ostream &) const;
44 
47 
48  // TODO: Add support for arbitrary (re)thrower-supplied details:
49  // std::tuple<Details...> details;
50 };
51 
53 std::ostream &CurrentException(std::ostream &);
54 
57 std::ostream &CurrentExceptionExtra(std::ostream &);
58 
60 std::ostream &operator <<(std::ostream &, const TextException &);
61 
63 #define TexcHere(msg) TextException((msg), Here())
64 
69 #define Must3(condition, description, location) \
70  Assure_(3, (condition), ("check failed: " description), (location))
71 
75 #define Must(condition) Must3((condition), #condition, Here())
76 
79 #define SWALLOW_EXCEPTIONS(code) \
80  try { \
81  code \
82  } catch (...) { \
83  debugs(0, DBG_IMPORTANT, "ERROR: Squid BUG: ignoring exception;" << \
84  Debug::Extra << "bug location: " << Here() << \
85  Debug::Extra << "ignored exception: " << CurrentException); \
86  }
87 
88 #endif /* SQUID_SRC_BASE_TEXTEXCEPTION_H */
89 
~TextException() override
Definition: SBuf.h:93
TextException(const char *message, const SourceLocation &location)
Definition: TextException.h:24
TextException & operator=(const TextException &)=default
std::ostream & CurrentExceptionExtra(std::ostream &)
SourceLocationId id() const
Definition: Here.cc:67
SourceLocation where
code location related to the exception; usually the thrower location
Definition: TextException.h:46
std::ostream & CurrentException(std::ostream &)
prints active (i.e., thrown but not yet handled) exception
uint32_t SourceLocationId
semi-uniquely identifies a source code location; stable across Squid runs
Definition: Here.h:18
std::ostream & operator<<(std::ostream &, const TextException &)
efficiently prints TextException
a source code location that is cheap to create, copy, and store
Definition: Here.h:29
const char * what() const override
an std::runtime_error with thrower location info
Definition: TextException.h:20
std::ostream & print(std::ostream &) const
dumps the exception text into the stream

 

Introduction

Documentation

Support

Miscellaneous