Akonadi

exception.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "exceptionbase.h"
8 
9 #include <QString>
10 
11 #include <memory>
12 
13 using namespace Akonadi;
14 
15 class Akonadi::ExceptionPrivate
16 {
17 public:
18  explicit ExceptionPrivate(const QByteArray &what)
19  : what(what)
20  {
21  }
22 
23  QByteArray what;
24  QByteArray assembledWhat;
25 };
26 
27 Exception::Exception(const char *what)
28 {
29  try {
30  d = std::make_unique<ExceptionPrivate>(what);
31  } catch (...) {
32  }
33 }
34 
36 {
37  try {
38  d = std::make_unique<ExceptionPrivate>(what);
39  } catch (...) {
40  }
41 }
42 
44 {
45  try {
46  d = std::make_unique<ExceptionPrivate>(what.toUtf8());
47  } catch (...) {
48  }
49 }
50 
51 Exception::Exception(Exception &&) noexcept = default;
52 
53 Exception::~Exception() = default;
54 
55 QByteArray Exception::type() const
56 {
57  static constexpr char mytype[] = "Akonadi::Exception";
58  try {
59  return QByteArray::fromRawData("Akonadi::Exception", sizeof(mytype) - 1);
60  } catch (...) {
61  return QByteArray();
62  }
63 }
64 
65 const char *Exception::what() const noexcept
66 {
67  static constexpr char fallback[] = "<some exception was thrown during construction: message lost>";
68  if (!d) {
69  return fallback;
70  }
71  if (d->assembledWhat.isEmpty()) {
72  try {
73  d->assembledWhat = QByteArray(type() + ": " + d->what);
74  } catch (...) {
75  return "caught some exception while assembling Akonadi::Exception::what() return value";
76  }
77  }
78  return d->assembledWhat.constData();
79 }
80 
81 #define AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE(classname) \
82  Akonadi::classname::~classname() = default; \
83  QByteArray Akonadi::classname::type() const \
84  { \
85  static constexpr char mytype[] = "Akonadi::" #classname; \
86  try { \
87  return QByteArray::fromRawData(mytype, sizeof(mytype) - 1); \
88  } catch (...) { \
89  return QByteArray(); \
90  } \
91  }
92 
93 AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE(PayloadException)
94 
95 #undef AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE
QByteArray fromRawData(const char *data, int size)
virtual QByteArray type() const
Returns the type of this exception.
Definition: exception.cpp:55
const char * what() const noexcept override
Returns the error message associated with this exception.
Definition: exception.cpp:65
Base class for exceptions used by the Akonadi library.
Definition: exceptionbase.h:29
Exception(const char *what)
Creates a new exception with the error message what.
Definition: exception.cpp:27
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.