2003-02-13 Anders Carlsson <andersca@codefactory.se>
[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  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-errors.h"
24 #include "dbus-internals.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 /**
29  * @defgroup DBusErrors Error reporting
30  * @ingroup  DBus
31  * @brief Error reporting
32  *
33  * Types and functions related to reporting errors.
34  *
35  *
36  * In essence D-BUS error reporting works as follows:
37  *
38  * @code
39  * DBusResultCode result = DBUS_RESULT_SUCCESS;
40  * dbus_some_function (arg1, arg2, &result);
41  * if (result != DBUS_RESULT_SUCCESS)
42  *   printf ("an error occurred\n");
43  * @endcode
44  * 
45  * @{
46  */
47
48 typedef struct
49 {
50   const char *name; /**< error name */
51   char *message; /**< error message */
52
53   unsigned int const_message : 1; /** Message is not owned by DBusError */
54 } DBusRealError;
55
56 /**
57  * Set a result code at a result code location,
58  * if code_address is not #NULL.
59  *
60  * @param code_address place to store the result code.
61  * @param code the result code itself.
62  */
63 void
64 dbus_set_result (DBusResultCode *code_address,
65                  DBusResultCode  code)
66 {
67   if (code_address)
68     *code_address = code;
69 }
70
71 /**
72  * Returns a string describing the given result code.
73  *
74  * @param code the result code to describe.
75  * @returns a constant string describing the code.
76  */
77 const char*
78 dbus_result_to_string (DBusResultCode code)
79 {
80   /* This is a switch to the compiler will complain if we
81    * aren't handling some codes
82    */
83   switch (code)
84     {
85     case DBUS_RESULT_SUCCESS:
86       return "Success";
87     case DBUS_RESULT_FAILED:
88       return "Unknown error";
89     case DBUS_RESULT_NO_MEMORY:
90       return "Not enough memory available";
91     case DBUS_RESULT_IO_ERROR:
92       return "Error reading or writing data";
93     case DBUS_RESULT_BAD_ADDRESS:
94       return "Could not parse address";
95     case DBUS_RESULT_NOT_SUPPORTED:
96       return "Feature not supported";
97     case DBUS_RESULT_LIMITS_EXCEEDED:
98       return "Resource limits exceeded";
99     case DBUS_RESULT_ACCESS_DENIED:
100       return "Permission denied";
101     case DBUS_RESULT_AUTH_FAILED:
102       return "Could not authenticate to server";
103     case DBUS_RESULT_NO_SERVER:
104       return "No server";
105     case DBUS_RESULT_TIMEOUT:
106       return "Connection timed out";
107     case DBUS_RESULT_NO_NETWORK:
108       return "Network unavailable";
109     case DBUS_RESULT_ADDRESS_IN_USE:
110       return "Address already in use";
111     case DBUS_RESULT_DISCONNECTED:
112       return "Disconnected.";
113     case DBUS_RESULT_INVALID_FIELDS:
114       return "Invalid fields.";
115     case DBUS_RESULT_NO_REPLY:
116       return "Did not get a reply message.";
117     case DBUS_RESULT_FILE_NOT_FOUND:
118       return "File doesn't exist.";
119       
120       /* no default, it would break our compiler warnings */
121     }
122
123   return "Invalid error code";
124 }
125
126 void
127 dbus_error_init (DBusError *error)
128 {
129   DBusRealError *real;
130
131   _dbus_assert (error != NULL);
132
133   _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
134
135   real = (DBusRealError *)error;
136   
137   real->name = NULL;  
138   real->message = NULL;
139
140   real->const_message = TRUE;
141 }
142
143 void
144 dbus_error_free (DBusError *error)
145 {
146   DBusRealError *real;
147
148   real = (DBusRealError *)error;
149
150   if (!real->const_message)
151     dbus_free (real->message);
152 }
153
154 void
155 dbus_set_error_const (DBusError  *error,
156                       const char *name,
157                       const char *message)
158 {
159   DBusRealError *real;
160
161   if (error == NULL)
162     return;
163
164   dbus_error_init (error);
165   
166   real = (DBusRealError *)error;
167   
168   real->name = name;
169   real->message = (char *)message;
170   real->const_message = TRUE;
171 }
172
173 dbus_bool_t
174 dbus_set_error (DBusError  *error,
175                 const char *name,
176                 const char *format,
177                 ...)
178 {
179   DBusRealError *real;
180   va_list args, args2;
181   int message_length;
182   char *message;
183   char c;
184
185   if (error == NULL)
186     return TRUE;
187   
188   va_start (args, format);
189
190   va_copy (args2, args);
191   
192   /* Measure the message length */
193   message_length = vsnprintf (&c, 1,format, args) + 1;
194
195   message = dbus_malloc (message_length);
196
197   vsprintf (message, format, args2);
198   
199   if (!message)
200     return FALSE;
201
202   va_end (args);
203
204   dbus_error_init (error);
205   real = (DBusRealError *)error;
206   
207   real->name = name;
208   real->message = message;
209   real->const_message = FALSE;
210   
211   return TRUE;
212 }
213
214 /** @} */