Akonadi

exception.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org>
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
13using namespace Akonadi;
14
15class Akonadi::ExceptionPrivate
16{
17public:
18 explicit ExceptionPrivate(const QByteArray &what)
19 : what(what)
20 {
21 }
22
23 QByteArray what;
24 QByteArray assembledWhat;
25};
26
27Exception::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
51Exception::Exception(Exception &&) noexcept = default;
52
53Exception::~Exception() = default;
54
55QByteArray 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
65const 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
93AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE(PayloadException)
94
95#undef AKONADI_EXCEPTION_IMPLEMENT_TRIVIAL_INSTANCE
Base class for exceptions used by the Akonadi library.
virtual QByteArray type() const
Returns the type of this exception.
Definition exception.cpp:55
Exception(const char *what)
Creates a new exception with the error message what.
Definition exception.cpp:27
const char * what() const noexcept override
Returns the error message associated with this exception.
Definition exception.cpp:65
Helper integration between Akonadi and Qt.
QByteArray fromRawData(const char *data, qsizetype size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.