00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "config.h"
00025 #include "qof.h"
00026 #include "qoferror-p.h"
00027 #include "qofsession-p.h"
00028
00029 struct QofError_s
00030 {
00031 QofErrorId id;
00032 gchar * message;
00033 const gchar * filename;
00034 gboolean use_file;
00035 QofTime * qt;
00036 };
00037
00038
00039 static GHashTable * error_table = NULL;
00040 static gint32 count = 0;
00041 static QofLogModule log_module = QOF_MOD_ERROR;
00042
00043 void
00044 qof_error_init (void)
00045 {
00046 error_table = g_hash_table_new (g_direct_hash, g_direct_equal);
00047 #ifndef QOF_DISABLE_DEPRECATED
00048 set_deprecated_errors ();
00049 #endif
00050 }
00051
00052 static void
00053 qof_error_free (QofError * error)
00054 {
00055 if (error->qt)
00056 qof_time_free (error->qt);
00057 g_free (error->message);
00058 g_free (error);
00059 }
00060
00061
00062 static void
00063 clear_table (gpointer key, gpointer value, gpointer user_data)
00064 {
00065 qof_error_free ((QofError*)value);
00066 }
00067
00068 void
00069 qof_error_close (void)
00070 {
00071 g_hash_table_foreach (error_table, clear_table, NULL);
00072 g_hash_table_destroy (error_table);
00073 }
00074
00075 #ifndef QOF_DISABLE_DEPRECATED
00076 void
00077 deprecated_support (QofErrorId id, const gchar * err_message)
00078 {
00079 QofError * qerr;
00080
00081 if (id >= ERR_LAST)
00082 return;
00083 qerr = g_new0 (QofError, 1);
00084 qerr->id = id;
00085 qerr->message = g_strdup (err_message);
00086 g_hash_table_insert (error_table, GINT_TO_POINTER(id), qerr);
00087 }
00088 #endif
00089
00090 QofErrorId
00091 qof_error_register (const gchar * err_message, gboolean use_file)
00092 {
00093 QofError * qerr;
00094
00095 ENTER (" ");
00096 qerr = g_new0 (QofError, 1);
00097 count++;
00098 #ifndef QOF_DISABLE_DEPRECATED
00099 count += ERR_LAST;
00100 #endif
00101 qerr->id = count;
00102 if (use_file)
00103 {
00104 gchar * spec;
00105
00106 spec = g_strrstr (err_message, "%s");
00107 use_file = (spec) ? TRUE : FALSE;
00108 }
00109 qerr->use_file = use_file;
00110 qerr->message = g_strdup (err_message);
00111 g_hash_table_insert (error_table, GINT_TO_POINTER(qerr->id), qerr);
00112 LEAVE (" ");
00113 return qerr->id;
00114 }
00115
00116 void
00117 qof_error_unregister (QofErrorId id)
00118 {
00119 QofError * qerr;
00120 gboolean result;
00121
00122 ENTER (" ");
00123 qerr = g_hash_table_lookup (error_table, GINT_TO_POINTER(id));
00124 qof_error_free (qerr);
00125 result = g_hash_table_remove (error_table,
00126 GINT_TO_POINTER(id));
00127 if (!result)
00128 LEAVE ("unable to remove registered error.");
00129 LEAVE (" ok.");
00130 }
00131
00132 void
00133 qof_error_set (QofSession * session, QofErrorId error)
00134 {
00135 QofError * qerr, * set;
00136
00137 g_return_if_fail (session);
00138 if (error == QOF_SUCCESS)
00139 {
00140 DEBUG (" passed success, not error.");
00141 return;
00142 }
00143 qerr = g_hash_table_lookup (error_table, GINT_TO_POINTER(error));
00144 if (!qerr)
00145 {
00146 DEBUG (" failed hash table lookup");
00147 return;
00148 }
00149 session->last_err = error;
00150 if (session->error_message)
00151 g_free (session->error_message);
00152 if (qerr->use_file)
00153 session->error_message = g_strdup_printf (qerr->message,
00154 qof_session_get_url (session));
00155 else
00156 session->error_message = g_strdup (qerr->message);
00157 if (!session->backend)
00158 return;
00159
00160 set = g_new0 (QofError, 1);
00161 if (qerr->use_file)
00162 set->message = g_strdup_printf (qerr->message,
00163 qof_session_get_file_path (session));
00164 else
00165 set->message = g_strdup (qerr->message);
00166 set->id = error;
00167 set->qt = qof_time_get_current ();
00168 session->backend->error_stack =
00169 g_list_prepend (session->backend->error_stack, set);
00170 #ifndef QOF_DISABLE_DEPRECATED
00171 session->backend->last_err = error;
00172 #endif
00173 }
00174
00175 void
00176 qof_error_set_be (QofBackend * be, QofErrorId error)
00177 {
00178 QofError * qerr, * set;
00179
00180 g_return_if_fail (be);
00181 if (error == QOF_SUCCESS)
00182 return;
00183 qerr = g_hash_table_lookup (error_table, GINT_TO_POINTER(error));
00184 if (!qerr)
00185 return;
00186
00187 set = g_new0 (QofError, 1);
00188 if (qerr->use_file)
00189 set->message = g_strdup_printf (qerr->message, be->fullpath);
00190 else
00191 set->message = g_strdup (qerr->message);
00192 set->id = error;
00193 set->qt = qof_time_get_current ();
00194 be->error_stack = g_list_prepend (be->error_stack,
00195 set);
00196 #ifndef QOF_DISABLE_DEPRECATED
00197 be->last_err = error;
00198 #endif
00199 }
00200
00201
00202 static void
00203 clear_list (gpointer value, gpointer user_data)
00204 {
00205 qof_error_free ((QofError*)value);
00206 }
00207
00208 void
00209 qof_error_clear (QofSession * session)
00210 {
00211 g_return_if_fail (session);
00212 if (!session->backend)
00213 return;
00214 g_list_foreach (session->backend->error_stack, clear_list, NULL);
00215 g_list_free (session->backend->error_stack);
00216 session->backend->error_stack = NULL;
00217 if (session->error_message)
00218 g_free (session->error_message);
00219 session->error_message = NULL;
00220 session->last_err = QOF_SUCCESS;
00221 #ifndef QOF_DISABLE_DEPRECATED
00222 session->backend->last_err = QOF_SUCCESS;
00223 #endif
00224 }
00225
00226 QofErrorId
00227 qof_error_check (QofSession * session)
00228 {
00229 g_return_val_if_fail (session, QOF_FATAL);
00230 return qof_error_check_be (session->backend);
00231 }
00232
00233 QofErrorId
00234 qof_error_check_be (QofBackend * be)
00235 {
00236 QofError * qerr;
00237 GList * first;
00238
00239 if (!be)
00240 return QOF_FATAL;
00241 if (g_list_length (be->error_stack) == 0)
00242 return QOF_SUCCESS;
00243 first = g_list_first (be->error_stack);
00244 qerr = (QofError*)first->data;
00245 if (!qerr)
00246 return QOF_FATAL;
00247 return qerr->id;
00248 }
00249
00250 QofTime *
00251 qof_error_get_time_be (QofBackend * be)
00252 {
00253 QofError * qerr;
00254 GList * first;
00255
00256 if (g_list_length(be->error_stack) == 0)
00257 return NULL;
00258 first = g_list_first (be->error_stack);
00259 qerr = (QofError*)first->data;
00260 return qerr->qt;
00261 }
00262
00263 QofTime *
00264 qof_error_get_time (QofSession * session)
00265 {
00266 return qof_error_get_time_be (session->backend);
00267 }
00268
00269 #ifndef QOF_DISABLE_DEPRECATED
00270 static void
00271 set_previous_error (QofBackend * be)
00272 {
00273 QofError * qerr;
00274 GList * pop;
00275
00276 if (!be)
00277 return;
00278 if (g_list_length(be->error_stack) == 0)
00279 return;
00280 pop = g_list_last (be->error_stack);
00281 qerr = (QofError*)pop->data;
00282 be->last_err = qerr->id;
00283 be->error_msg = qerr->message;
00284 }
00285 #endif
00286
00287 QofErrorId
00288 qof_error_get_id (QofSession * session)
00289 {
00290 QofErrorId id;
00291
00292 g_return_val_if_fail (session, QOF_FATAL);
00293 id = QOF_SUCCESS;
00294 id = qof_error_get_id_be (session->backend);
00295 {
00296 QofError * qerr;
00297
00298 qerr = g_hash_table_lookup (error_table,
00299 GINT_TO_POINTER(id));
00300 if (!qerr)
00301 {
00302 DEBUG (" empty QofError value");
00303 return QOF_FATAL;
00304 }
00305 if (session->error_message)
00306 g_free (session->error_message);
00307 session->error_message = qerr->message;
00308 session->last_err = id;
00309 }
00310 return id;
00311 }
00312
00313 QofErrorId
00314 qof_error_get_id_be (QofBackend * be)
00315 {
00316 QofError * qerr;
00317 GList * first;
00318
00319 if (!be)
00320 return QOF_FATAL;
00321 if (g_list_length (be->error_stack) == 0)
00322 return QOF_SUCCESS;
00323 first = g_list_first (be->error_stack);
00324 qerr = (QofError*)first->data;
00325 if (!qerr)
00326 return QOF_FATAL;
00327 be->error_stack =
00328 g_list_remove (be->error_stack, qerr);
00329 #ifndef QOF_DISABLE_DEPRECATED
00330 set_previous_error (be);
00331 #endif
00332 return qerr->id;
00333 }
00334
00335 const gchar *
00336 qof_error_get_message (QofSession * session)
00337 {
00338 const gchar * msg;
00339
00340 g_return_val_if_fail (session, NULL);
00341 if (!session->backend)
00342 return session->error_message;
00343 msg = qof_error_get_message_be (session->backend);
00344 DEBUG (" msg_1=%s", msg);
00345 #ifndef QOF_DISABLE_DEPRECATED
00346 {
00347 QofError * qerr;
00348
00349 qerr = g_hash_table_lookup (error_table,
00350 GINT_TO_POINTER(session->backend->last_err));
00351 if (qerr)
00352 {
00353 if (session->error_message)
00354 g_free (session->error_message);
00355 session->error_message = g_strdup(msg);
00356 session->last_err = qerr->id;
00357 }
00358 }
00359 #endif
00360 return msg;
00361 }
00362
00363 const gchar *
00364 qof_error_get_message_be (QofBackend * be)
00365 {
00366 QofError * qerr;
00367 GList * first;
00368
00369 g_return_val_if_fail (be, NULL);
00370 if (g_list_length (be->error_stack) == 0)
00371 {
00372 DEBUG (" empty error stack");
00373 return NULL;
00374 }
00375 first = g_list_first (be->error_stack);
00376 qerr = (QofError*)first->data;
00377 if (!qerr)
00378 {
00379 DEBUG (" empty QofError value");
00380 return NULL;
00381 }
00382 DEBUG (" qerr->message=%s", qerr->message);
00383 be->error_stack =
00384 g_list_remove (be->error_stack, qerr);
00385 #ifndef QOF_DISABLE_DEPRECATED
00386 be->error_msg = qerr->message;
00387 set_previous_error (be);
00388 #endif
00389 return qerr->message;
00390 }