2004-06-20 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-errors.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-errors.c Error reporting
3  *
4  * Copyright (C) 2002, 2004  Red Hat Inc.
5  * Copyright (C) 2003  CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.0
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #include "dbus-errors.h"
25 #include "dbus-internals.h"
26 #include "dbus-string.h"
27 #include "dbus-protocol.h"
28 #include <stdarg.h>
29 #include <string.h>
30
31 /**
32  * @defgroup DBusErrorInternals Error reporting internals
33  * @ingroup  DBusInternals
34  * @brief Error reporting internals
35  * @{
36  */
37  
38 /**
39  * Internals of DBusError
40  */
41 typedef struct
42 {
43   const char *name; /**< error name */
44   char *message; /**< error message */
45
46   unsigned int const_message : 1; /**< Message is not owned by DBusError */
47
48   unsigned int dummy2 : 1; /**< placeholder */
49   unsigned int dummy3 : 1; /**< placeholder */
50   unsigned int dummy4 : 1; /**< placeholder */
51   unsigned int dummy5 : 1; /**< placeholder */
52
53   void *padding1; /**< placeholder */
54   
55 } DBusRealError;
56
57 /**
58  * Returns a longer message describing an error name.
59  * If the error name is unknown, returns the name
60  * itself.
61  *
62  * @param error the error to describe
63  * @returns a constant string describing the error.
64  */
65 static const char*
66 message_from_error (const char *error)
67 {
68   if (strcmp (error, DBUS_ERROR_FAILED) == 0)
69     return "Unknown error";
70   else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
71     return "Not enough memory available";
72   else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
73     return "Error reading or writing data";
74   else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
75     return "Could not parse address";
76   else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
77     return "Feature not supported";
78   else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
79     return "Resource limits exceeded";
80   else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
81     return "Permission denied";
82   else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
83     return "Could not authenticate to server";
84   else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
85     return "No server available at address";
86   else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
87     return "Connection timed out";
88   else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
89     return "Network unavailable";
90   else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
91     return "Address already in use";
92   else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
93     return "Disconnected.";
94   else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
95     return "Invalid arguments.";
96   else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
97     return "Did not get a reply message.";
98   else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
99     return "File doesn't exist.";
100   else
101     return error;
102 }
103
104 /** @} */ /* End of internals */
105
106 /**
107  * @defgroup DBusErrors Error reporting
108  * @ingroup  DBus
109  * @brief Error reporting
110  *
111  * Types and functions related to reporting errors.
112  *
113  *
114  * In essence D-BUS error reporting works as follows:
115  *
116  * @code
117  * DBusError error;
118  * dbus_error_init (&error);
119  * dbus_some_function (arg1, arg2, &error);
120  * if (dbus_error_is_set (&error))
121  *   {
122  *     fprintf (stderr, "an error occurred: %s\n", error.message);
123  *     dbus_error_free (&error);
124  *   }
125  * @endcode
126  *
127  * There are some rules. An error passed to a D-BUS function must
128  * always be unset; you can't pass in an error that's already set.  If
129  * a function has a return code indicating whether an error occurred,
130  * and also a #DBusError parameter, then the error will always be set
131  * if and only if the return code indicates an error occurred. i.e.
132  * the return code and the error are never going to disagree.
133  *
134  * An error only needs to be freed if it's been set, not if
135  * it's merely been initialized.
136  *
137  * You can check the specific error that occurred using
138  * dbus_error_has_name().
139  * 
140  * @{
141  */
142
143 /**
144  * Initializes a DBusError structure. Does not allocate
145  * any memory; the error only needs to be freed
146  * if it is set at some point. 
147  *
148  * @param error the DBusError.
149  */
150 void
151 dbus_error_init (DBusError *error)
152 {
153   DBusRealError *real;
154
155   _dbus_return_if_fail (error != NULL);
156
157   _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
158
159   real = (DBusRealError *)error;
160   
161   real->name = NULL;  
162   real->message = NULL;
163
164   real->const_message = TRUE;
165 }
166
167 /**
168  * Frees an error that's been set (or just initialized),
169  * then reinitializes the error as in dbus_error_init().
170  *
171  * @param error memory where the error is stored.
172  */
173 void
174 dbus_error_free (DBusError *error)
175 {
176   DBusRealError *real;
177
178   _dbus_return_if_fail (error != NULL);
179   
180   real = (DBusRealError *)error;
181
182   if (!real->const_message)
183     dbus_free (real->message);
184
185   dbus_error_init (error);
186 }
187
188 /**
189  * Assigns an error name and message to a DBusError.  Does nothing if
190  * error is #NULL. The message may be NULL, which means a default
191  * message will be deduced from the name. If the error name is unknown
192  * to D-BUS the default message will be totally useless, though.
193  *
194  * @todo should be called dbus_error_set_const() 
195  * 
196  * @param error the error.
197  * @param name the error name (not copied!!!)
198  * @param message the error message (not copied!!!)
199  */
200 void
201 dbus_set_error_const (DBusError  *error,
202                       const char *name,
203                       const char *message)
204 {
205   DBusRealError *real;
206
207   _dbus_return_if_error_is_set (error);
208   _dbus_return_if_fail (name != NULL);
209   
210   if (error == NULL)
211     return;
212
213   _dbus_assert (error->name == NULL);
214   _dbus_assert (error->message == NULL);
215
216   if (message == NULL)
217     message = message_from_error (name);
218   
219   real = (DBusRealError *)error;
220   
221   real->name = name;
222   real->message = (char *)message;
223   real->const_message = TRUE;
224 }
225
226 /**
227  * Moves an error src into dest, freeing src and
228  * overwriting dest. Both src and dest must be initialized.
229  * src is reinitialized to an empty error. dest may not
230  * contain an existing error. If the destination is
231  * #NULL, just frees and reinits the source error.
232  * 
233  * @param src the source error
234  * @param dest the destination error or #NULL
235  */
236 void
237 dbus_move_error (DBusError *src,
238                  DBusError *dest)
239 {
240   _dbus_return_if_error_is_set (dest);
241
242   if (dest)
243     {
244       dbus_error_free (dest);
245       *dest = *src;
246       dbus_error_init (src);
247     }
248   else
249     dbus_error_free (src);
250 }
251
252 /**
253  * Checks whether the error is set and has the given
254  * name.
255  * @param error the error
256  * @param name the name
257  * @returns #TRUE if the given named error occurred
258  */
259 dbus_bool_t
260 dbus_error_has_name (const DBusError *error,
261                      const char      *name)
262 {
263   _dbus_return_val_if_fail (error != NULL, FALSE);
264   _dbus_return_val_if_fail (name != NULL, FALSE);
265
266   _dbus_assert ((error->name != NULL && error->message != NULL) ||
267                 (error->name == NULL && error->message == NULL));
268   
269   if (error->name != NULL)
270     {
271       DBusString str1, str2;
272       _dbus_string_init_const (&str1, error->name);
273       _dbus_string_init_const (&str2, name);
274       return _dbus_string_equal (&str1, &str2);
275     }
276   else
277     return FALSE;
278 }
279
280 /**
281  * Checks whether an error occurred (the error is set).
282  *
283  * @param error the error object
284  * @returns #TRUE if an error occurred
285  */
286 dbus_bool_t
287 dbus_error_is_set (const DBusError *error)
288 {
289   _dbus_return_val_if_fail (error != NULL, FALSE);  
290   _dbus_assert ((error->name != NULL && error->message != NULL) ||
291                 (error->name == NULL && error->message == NULL));
292   return error->name != NULL;
293 }
294
295 /**
296  * Assigns an error name and message to a DBusError.
297  * Does nothing if error is #NULL.
298  *
299  * The format may be NULL, which means a default message will be
300  * deduced from the name. If the error name is unknown to D-BUS the
301  * default message will be totally useless, though.
302  *
303  * If no memory can be allocated for the error message, 
304  * an out-of-memory error message will be set instead.
305  *
306  * @todo should be called dbus_error_set()
307  *
308  * @param error the error.
309  * @param name the error name (not copied!!!)
310  * @param format printf-style format string.
311  */
312 void
313 dbus_set_error (DBusError  *error,
314                 const char *name,
315                 const char *format,
316                 ...)
317 {
318   DBusRealError *real;
319   DBusString str;
320   va_list args;
321   
322   if (error == NULL)
323     return;
324
325   /* it's a bug to pile up errors */
326   _dbus_return_if_error_is_set (error);
327   _dbus_return_if_fail (name != NULL);
328   
329   _dbus_assert (error->name == NULL);
330   _dbus_assert (error->message == NULL);
331
332   if (!_dbus_string_init (&str))
333     goto nomem;
334   
335   if (format == NULL)
336     {
337       if (!_dbus_string_append (&str,
338                                 message_from_error (name)))
339         {
340           _dbus_string_free (&str);
341           goto nomem;
342         }
343     }
344   else
345     {
346       va_start (args, format);
347       if (!_dbus_string_append_printf_valist (&str, format, args))
348         {
349           _dbus_string_free (&str);
350           goto nomem;
351         }
352       va_end (args);
353     }
354
355   real = (DBusRealError *)error;
356
357   if (!_dbus_string_steal_data (&str, &real->message))
358     {
359       _dbus_string_free (&str);
360       goto nomem;
361     }
362   
363   real->name = name;
364   real->const_message = FALSE;
365
366   _dbus_string_free (&str);
367
368   return;
369   
370  nomem:
371   dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY, NULL);      
372 }
373
374 /** @} */ /* End public API */