summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-errors.c
blob: dfc52fb32227576c8b41b7841d119326812fde3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-errors.c Error reporting
 *
 * Copyright (C) 2002  Red Hat Inc.
 * Copyright (C) 2003  CodeFactory AB
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "dbus-errors.h"
#include "dbus-internals.h"
#include <stdarg.h>
#include <stdio.h>

/**
 * @defgroup DBusErrors Error reporting
 * @ingroup  DBus
 * @brief Error reporting
 *
 * Types and functions related to reporting errors.
 *
 *
 * In essence D-BUS error reporting works as follows:
 *
 * @code
 * DBusResultCode result = DBUS_RESULT_SUCCESS;
 * dbus_some_function (arg1, arg2, &result);
 * if (result != DBUS_RESULT_SUCCESS)
 *   printf ("an error occurred\n");
 * @endcode
 *
 * @todo add docs with DBusError
 *
 * @todo add dbus_error_is_set() to check
 * whether an error is set.
 * 
 * @{
 */

typedef struct
{
  const char *name; /**< error name */
  char *message; /**< error message */

  unsigned int const_message : 1; /** Message is not owned by DBusError */

  unsigned int dummy2 : 1; /**< placeholder */
  unsigned int dummy3 : 1; /**< placeholder */
  unsigned int dummy4 : 1; /**< placeholder */
  unsigned int dummy5 : 1; /**< placeholder */

  void *padding1; /**< placeholder */
  
} DBusRealError;

/**
 * Set a result code at a result code location,
 * if code_address is not #NULL.
 *
 * @param code_address place to store the result code.
 * @param code the result code itself.
 */
void
dbus_set_result (DBusResultCode *code_address,
                 DBusResultCode  code)
{
  if (code_address)
    *code_address = code;
}

/**
 * Returns a string describing the given result code.
 *
 * @param code the result code to describe.
 * @returns a constant string describing the code.
 */
const char*
dbus_result_to_string (DBusResultCode code)
{
  /* This is a switch to the compiler will complain if we
   * aren't handling some codes
   */
  switch (code)
    {
    case DBUS_RESULT_SUCCESS:
      return "Success";
    case DBUS_RESULT_FAILED:
      return "Unknown error";
    case DBUS_RESULT_NO_MEMORY:
      return "Not enough memory available";
    case DBUS_RESULT_IO_ERROR:
      return "Error reading or writing data";
    case DBUS_RESULT_BAD_ADDRESS:
      return "Could not parse address";
    case DBUS_RESULT_NOT_SUPPORTED:
      return "Feature not supported";
    case DBUS_RESULT_LIMITS_EXCEEDED:
      return "Resource limits exceeded";
    case DBUS_RESULT_ACCESS_DENIED:
      return "Permission denied";
    case DBUS_RESULT_AUTH_FAILED:
      return "Could not authenticate to server";
    case DBUS_RESULT_NO_SERVER:
      return "No server";
    case DBUS_RESULT_TIMEOUT:
      return "Connection timed out";
    case DBUS_RESULT_NO_NETWORK:
      return "Network unavailable";
    case DBUS_RESULT_ADDRESS_IN_USE:
      return "Address already in use";
    case DBUS_RESULT_DISCONNECTED:
      return "Disconnected.";
    case DBUS_RESULT_INVALID_ARGS:
      return "Invalid argumemts.";
    case DBUS_RESULT_NO_REPLY:
      return "Did not get a reply message.";
    case DBUS_RESULT_FILE_NOT_FOUND:
      return "File doesn't exist.";
      
      /* no default, it would break our compiler warnings */
    }

  return "Invalid error code";
}

/**
 * Initializes a DBusError structure.
 * 
 * @todo calling dbus_error_init() in here is no good,
 * for the same reason a GError* has to be set to NULL
 * before you pass it in.
 *
 * @param error the DBusError.
 */
void
dbus_error_init (DBusError *error)
{
  DBusRealError *real;

  _dbus_assert (error != NULL);

  _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));

  real = (DBusRealError *)error;
  
  real->name = NULL;  
  real->message = NULL;

  real->const_message = TRUE;
}

/**
 * Frees an error created by dbus_error_init().
 *
 * @param error memory where the error is stored.
 */
void
dbus_error_free (DBusError *error)
{
  DBusRealError *real;

  real = (DBusRealError *)error;

  if (!real->const_message)
    dbus_free (real->message);
}

/**
 * Assigns an error name and message to a DBusError.
 * Does nothing if error is #NULL.
 *
 * @todo calling dbus_error_init() in here is no good,
 * for the same reason a GError* has to be set to NULL
 * before you pass it in.
 *
 * @param error the error.
 * @param name the error name (not copied!!!)
 * @param message the error message (not copied!!!)
 */
void
dbus_set_error_const (DBusError  *error,
		      const char *name,
		      const char *message)
{
  DBusRealError *real;

  if (error == NULL)
    return;

  dbus_error_init (error);
  
  real = (DBusRealError *)error;
  
  real->name = name;
  real->message = (char *)message;
  real->const_message = TRUE;
}

/**
 * Assigns an error name and message to a DBusError.
 * Does nothing if error is #NULL.
 *
 * If no memory can be allocated for the error message, 
 * an out-of-memory error message will be set instead.
 * 
 * @param error the error.
 * @param name the error name (not copied!!!)
 * @param format printf-style format string.
 */
void
dbus_set_error (DBusError  *error,
		const char *name,
		const char *format,
		...)
{
  DBusRealError *real;
  va_list args, args2;
  int message_length;
  char *message;
  char c;

  if (error == NULL)
    return;
  
  va_start (args, format);

  va_copy (args2, args);
  
  /* Measure the message length */
  message_length = vsnprintf (&c, 1,format, args) + 1;

  message = dbus_malloc (message_length);

  vsprintf (message, format, args2);
  
  if (!message)
    {
      dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY,
			    "Failed to allocate memory for error message.");
      return;
    }
  
  va_end (args);

  dbus_error_init (error);
  real = (DBusRealError *)error;
  
  real->name = name;
  real->message = message;
  real->const_message = FALSE;
}

/** @} */