1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-errors.c Error reporting
4 * Copyright (C) 2002 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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.
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.
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
23 #include "dbus-errors.h"
24 #include "dbus-internals.h"
29 * @defgroup DBusErrors Error reporting
31 * @brief Error reporting
33 * Types and functions related to reporting errors.
36 * In essence D-BUS error reporting works as follows:
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");
50 const char *name; /**< error name */
51 char *message; /**< error message */
53 unsigned int const_message : 1; /** Message is not owned by DBusError */
57 * Set a result code at a result code location,
58 * if code_address is not #NULL.
60 * @param code_address place to store the result code.
61 * @param code the result code itself.
64 dbus_set_result (DBusResultCode *code_address,
72 * Returns a string describing the given result code.
74 * @param code the result code to describe.
75 * @returns a constant string describing the code.
78 dbus_result_to_string (DBusResultCode code)
80 /* This is a switch to the compiler will complain if we
81 * aren't handling some codes
85 case DBUS_RESULT_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:
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.";
120 /* no default, it would break our compiler warnings */
123 return "Invalid error code";
127 dbus_error_init (DBusError *error)
131 _dbus_assert (error != NULL);
133 _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
135 real = (DBusRealError *)error;
138 real->message = NULL;
140 real->const_message = TRUE;
144 dbus_error_free (DBusError *error)
148 real = (DBusRealError *)error;
150 if (!real->const_message)
151 dbus_free (real->message);
155 dbus_set_error_const (DBusError *error,
164 dbus_error_init (error);
166 real = (DBusRealError *)error;
169 real->message = (char *)message;
170 real->const_message = TRUE;
174 dbus_set_error (DBusError *error,
188 va_start (args, format);
190 va_copy (args2, args);
192 /* Measure the message length */
193 message_length = vsnprintf (&c, 1,format, args) + 1;
195 message = dbus_malloc (message_length);
197 vsprintf (message, format, args2);
204 dbus_error_init (error);
205 real = (DBusRealError *)error;
208 real->message = message;
209 real->const_message = FALSE;