[evolution-ews] Report errors from backends in their error domain



commit 9f7c3e59bc288e2b3b4c130e20143a793f18862e
Author: Milan Crha <mcrha redhat com>
Date:   Thu Apr 19 10:58:00 2012 +0200

    Report errors from backends in their error domain
    
    This way the client can recognize the error and act accordingly,
    instead of showing an unknown error to a user.

 src/addressbook/e-book-backend-ews.c |   64 +++++++++++++++++++++----
 src/calendar/e-cal-backend-ews.c     |   87 ++++++++++++++++++++++++++-------
 2 files changed, 122 insertions(+), 29 deletions(-)
---
diff --git a/src/addressbook/e-book-backend-ews.c b/src/addressbook/e-book-backend-ews.c
index ab05e5e..b1b1496 100644
--- a/src/addressbook/e-book-backend-ews.c
+++ b/src/addressbook/e-book-backend-ews.c
@@ -59,7 +59,6 @@
 
 #define EDB_ERROR(_code) e_data_book_create_error (E_DATA_BOOK_STATUS_ ## _code, NULL)
 #define EDB_ERROR_EX(_code,_msg) e_data_book_create_error (E_DATA_BOOK_STATUS_ ## _code, _msg)
-#define EDB_ERROR_FAILED_STATUS(_code, _status) e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_ ## _code, "Failed with status 0x%x", _status)
 
 G_DEFINE_TYPE (EBookBackendEws, e_book_backend_ews, E_TYPE_BOOK_BACKEND)
 
@@ -122,6 +121,43 @@ enum {
 #define PRIV_UNLOCK(p) (g_static_rec_mutex_unlock (&(p)->rec_mutex))
 
 static void
+convert_error_to_edb_error (GError **perror)
+{
+	GError *error = NULL;
+
+	g_return_if_fail (perror != NULL);
+
+	if (!*perror || (*perror)->domain == E_DATA_BOOK_ERROR)
+		return;
+
+	if ((*perror)->domain == EWS_CONNECTION_ERROR) {
+		switch ((*perror)->code) {
+		case EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED:
+			error = EDB_ERROR (AUTHENTICATION_FAILED);
+			break;
+		case EWS_CONNECTION_ERROR_CANCELLED:
+			break;
+		case EWS_CONNECTION_ERROR_FOLDERNOTFOUND:
+		case EWS_CONNECTION_ERROR_MANAGEDFOLDERNOTFOUND:
+		case EWS_CONNECTION_ERROR_PARENTFOLDERNOTFOUND:
+		case EWS_CONNECTION_ERROR_PUBLICFOLDERSERVERNOTFOUND:
+			error = EDB_ERROR (NO_SUCH_BOOK);
+			break;
+		case EWS_CONNECTION_ERROR_EVENTNOTFOUND:
+		case EWS_CONNECTION_ERROR_ITEMNOTFOUND:
+			error = EDB_ERROR (CONTACT_NOT_FOUND);
+			break;
+		}
+	}
+
+	if (!error)
+		error = EDB_ERROR_EX (OTHER_ERROR, (*perror)->message);
+
+	g_error_free (*perror);
+	*perror = error;
+}
+
+static void
 ews_auth_required (EBookBackend *backend)
 {
 	e_book_backend_notify_auth_required (backend, TRUE, NULL);
@@ -1296,6 +1332,7 @@ e_book_backend_ews_get_contact_list (EBookBackend *backend,
 				e_book_backend_sqlitedb_search_data_free (s_data);
 				l = l->next;
 			}
+			convert_error_to_edb_error (&error);
 			e_data_book_respond_get_contact_list (book, opid, error, vcard_list);
 
 			g_slist_free (list);
@@ -1323,6 +1360,7 @@ e_book_backend_ews_get_contact_list (EBookBackend *backend,
 			l = l->next;
 		}
 
+		convert_error_to_edb_error (&error);
 		e_data_book_respond_get_contact_list (book, opid, error, vcard_list);
 
 		g_slist_free (list);
@@ -1348,6 +1386,7 @@ e_book_backend_ews_get_contact_list (EBookBackend *backend,
 
 		/*we have got Id for items lets fetch them using getitem operation*/
 		ebews_fetch_items (ebews, items, FALSE, &vcard_list, &error);
+		convert_error_to_edb_error (&error);
 		e_data_book_respond_get_contact_list (book, opid, error, vcard_list);
 
 		e_ews_folder_free_fid (fid);
@@ -2614,18 +2653,22 @@ e_book_backend_ews_authenticate_user (EBookBackend *backend,
 	} else
 		priv->is_writable = TRUE;
 
-	/* a dummy request to make sure we have a authenticated connection */
-	fid = g_new0 (EwsFolderId, 1);
-	fid->id = g_strdup ("contacts");
-	fid->is_distinguished_id = TRUE;
-	ids = g_slist_append (ids, fid);
-	e_ews_connection_get_folder (cnc, EWS_PRIORITY_MEDIUM, "Default", NULL, ids, &folders, NULL, &error);
+	if (!error && cnc) {
+		/* a dummy request to make sure we have a authenticated connection */
+		fid = g_new0 (EwsFolderId, 1);
+		fid->id = g_strdup ("contacts");
+		fid->is_distinguished_id = TRUE;
+		ids = g_slist_append (ids, fid);
+		e_ews_connection_get_folder (cnc, EWS_PRIORITY_MEDIUM, "Default", NULL, ids, &folders, NULL, &error);
 
-	e_ews_folder_free_fid (fid);
-	g_slist_free (ids);
-	ids = NULL;
+		e_ews_folder_free_fid (fid);
+		g_slist_free (ids);
+		ids = NULL;
+	}
 
 	if (error) {
+		convert_error_to_edb_error (&error);
+
 		e_book_backend_notify_auth_required (backend, TRUE, priv->credentials);
 		e_book_backend_notify_opened (backend, error);
 		g_object_unref (cnc);
@@ -2746,6 +2789,7 @@ e_book_backend_ews_open (EBookBackend *backend,
 
 	source = e_backend_get_source (E_BACKEND (backend));
 	e_book_backend_ews_load_source (backend, source, only_if_exists, &error);
+	convert_error_to_edb_error (&error);
 	e_data_book_respond_open (book, opid, error);
 }
 
diff --git a/src/calendar/e-cal-backend-ews.c b/src/calendar/e-cal-backend-ews.c
index 6758c30..777c378 100644
--- a/src/calendar/e-cal-backend-ews.c
+++ b/src/calendar/e-cal-backend-ews.c
@@ -123,6 +123,43 @@ static gboolean ews_start_sync	(gpointer data);
 static icaltimezone * e_cal_get_timezone_from_ical_component (ECalBackend *backend, icalcomponent *comp);
 
 static void
+convert_error_to_edc_error (GError **perror)
+{
+	GError *error = NULL;
+
+	g_return_if_fail (perror != NULL);
+
+	if (!*perror || (*perror)->domain == E_DATA_CAL_ERROR)
+		return;
+
+	if ((*perror)->domain == EWS_CONNECTION_ERROR) {
+		switch ((*perror)->code) {
+		case EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED:
+			error = EDC_ERROR (AuthenticationFailed);
+			break;
+		case EWS_CONNECTION_ERROR_CANCELLED:
+			break;
+		case EWS_CONNECTION_ERROR_FOLDERNOTFOUND:
+		case EWS_CONNECTION_ERROR_MANAGEDFOLDERNOTFOUND:
+		case EWS_CONNECTION_ERROR_PARENTFOLDERNOTFOUND:
+		case EWS_CONNECTION_ERROR_PUBLICFOLDERSERVERNOTFOUND:
+			error = EDC_ERROR (NoSuchCal);
+			break;
+		case EWS_CONNECTION_ERROR_EVENTNOTFOUND:
+		case EWS_CONNECTION_ERROR_ITEMNOTFOUND:
+			error = EDC_ERROR (ObjectNotFound);
+			break;
+		}
+	}
+
+	if (!error)
+		error = EDC_ERROR_EX (OtherError, (*perror)->message);
+
+	g_error_free (*perror);
+	*perror = error;
+}
+
+static void
 switch_offline (ECalBackendEws *cbews)
 {
 	ECalBackendEwsPrivate *priv;
@@ -202,6 +239,7 @@ e_cal_backend_ews_add_timezone (ECalBackend *backend,
 
 exit:
 	/*FIXME pass tzid here */
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_add_timezone (cal, context, error);
 }
 
@@ -247,11 +285,10 @@ ews_cal_discard_alarm_cb (GObject *object,
 	GError *error = NULL;
 
 	if (!e_ews_connection_update_items_finish (cnc, res, NULL, &error)) {
-		/* The calendar UI doesn't *display* errors unless they have
-		 * the OtherError code */
-		error->code = OtherError;
+		convert_error_to_edc_error (&error);
 	}
 
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_discard_alarm (edad->cal, edad->context, error);
 
 	g_free (edad->itemid);
@@ -387,6 +424,7 @@ e_cal_backend_ews_get_timezone (ECalBackend *backend,
 		}
 	}
 
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_get_timezone (cal, context, error, object);
 	g_free (object);
 
@@ -625,6 +663,7 @@ e_cal_backend_ews_open_compat (ECalBackend *backend,
 		e_cal_backend_notify_auth_required (backend, TRUE, priv->credentials);
 
 	e_cal_backend_notify_opened (backend, NULL);
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_open (cal, opid, error);
 }
 
@@ -659,7 +698,7 @@ e_cal_backend_ews_authenticate_user (ECalBackend *backend,
 
 	PRIV_UNLOCK (priv);
 
-	/* This seems in-correct, but thats how authenticate_user_sync works */
+	convert_error_to_edc_error (&error);
 	e_cal_backend_notify_opened (backend, error);
 }
 
@@ -729,6 +768,7 @@ e_cal_backend_ews_get_object (ECalBackend *backend,
 	g_propagate_error (&error, EDC_ERROR (ObjectNotFound));
 
 exit:
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_get_object (cal, context, error, object);
 	g_free (object);
 }
@@ -797,6 +837,7 @@ e_cal_backend_ews_get_object_list (ECalBackend *backend,
 
 	cal_backend_ews_get_object_list (backend, sexp, &objects, &error);
 
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_get_object_list (cal, context, error, objects);
 	if (objects) {
 		for (l = objects; l != NULL; l = g_slist_next (l))
@@ -870,8 +911,9 @@ ews_cal_remove_object_cb (GObject *object,
 		 * more than one item at a time... */
 		if (remove_data->comp) ews_cal_delete_comp (remove_data->cbews, remove_data->comp, remove_data->item_id.id);
 		if (remove_data->parent) ews_cal_append_exdate (remove_data->cbews, remove_data->parent, remove_data->rid, remove_data->mod);
+	}
 
-	} else error->code = OtherError;
+	convert_error_to_edc_error (&error);
 
 	if (remove_data->context)
 		e_data_cal_respond_remove_objects (remove_data->cal, remove_data->context, error, NULL, NULL, NULL);
@@ -1015,6 +1057,7 @@ errorlvl1:
 	if (parent) g_object_unref (parent);
 
 exit:
+	convert_error_to_edc_error (&error);
 	if (context)
 		e_data_cal_respond_remove_objects (cal, context, error, NULL, NULL, NULL);
 	else if (error) {
@@ -1435,6 +1478,7 @@ ews_create_object_cb (GObject *object,
 
 	/* make sure there was no error */
 	if (error != NULL) {
+		convert_error_to_edc_error (&error);
 		e_data_cal_respond_create_objects (create_data->cal, create_data->context, error, NULL, NULL);
 		return;
 	}
@@ -1458,6 +1502,7 @@ ews_create_object_cb (GObject *object,
 		if (!res && error != NULL) {
 			if (items_req)
 				g_slist_free_full (items_req, g_object_unref);
+			convert_error_to_edc_error (&error);
 			e_data_cal_respond_create_objects (create_data->cal, create_data->context, error, NULL, NULL);
 			return;
 		}
@@ -1467,6 +1512,7 @@ ews_create_object_cb (GObject *object,
 			error = g_error_copy (e_ews_item_get_error (item));
 			g_slist_free_full (items_req, g_object_unref);
 
+			convert_error_to_edc_error (&error);
 			e_data_cal_respond_create_objects (create_data->cal, create_data->context, error, NULL, NULL);
 			return;
 		}
@@ -1525,6 +1571,7 @@ ews_create_object_cb (GObject *object,
 	new_uids = g_slist_append (NULL, (gpointer) comp_uid);
 	new_comps = g_slist_append (NULL, create_data->comp);
 
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_create_objects (create_data->cal, create_data->context, error, new_uids, new_comps);
 
 	g_slist_free (new_uids);
@@ -1707,6 +1754,7 @@ e_cal_backend_ews_create_objects (ECalBackend *backend,
 	return;
 
 exit:
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_create_objects (cal, context, error, NULL, NULL);
 }
 
@@ -1728,9 +1776,7 @@ ews_cal_modify_object_cb (GObject *object,
 	const gchar *x_name;
 
 	if (!e_ews_connection_update_items_finish (cnc, res, &ids, &error)) {
-		/* The calendar UI doesn't *display* errors unless they have
-		 * the OtherError code */
-		error->code = OtherError;
+		convert_error_to_edc_error (&error);
 		if (modify_data->context)
 			e_data_cal_respond_modify_objects (modify_data->cal, modify_data->context, error, NULL, NULL);
 		goto exit;
@@ -1768,6 +1814,7 @@ ews_cal_modify_object_cb (GObject *object,
 		old_components = g_slist_append (NULL, modify_data->oldcomp);
 		new_components = g_slist_append (NULL, modify_data->comp);
 
+		convert_error_to_edc_error (&error);
 		e_data_cal_respond_modify_objects (modify_data->cal, modify_data->context, error, old_components, new_components);
 
 		g_slist_free (old_components);
@@ -2240,8 +2287,10 @@ e_cal_backend_ews_modify_object (ECalBackend *backend,
 		attach_data->itemid = itemid;
 		attach_data->changekey = changekey;
 
-		if (context)
+		if (context) {
+			convert_error_to_edc_error (&error);
 			e_data_cal_respond_modify_objects (cal, context, error, NULL, NULL);
+		}
 
 		e_ews_connection_create_attachments_start (priv->cnc, EWS_PRIORITY_MEDIUM,
 							   item_id, added_attachments,
@@ -2281,6 +2330,7 @@ e_cal_backend_ews_modify_object (ECalBackend *backend,
 	return;
 
 exit:
+	convert_error_to_edc_error (&error);
 	if (context)
 		e_data_cal_respond_modify_objects (cal, context, error, NULL, NULL);
 	else if (error) {
@@ -2492,11 +2542,7 @@ e_cal_backend_ews_receive_objects (ECalBackend *backend,
 								       prepare_accept_item_request,
 								       accept_data,
 								       &ids, cancellable, &error);
-				if (error)
-					/* The calendar UI doesn't *display* errors unless they have
-					 * the OtherError code */
-					error->code = OtherError;
-			else {
+			if (!error) {
 				transp = icalcomponent_get_first_property (subcomp, ICAL_TRANSP_PROPERTY);
 				if (!g_strcmp0 (icalproperty_get_value_as_string (transp), "TRANSPARENT") &&
 				    !g_strcmp0 (response_type, "ACCEPTED")) {
@@ -2520,8 +2566,6 @@ e_cal_backend_ews_receive_objects (ECalBackend *backend,
 								       &ids,
 								       cancellable,
 								       &error);
-					if (error)
-						error->code = OtherError;
 				}
 			}
 				g_free (item_id);
@@ -2558,6 +2602,7 @@ e_cal_backend_ews_receive_objects (ECalBackend *backend,
 	icalcomponent_free (icalcomp);
 
 exit:
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_receive_objects (cal, context, error);
 }
 
@@ -2668,7 +2713,7 @@ ewscal_send_cancellation_email (ECalBackend *backend,
 	camel_ews_utils_create_mime_message (cnc, "SendOnly", NULL, message, 0, from, NULL, NULL, NULL, &error);
 
 	if (error) {
-		g_warning ("Failed to send cancellation email\n");
+		g_warning ("Failed to send cancellation email: %s", error->message);
 		g_clear_error (&error);
 	}
 
@@ -2767,6 +2812,7 @@ e_cal_backend_ews_send_objects (ECalBackend *backend,
 	icalcomponent_free (icalcomp);
 
 exit:
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_send_objects (cal, context, error,  NULL, calobj);
 }
 
@@ -2829,7 +2875,7 @@ ews_get_attachments_ready_callback (GObject *object,
 	ids = e_ews_connection_get_attachments_finish	(cnc, res, &uris, &error);
 
 	if (error != NULL) {
-		error->code = OtherError;
+		g_clear_error (&error);
 		return;
 	}
 
@@ -3596,6 +3642,7 @@ e_cal_backend_ews_refresh (ECalBackend *backend,
 	PRIV_UNLOCK (priv);
 
 exit:
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_refresh (cal, context, error);
 }
 
@@ -3663,7 +3710,6 @@ ews_cal_get_free_busy_cb (GObject *obj,
 	GError *error = NULL;
 
 	if (!e_ews_connection_get_free_busy_finish (cnc, res, &free_busy_sl, &error)) {
-		error->code = OtherError;
 		goto done;
 	}
 
@@ -3678,6 +3724,7 @@ ews_cal_get_free_busy_cb (GObject *obj,
 done:
 	if (free_busy)
 		e_data_cal_report_free_busy_data (free_busy_data->cal, free_busy);
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_get_free_busy (free_busy_data->cal, free_busy_data->context, error);
 
 	/* FIXME free free_busy_sl ? */
@@ -3742,6 +3789,7 @@ e_cal_backend_ews_get_free_busy (ECalBackend *backend,
 	return;
 
 exit:
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_get_free_busy (cal, context, error);
 
 }
@@ -3806,6 +3854,7 @@ e_cal_backend_ews_get_backend_property (ECalBackend *backend,
 		return;
 	}
 
+	convert_error_to_edc_error (&error);
 	e_data_cal_respond_get_backend_property (cal, opid, error, prop_value);
 	g_free (prop_value);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]