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#if QT_VERSION <= QT_VERSION_CHECK(7, 0, 0)
165 // ### This class has an implicit (therefore inline) destructor
166 // so the following field cannot be used.
167 Q_UNUSED(unused);
168#endif
169}
170
171#ifndef QT_BOOTSTRAPPED
172/*!
173 \internal
174 Constructs a QDBusError from a DBusError structure.
175*/
176QDBusError::QDBusError(const DBusError *error)
177 : code(NoError)
178{
179 if (!error || !q_dbus_error_is_set(error))
180 return;
181
182 code = get(error->name);
183 msg = QString::fromUtf8(error->message);
184 nm = QString::fromUtf8(error->name);
185}
186
187/*!
188 \internal
189 Constructs a QDBusError from a QDBusMessage.
190*/
191QDBusError::QDBusError(const QDBusMessage &qdmsg)
192 : code(NoError)
193{
194 if (qdmsg.type() != QDBusMessage::ErrorMessage)
195 return;
196
197 code = get(qdmsg.errorName().toUtf8().constData());
198 nm = qdmsg.errorName();
199 msg = qdmsg.errorMessage();
200}
201#endif
202
203/*!
204 \internal
205 Constructs a QDBusError from a well-known error code
206*/
207QDBusError::QDBusError(ErrorType error, const QString &mess)
208 : code(error)
209{
210 nm = QLatin1StringView(errorMessages[error]);
211 msg = mess;
212}
213
214/*!
215 \internal
216 Constructs a QDBusError from another QDBusError object
217*/
218QDBusError::QDBusError(const QDBusError &other)
219 : code(other.code), msg(other.msg), nm(other.nm)
220{
221}
222
223/*!
224 \internal
225 Assignment operator
226*/
227
228QDBusError &QDBusError::operator=(const QDBusError &other)
229{
230 code = other.code;
231 msg = other.msg;
232 nm = other.nm;
233 return *this;
234}
235
236#ifndef QT_BOOTSTRAPPED
237/*!
238 \internal
239 Assignment operator from a QDBusMessage
240*/
241QDBusError &QDBusError::operator=(const QDBusMessage &qdmsg)
242{
243 if (qdmsg.type() == QDBusMessage::ErrorMessage) {
244 code = get(qdmsg.errorName().toUtf8().constData());
245 nm = qdmsg.errorName();
246 msg = qdmsg.errorMessage();
247 } else {
248 code =NoError;
249 nm.clear();
250 msg.clear();
251 }
252 return *this;
253}
254#endif
255
256/*!
257 Returns this error's ErrorType.
258
259 \sa ErrorType
260*/
261
262QDBusError::ErrorType QDBusError::type() const
263{
264 return code;
265}
266
267/*!
268 Returns this error's name. Error names are similar to D-Bus Interface names, like
269 \c org.freedesktop.DBus.InvalidArgs.
270
271 \sa type()
272*/
273
274QString QDBusError::name() const
275{
276 return nm;
277}
278
279/*!
280 Returns the message that the callee associated with this error. Error messages are
281 implementation defined and usually contain a human-readable error code, though this does not
282 mean it is suitable for your end-users.
283*/
284
285QString QDBusError::message() const
286{
287 return msg;
288}
289
290/*!
291 Returns \c true if this is a valid error condition (i.e., if there was an error),
292 otherwise false.
293*/
294
295bool QDBusError::isValid() const
296{
297 return (code != NoError);
298}
299
300/*!
301 \since 4.3
302 Returns the error name associated with error condition \a error.
303*/
304QString QDBusError::errorString(ErrorType error)
305{
306 return QLatin1StringView(errorMessages[error]);
307}
308
309#ifndef QT_NO_DEBUG_STREAM
310QDebug operator<<(QDebug dbg, const QDBusError &msg)
311{
312 QDebugStateSaver saver(dbg);
313 dbg.nospace() << "QDBusError(" << msg.name() << ", " << msg.message() << ')';
314 return dbg;
315}
316#endif
317
318/*!
319 \fn void QDBusError::swap(QDBusError &other)
320 \memberswap{error}
321*/
322
323QT_END_NAMESPACE
324
325#include "moc_qdbuserror.cpp"
326
327#endif // QT_NO_DBUS
328
329/*
330MSVC2015 has the warning C4503 at the end of the file:
331QtPrivate::StaticStringBuilder<QtPrivate::IndexesList<...> - decorated name length exceeded, name was truncated
332It is used by qOffsetStringArray in a constexpr evaluation and this code does not exist in the object file,
333but we still have the warning or even error with -WX flag
334*/
335QT_WARNING_DISABLE_MSVC(4503)
Combined button and popup list for selecting options.
static QDBusError::ErrorType get(const char *name)
QDebug operator<<(QDebug dbg, const QDBusError &msg)