OpenSync  0.22
opensync_error.c
1 /*
2  * libopensync - A synchronization framework
3  * Copyright (C) 2004-2005 Armin Bauer <armin.bauer@opensync.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #include "opensync.h"
22 #include "opensync_internals.h"
23 
33 
40 static const char *osync_error_name_from_type(OSyncErrorType type)
41 {
42  switch (type) {
43  case OSYNC_NO_ERROR:
44  return "NoError";
45  case OSYNC_ERROR_GENERIC:
46  return "UnknownError";
47  case OSYNC_ERROR_IO_ERROR:
48  return "IOError";
49  case OSYNC_ERROR_NOT_SUPPORTED:
50  return "NotSupported";
51  case OSYNC_ERROR_TIMEOUT:
52  return "Timeout";
53  case OSYNC_ERROR_DISCONNECTED:
54  return "Disconnected";
55  case OSYNC_ERROR_FILE_NOT_FOUND:
56  return "FileNotFound";
57  default:
58  return "UnspecifiedError";
59  }
60 }
61 
70 void osync_error_set_vargs(OSyncError **error, OSyncErrorType type, const char *format, va_list args)
71 {
72  if (!error || !format)
73  return;
74  if (osync_error_is_set(error))
75  osync_error_free(error);
76  osync_assert(osync_error_is_set(error) == FALSE);
77 
78  char buffer[1024];
79  memset(buffer, 0, sizeof(buffer));
80  *error = g_malloc0(sizeof(OSyncError));
81  g_vsnprintf(buffer, 1024, format, args);
82 
83  (*error)->message = g_strdup(buffer);
84  (*error)->type = type;
85  return;
86 }
87 
97 
98 
105 const char *osync_error_get_name(OSyncError **error)
106 {
107  osync_return_val_if_fail(error != NULL, NULL);
108  if (!*error)
109  return osync_error_name_from_type(OSYNC_NO_ERROR);
110  return osync_error_name_from_type((*error)->type);
111 }
112 
119 {
120  osync_return_if_fail(error != NULL);
121  if (*error == NULL)
122  return;
123 
124  if ((*error)->message)
125  g_free ((*error)->message);
126 
127  g_free(*error);
128  *error = NULL;
129 }
130 
137 osync_bool osync_error_is_set (OSyncError **error)
138 {
139  if (!error)
140  return FALSE;
141 
142  if (*error == NULL)
143  return FALSE;
144 
145  if ((*error)->type)
146  return TRUE;
147 
148  return FALSE;
149 }
150 
158 {
159  if (!osync_error_is_set(error))
160  return OSYNC_NO_ERROR;
161  return (*error)->type;
162 }
163 
170 const char *osync_error_print(OSyncError **error)
171 {
172  if (!osync_error_is_set(error))
173  return NULL;
174  return (*error)->message;
175 }
176 
187 void osync_error_update(OSyncError **error, const char *format, ...)
188 {
189  osync_return_if_fail(error != NULL);
190  osync_return_if_fail(*error != NULL);
191 
192  va_list args;
193  va_start(args, format);
194 
195  char buffer[1024];
196  memset(buffer, 0, sizeof(buffer));
197  g_vsnprintf(buffer, 1024, format, args);
198 
199  g_free((*error)->message);
200  (*error)->message = g_strdup(buffer);
201 
202  va_end (args);
203 }
204 
213 {
214  if (!target)
215  return;
216 
217  osync_return_if_fail(osync_error_is_set(source));
218 
219  if (!osync_error_is_set(source)) {
220  *target = NULL;
221  return;
222  }
223 
224  *target = g_malloc0(sizeof(OSyncError));
225  (*target)->message = g_strdup((*source)->message);
226  (*target)->type = (*source)->type;
227 }
228 
238 void osync_error_set(OSyncError **error, OSyncErrorType type, const char *format, ...)
239 {
240  va_list args;
241  va_start(args, format);
242  osync_error_set_vargs(error, type, format, args);
243  va_end (args);
244 }
245 
253 {
254  if (!error)
255  return;
256 
257  (*error)->type = type;
258  return;
259 }
260 
void osync_error_duplicate(OSyncError **target, OSyncError **source)
Duplicates the error into the target.
Represent an error.
osync_bool osync_error_is_set(OSyncError **error)
Checks if the error is set.
OSyncErrorType
Defines the possible error types.
Definition: opensync_error.h:5
void osync_error_set_vargs(OSyncError **error, OSyncErrorType type, const char *format, va_list args)
Sets a error from a va_list.
void osync_error_free(OSyncError **error)
Frees the error so it can be reused.
const char * osync_error_get_name(OSyncError **error)
This will return a string describing the type of the error.
void osync_error_set_type(OSyncError **error, OSyncErrorType type)
Sets the type of an error.
void osync_error_update(OSyncError **error, const char *format,...)
Updates the error message.
OSyncErrorType osync_error_get_type(OSyncError **error)
Returns the type of the error.
void osync_error_set(OSyncError **error, OSyncErrorType type, const char *format,...)
Sets the error.
const char * osync_error_print(OSyncError **error)
Returns the message of the error.