1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDBus module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qdbuserror.h"
45 #include <qvarlengtharray.h>
47 #include <qdbus_symbols_p.h>
48 #include "qdbusmessage.h"
49 #include "qdbusmessage_p.h"
54 * Use the following Perl script to generate the error string index list:
55 ===== PERL SCRIPT ====
56 print "static const char errorMessages_string[] =\n";
62 $sizes[$i++] = $counter;
63 $counter += 1 + length $_;
65 print " \"\\0\";\n\nstatic const int errorMessages_indices[] = {\n ";
66 for ($j = 0; $j < $i; ++$j) {
67 printf "$sizes[$j], ";
70 ===== PERL SCRIPT ====
72 * The input data is as follows:
74 org.freedesktop.DBus.Error.Failed
75 org.freedesktop.DBus.Error.NoMemory
76 org.freedesktop.DBus.Error.ServiceUnknown
77 org.freedesktop.DBus.Error.NoReply
78 org.freedesktop.DBus.Error.BadAddress
79 org.freedesktop.DBus.Error.NotSupported
80 org.freedesktop.DBus.Error.LimitsExceeded
81 org.freedesktop.DBus.Error.AccessDenied
82 org.freedesktop.DBus.Error.NoServer
83 org.freedesktop.DBus.Error.Timeout
84 org.freedesktop.DBus.Error.NoNetwork
85 org.freedesktop.DBus.Error.AddressInUse
86 org.freedesktop.DBus.Error.Disconnected
87 org.freedesktop.DBus.Error.InvalidArgs
88 org.freedesktop.DBus.Error.UnknownMethod
89 org.freedesktop.DBus.Error.TimedOut
90 org.freedesktop.DBus.Error.InvalidSignature
91 org.freedesktop.DBus.Error.UnknownInterface
92 com.trolltech.QtDBus.Error.InternalError
93 org.freedesktop.DBus.Error.UnknownObject
96 // in the same order as KnownErrors!
97 static const char errorMessages_string
[] =
99 "org.freedesktop.DBus.Error.Failed\0"
100 "org.freedesktop.DBus.Error.NoMemory\0"
101 "org.freedesktop.DBus.Error.ServiceUnknown\0"
102 "org.freedesktop.DBus.Error.NoReply\0"
103 "org.freedesktop.DBus.Error.BadAddress\0"
104 "org.freedesktop.DBus.Error.NotSupported\0"
105 "org.freedesktop.DBus.Error.LimitsExceeded\0"
106 "org.freedesktop.DBus.Error.AccessDenied\0"
107 "org.freedesktop.DBus.Error.NoServer\0"
108 "org.freedesktop.DBus.Error.Timeout\0"
109 "org.freedesktop.DBus.Error.NoNetwork\0"
110 "org.freedesktop.DBus.Error.AddressInUse\0"
111 "org.freedesktop.DBus.Error.Disconnected\0"
112 "org.freedesktop.DBus.Error.InvalidArgs\0"
113 "org.freedesktop.DBus.Error.UnknownMethod\0"
114 "org.freedesktop.DBus.Error.TimedOut\0"
115 "org.freedesktop.DBus.Error.InvalidSignature\0"
116 "org.freedesktop.DBus.Error.UnknownInterface\0"
117 "com.trolltech.QtDBus.Error.InternalError\0"
118 "org.freedesktop.DBus.Error.UnknownObject\0"
121 static const int errorMessages_indices
[] = {
122 0, 6, 40, 76, 118, 153, 191, 231,
123 273, 313, 349, 384, 421, 461, 501, 540,
124 581, 617, 661, 705, 746, 0
127 static const int errorMessages_count
= sizeof errorMessages_indices
/
128 sizeof errorMessages_indices
[0];
130 static inline const char *get(QDBusError::ErrorType code
)
132 int intcode
= qBound(0, int(code
) - int(QDBusError::Other
), errorMessages_count
);
133 return errorMessages_string
+ errorMessages_indices
[intcode
];
136 static inline QDBusError::ErrorType
get(const char *name
)
139 return QDBusError::NoError
;
140 for (int i
= 0; i
< errorMessages_count
; ++i
)
141 if (strcmp(name
, errorMessages_string
+ errorMessages_indices
[i
]) == 0)
142 return QDBusError::ErrorType(i
+ int(QDBusError::Other
));
143 return QDBusError::Other
;
151 \brief The QDBusError class represents an error received from the
152 D-Bus bus or from remote applications found in the bus.
154 When dealing with the D-Bus bus service or with remote
155 applications over D-Bus, a number of error conditions can
156 happen. This error conditions are sometimes signalled by a
157 returned error value or by a QDBusError.
159 C++ and Java exceptions are a valid analogy for D-Bus errors:
160 instead of returning normally with a return value, remote
161 applications and the bus may decide to throw an error
162 condition. However, the QtDBus implementation does not use the C++
163 exception-throwing mechanism, so you will receive QDBusErrors in
164 the return reply (see QDBusReply::error()).
166 QDBusError objects are used to inspect the error name and message
167 as received from the bus and remote applications. You should not
168 create such objects yourself to signal error conditions when
169 called from D-Bus: instead, use QDBusMessage::createError() and
170 QDBusConnection::send().
172 \sa QDBusConnection::send(), QDBusMessage, QDBusReply
176 \enum QDBusError::ErrorType
178 In order to facilitate verification of the most common D-Bus errors generated by the D-Bus
179 implementation and by the bus daemon itself, QDBusError can be compared to a set of pre-defined
182 \value NoError QDBusError is invalid (i.e., the call succeeded)
183 \value Other QDBusError contains an error that is one of the well-known ones
184 \value Failed The call failed (\c org.freedesktop.DBus.Error.Failed)
185 \value NoMemory Out of memory (\c org.freedesktop.DBus.Error.NoMemory)
186 \value ServiceUnknown The called service is not known
187 (\c org.freedesktop.DBus.Error.ServiceUnknown)
188 \value NoReply The called method did not reply within the specified timeout
189 (\c org.freedesktop.DBus.Error.NoReply)
190 \value BadAddress The address given is not valid
191 (\c org.freedesktop.DBus.Error.BadAddress)
192 \value NotSupported The call/operation is not supported
193 (\c org.freedesktop.DBus.Error.NotSupported)
194 \value LimitsExceeded The limits allocated to this process/call/connection exceeded the
195 pre-defined values (\c org.freedesktop.DBus.Error.LimitsExceeded)
196 \value AccessDenied The call/operation tried to access a resource it isn't allowed to
197 (\c org.freedesktop.DBus.Error.AccessDenied)
198 \value NoServer \e {Documentation doesn't say what this is for}
199 (\c org.freedesktop.DBus.Error.NoServer)
200 \value Timeout \e {Documentation doesn't say what this is for or how it's used}
201 (\c org.freedesktop.DBus.Error.Timeout)
202 \value NoNetwork \e {Documentation doesn't say what this is for}
203 (\c org.freedesktop.DBus.Error.NoNetwork)
204 \value AddressInUse QDBusServer tried to bind to an address that is already in use
205 (\c org.freedesktop.DBus.Error.AddressInUse)
206 \value Disconnected The call/process/message was sent after QDBusConnection disconnected
207 (\c org.freedesktop.DBus.Error.Disconnected)
208 \value InvalidArgs The arguments passed to this call/operation are not valid
209 (\c org.freedesktop.DBus.Error.InvalidArgs)
210 \value UnknownMethod The method called was not found in this object/interface with the
211 given parameters (\c org.freedesktop.DBus.Error.UnknownMethod)
212 \value TimedOut \e {Documentation doesn't say...}
213 (\c org.freedesktop.DBus.Error.TimedOut)
214 \value InvalidSignature The type signature is not valid or compatible
215 (\c org.freedesktop.DBus.Error.InvalidSignature)
216 \value UnknownInterface The interface is not known
217 \value InternalError An internal error occurred
218 (\c com.trolltech.QtDBus.Error.InternalError)
219 \value UnknownObject The remote object could not be found.
225 Constructs a QDBusError from a DBusError structure.
227 QDBusError::QDBusError(const DBusError
*error
)
230 if (!error
|| !q_dbus_error_is_set(error
))
233 code
= ::get(error
->name
);
234 msg
= QString::fromUtf8(error
->message
);
235 nm
= QString::fromUtf8(error
->name
);
240 Constructs a QDBusError from a QDBusMessage.
242 QDBusError::QDBusError(const QDBusMessage
&qdmsg
)
245 if (qdmsg
.type() != QDBusMessage::ErrorMessage
)
248 code
= ::get(qdmsg
.errorName().toUtf8().constData());
249 nm
= qdmsg
.errorName();
250 msg
= qdmsg
.errorMessage();
255 Constructs a QDBusError from a well-known error code
257 QDBusError::QDBusError(ErrorType error
, const QString
&mess
)
260 nm
= QLatin1String(::get(error
));
266 Constructs a QDBusError from another QDBusError object
268 QDBusError::QDBusError(const QDBusError
&other
)
269 : code(other
.code
), msg(other
.msg
), nm(other
.nm
)
278 QDBusError
&QDBusError::operator=(const QDBusError
&other
)
287 Returns this error's ErrorType.
292 QDBusError::ErrorType
QDBusError::type() const
298 Returns this error's name. Error names are similar to D-Bus Interface names, like
299 \c org.freedesktop.DBus.InvalidArgs.
304 QString
QDBusError::name() const
310 Returns the message that the callee associated with this error. Error messages are
311 implementation defined and usually contain a human-readable error code, though this does not
312 mean it is suitable for your end-users.
315 QString
QDBusError::message() const
321 Returns true if this is a valid error condition (i.e., if there was an error),
325 bool QDBusError::isValid() const
327 return (code
!= NoError
);
332 Returns the error name associated with error condition \a error.
334 QString
QDBusError::errorString(ErrorType error
)
336 return QLatin1String(::get(error
));
339 #ifndef QT_NO_DEBUG_STREAM
340 QDebug
operator<<(QDebug dbg
, const QDBusError
&msg
)
342 dbg
.nospace() << "QDBusError(" << msg
.name() << ", " << msg
.message() << ")";