Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdbuserror.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qdbuserror.h"
6
7#include <qdebug.h>
8#include <qvarlengtharray.h>
9#include <private/qoffsetstringarray_p.h>
10
11#ifndef QT_BOOTSTRAPPED
13#include "qdbusmessage.h"
14#include "qdbusmessage_p.h"
15#endif
16
17#ifndef QT_NO_DBUS
18
20
21QT_IMPL_METATYPE_EXTERN(QDBusError)
22
23static constexpr const auto errorMessages = qOffsetStringArray(
24 "NoError",
25 "other",
26 "org.freedesktop.DBus.Error.Failed",
27 "org.freedesktop.DBus.Error.NoMemory",
28 "org.freedesktop.DBus.Error.ServiceUnknown",
29 "org.freedesktop.DBus.Error.NoReply",
30 "org.freedesktop.DBus.Error.BadAddress",
31 "org.freedesktop.DBus.Error.NotSupported",
32 "org.freedesktop.DBus.Error.LimitsExceeded",
33 "org.freedesktop.DBus.Error.AccessDenied",
34 "org.freedesktop.DBus.Error.NoServer",
35 "org.freedesktop.DBus.Error.Timeout",
36 "org.freedesktop.DBus.Error.NoNetwork",
37 "org.freedesktop.DBus.Error.AddressInUse",
38 "org.freedesktop.DBus.Error.Disconnected",
39 "org.freedesktop.DBus.Error.InvalidArgs",
40 "org.freedesktop.DBus.Error.UnknownMethod",
41 "org.freedesktop.DBus.Error.TimedOut",
42 "org.freedesktop.DBus.Error.InvalidSignature",
43 "org.freedesktop.DBus.Error.UnknownInterface",
44 "org.freedesktop.DBus.Error.UnknownObject",
45 "org.freedesktop.DBus.Error.UnknownProperty",
46 "org.freedesktop.DBus.Error.PropertyReadOnly",
47 "org.qtproject.QtDBus.Error.InternalError",
48 "org.qtproject.QtDBus.Error.InvalidService",
49 "org.qtproject.QtDBus.Error.InvalidObjectPath",
50 "org.qtproject.QtDBus.Error.InvalidInterface",
51 "org.qtproject.QtDBus.Error.InvalidMember",
52 ""
53);
54
55#ifndef QT_BOOTSTRAPPED
56static inline QDBusError::ErrorType get(const char *name)
57{
58 if (!name || !*name)
59 return QDBusError::NoError;
60 for (int i = 0; i < errorMessages.count(); ++i)
61 if (strcmp(name, errorMessages.at(i)) == 0)
62 return QDBusError::ErrorType(i);
63 return QDBusError::Other;
64}
65#endif
66
67/*!
68 \class QDBusError
69 \inmodule QtDBus
70 \since 4.2
71
72 \brief The QDBusError class represents an error received from the
73 D-Bus bus or from remote applications found in the bus.
74
75 When dealing with the D-Bus bus service or with remote
76 applications over D-Bus, a number of error conditions can
77 happen. This error conditions are sometimes signalled by a
78 returned error value or by a QDBusError.
79
80 C++ and Java exceptions are a valid analogy for D-Bus errors:
81 instead of returning normally with a return value, remote
82 applications and the bus may decide to throw an error
83 condition. However, the Qt D-Bus implementation does not use the C++
84 exception-throwing mechanism, so you will receive QDBusErrors in
85 the return reply (see QDBusReply::error()).
86
87 QDBusError objects are used to inspect the error name and message
88 as received from the bus and remote applications. You should not
89 create such objects yourself to signal error conditions when
90 called from D-Bus: instead, use QDBusMessage::createError() and
91 QDBusConnection::send().
92
93 \sa QDBusConnection::send(), QDBusMessage, QDBusReply
94*/
95
96/*!
97 \enum QDBusError::ErrorType
98
99 In order to facilitate verification of the most common D-Bus errors generated by the D-Bus
100 implementation and by the bus daemon itself, QDBusError can be compared to a set of pre-defined
101 values:
102
103 \value NoError QDBusError is invalid (i.e., the call succeeded)
104 \value Other QDBusError contains an error that is not one of the well-known ones
105 \value Failed The call failed (\c org.freedesktop.DBus.Error.Failed)
106 \value NoMemory Out of memory (\c org.freedesktop.DBus.Error.NoMemory)
107 \value ServiceUnknown The called service is not known
108 (\c org.freedesktop.DBus.Error.ServiceUnknown)
109 \value NoReply The called method did not reply within the specified timeout
110 (\c org.freedesktop.DBus.Error.NoReply)
111 \value BadAddress The address given is not valid
112 (\c org.freedesktop.DBus.Error.BadAddress)
113 \value NotSupported The call/operation is not supported
114 (\c org.freedesktop.DBus.Error.NotSupported)
115 \value LimitsExceeded The limits allocated to this process/call/connection exceeded the
116 pre-defined values (\c org.freedesktop.DBus.Error.LimitsExceeded)
117 \value AccessDenied The call/operation tried to access a resource it isn't allowed to
118 (\c org.freedesktop.DBus.Error.AccessDenied)
119 \value NoServer \e {Documentation doesn't say what this is for}
120 (\c org.freedesktop.DBus.Error.NoServer)
121 \value Timeout \e {Documentation doesn't say what this is for or how it's used}
122 (\c org.freedesktop.DBus.Error.Timeout)
123 \value NoNetwork \e {Documentation doesn't say what this is for}
124 (\c org.freedesktop.DBus.Error.NoNetwork)
125 \value AddressInUse QDBusServer tried to bind to an address that is already in use
126 (\c org.freedesktop.DBus.Error.AddressInUse)
127 \value Disconnected The call/process/message was sent after QDBusConnection disconnected
128 (\c org.freedesktop.DBus.Error.Disconnected)
129 \value InvalidArgs The arguments passed to this call/operation are not valid
130 (\c org.freedesktop.DBus.Error.InvalidArgs)
131 \value UnknownMethod The method called was not found in this object/interface with the
132 given parameters (\c org.freedesktop.DBus.Error.UnknownMethod)
133 \value TimedOut \e {Documentation doesn't say...}
134 (\c org.freedesktop.DBus.Error.TimedOut)
135 \value InvalidSignature The type signature is not valid or compatible
136 (\c org.freedesktop.DBus.Error.InvalidSignature)
137 \value UnknownInterface The interface is not known in this object
138 (\c org.freedesktop.DBus.Error.UnknownInterface)
139 \value UnknownObject The object path points to an object that does not exist
140 (\c org.freedesktop.DBus.Error.UnknownObject)
141 \value UnknownProperty The property does not exist in this interface
142 (\c org.freedesktop.DBus.Error.UnknownProperty)
143 \value PropertyReadOnly The property set failed because the property is read-only
144 (\c org.freedesktop.DBus.Error.PropertyReadOnly)
145
146 \value InternalError An internal error occurred
147
148 \value InvalidObjectPath The object path provided is invalid.
149
150 \value InvalidService The service requested is invalid.
151
152 \value InvalidMember The member is invalid.
153
154 \value InvalidInterface The interface is invalid.
155*/
156
157/*!
158 \internal
159 Constructs a QDBusError that represents no error.
160*/
161QDBusError::QDBusError()
162 : code(NoError)
163{
164 // ### This class has an implicit (therefore inline) destructor
165 // so the following field cannot be used.
166 Q_UNUSED(unused);
167}
168
169#ifndef QT_BOOTSTRAPPED
170/*!
171 \internal
172 Constructs a QDBusError from a DBusError structure.
173*/
174QDBusError::QDBusError(const DBusError *error)
175 : code(NoError)
176{
177 if (!error || !q_dbus_error_is_set(error))
178 return;
179
180 code = get(error->name);
181 msg = QString::fromUtf8(error->message);
182 nm = QString::fromUtf8(error->name);
183}
184
185/*!
186 \internal
187 Constructs a QDBusError from a QDBusMessage.
188*/
189QDBusError::QDBusError(const QDBusMessage &qdmsg)
190 : code(NoError)
191{
192 if (qdmsg.type() != QDBusMessage::ErrorMessage)
193 return;
194
195 code = get(qdmsg.errorName().toUtf8().constData());
196 nm = qdmsg.errorName();
197 msg = qdmsg.errorMessage();
198}
199#endif
200
201/*!
202 \internal
203 Constructs a QDBusError from a well-known error code
204*/
205QDBusError::QDBusError(ErrorType error, const QString &mess)
206 : code(error)
207{
208 nm = QLatin1StringView(errorMessages[error]);
209 msg = mess;
210}
211
212/*!
213 \internal
214 Constructs a QDBusError from another QDBusError object
215*/
216QDBusError::QDBusError(const QDBusError &other)
217 : code(other.code), msg(other.msg), nm(other.nm)
218{
219}
220
221/*!
222 \internal
223 Assignment operator
224*/
225
226QDBusError &QDBusError::operator=(const QDBusError &other)
227{
228 code = other.code;
229 msg = other.msg;
230 nm = other.nm;
231 return *this;
232}
233
234#ifndef QT_BOOTSTRAPPED
235/*!
236 \internal
237 Assignment operator from a QDBusMessage
238*/
239QDBusError &QDBusError::operator=(const QDBusMessage &qdmsg)
240{
241 if (qdmsg.type() == QDBusMessage::ErrorMessage) {
242 code = get(qdmsg.errorName().toUtf8().constData());
243 nm = qdmsg.errorName();
244 msg = qdmsg.errorMessage();
245 } else {
246 code =NoError;
247 nm.clear();
248 msg.clear();
249 }
250 return *this;
251}
252#endif
253
254/*!
255 Returns this error's ErrorType.
256
257 \sa ErrorType
258*/
259
260QDBusError::ErrorType QDBusError::type() const
261{
262 return code;
263}
264
265/*!
266 Returns this error's name. Error names are similar to D-Bus Interface names, like
267 \c org.freedesktop.DBus.InvalidArgs.
268
269 \sa type()
270*/
271
272QString QDBusError::name() const
273{
274 return nm;
275}
276
277/*!
278 Returns the message that the callee associated with this error. Error messages are
279 implementation defined and usually contain a human-readable error code, though this does not
280 mean it is suitable for your end-users.
281*/
282
283QString QDBusError::message() const
284{
285 return msg;
286}
287
288/*!
289 Returns \c true if this is a valid error condition (i.e., if there was an error),
290 otherwise false.
291*/
292
293bool QDBusError::isValid() const
294{
295 return (code != NoError);
296}
297
298/*!
299 \since 4.3
300 Returns the error name associated with error condition \a error.
301*/
302QString QDBusError::errorString(ErrorType error)
303{
304 return QLatin1StringView(errorMessages[error]);
305}
306
307#ifndef QT_NO_DEBUG_STREAM
308QDebug operator<<(QDebug dbg, const QDBusError &msg)
309{
310 QDebugStateSaver saver(dbg);
311 dbg.nospace() << "QDBusError(" << msg.name() << ", " << msg.message() << ')';
312 return dbg;
313}
314#endif
315
316/*!
317 \fn void QDBusError::swap(QDBusError &other)
318 \memberswap{error}
319*/
320
321QT_END_NAMESPACE
322
323#include "moc_qdbuserror.cpp"
324
325#endif // QT_NO_DBUS
326
327/*
328MSVC2015 has the warning C4503 at the end of the file:
329QtPrivate::StaticStringBuilder<QtPrivate::IndexesList<...> - decorated name length exceeded, name was truncated
330It is used by qOffsetStringArray in a constexpr evaluation and this code does not exist in the object file,
331but we still have the warning or even error with -WX flag
332*/
333QT_WARNING_DISABLE_MSVC(4503)
static QDBusError::ErrorType get(const char *name)
QDebug operator<<(QDebug dbg, const QDBusError &msg)