TextException.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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__TEXTEXCEPTION_H
10 #define SQUID__TEXTEXCEPTION_H
11 
12 #include "base/Here.h"
13 
14 #include <stdexcept>
15 
16 class SBuf;
17 
19 class TextException: public std::runtime_error
20 {
21 
22 public:
23  TextException(const char *message, const SourceLocation &location):
24  std::runtime_error(message),
25  where(location)
26  {}
27 
28  TextException(SBuf message, const SourceLocation &location);
29 
30  TextException(const TextException &) = default;
32  TextException& operator=(const TextException &) = default;
33 
34  /* std::runtime_error API */
35  virtual ~TextException() throw() override;
36  virtual const char *what() const throw() override;
37 
39  SourceLocationId id() const { return where.id(); }
40 
42  std::ostream &print(std::ostream &) const;
43 
46 
47  // TODO: Add support for arbitrary (re)thrower-supplied details:
48  // std::tuple<Details...> details;
49 };
50 
52 std::ostream &CurrentException(std::ostream &);
53 
55 std::ostream &operator <<(std::ostream &, const TextException &);
56 
58 #define TexcHere(msg) TextException((msg), Here())
59 
63 #define Must3(condition, description, location) \
64  do { \
65  if (!(condition)) { \
66  const TextException Must_ex_(("check failed: " description), (location)); \
67  debugs(0, 3, Must_ex_.what()); \
68  throw Must_ex_; \
69  } \
70  } while (/*CONSTCOND*/ false)
71 
73 #define Must(condition) Must3((condition), #condition, Here())
74 
77 #define SWALLOW_EXCEPTIONS(code) \
78  try { \
79  code \
80  } catch (...) { \
81  debugs(0, DBG_IMPORTANT, "BUG: ignoring exception;" << \
82  Debug::Extra << "bug location: " << Here() << \
83  Debug::Extra << "ignored exception: " << CurrentException); \
84  }
85 
86 #endif /* SQUID__TEXTEXCEPTION_H */
87 
virtual ~TextException() override
TextException(TextException &&)=default
Definition: SBuf.h:87
TextException(const char *message, const SourceLocation &location)
Definition: TextException.h:23
TextException & operator=(const TextException &)=default
SourceLocationId id() const
Definition: Here.cc:67
SourceLocation where
code location related to the exception; usually the thrower location
Definition: TextException.h:45
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:30
virtual 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
TextException(const TextException &)=default

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors