Add response error handling to the HTTP API

This commit is contained in:
Gergely Polonkai 2015-12-15 12:25:45 +01:00
parent 9552136059
commit 2dbc3ea316
3 changed files with 49 additions and 4 deletions

View File

@ -48,7 +48,7 @@ typedef struct _MatrixAPI MatrixAPI;
typedef void (*MatrixAPICallback)(MatrixAPI *api, typedef void (*MatrixAPICallback)(MatrixAPI *api,
JsonNode *content, JsonNode *content,
gpointer data, gpointer data,
GError **err); GError *err);
struct _MatrixAPIInterface { struct _MatrixAPIInterface {
/*< private >*/ /*< private >*/

View File

@ -348,6 +348,7 @@ response_callback(SoupSession *session,
if (JSON_NODE_HOLDS_OBJECT(content)) { if (JSON_NODE_HOLDS_OBJECT(content)) {
JsonObject *root_object; JsonObject *root_object;
JsonNode *node; JsonNode *node;
GError *err = NULL;
root_object = json_node_get_object(content); root_object = json_node_get_object(content);
@ -372,13 +373,48 @@ response_callback(SoupSession *session,
g_debug("Our home server calls itself %s", homeserver); g_debug("Our home server calls itself %s", homeserver);
} }
/* Check if the response holds an error code */
if ((node = json_object_get_member(
root_object, "errcode")) != NULL) {
const gchar *errcode = json_node_get_string(node);
gchar *message = NULL;
MatrixAPIError error_code = MATRIX_API_ERROR_UNKNOWN_ERROR;
/* Set the message as M_CODE: message */
if ((node = json_object_get_member(
root_object, "error")) != NULL) {
message = g_strdup_printf("%s: %s",
errcode,
json_node_get_string(node));
} else {
/* If there is no message, issue a warning and
* set up the message as plain M_CODE */
message = g_strdup(errcode);
}
/* Set the actual GError code according to errcode */
if (strcmp("M_MISSING_TOKEN", errcode) == 0) {
error_code = MATRIX_API_ERROR_MISSING_TOKEN;
} else if (strcmp("M_FORBIDDEN", errcode) == 0) {
error_code = MATRIX_API_ERROR_FORBIDDEN;
} else if (strcmp("M_UNKNOWN", errcode) == 0) {
error_code = MATRIX_API_ERROR_UNKNOWN;
}
err = g_error_new_literal(MATRIX_API_ERROR,
error_code,
message);
}
/* Call the assigned function, if any */ /* Call the assigned function, if any */
if (request->callback) { if (request->callback) {
request->callback( request->callback(
MATRIX_API(api), MATRIX_API(api),
content, content,
request->callback_data, request->callback_data,
NULL); err);
g_clear_error(&err);
} }
} else { } else {
g_debug("Invalid response: %s", data); g_debug("Invalid response: %s", data);

View File

@ -31,10 +31,19 @@ static GOptionEntry entries[] = {
}; };
static void static void
login_finished(MatrixAPI *api, JsonNode *content, gpointer data, GError **err) login_finished(MatrixAPI *api, JsonNode *content, gpointer data, GError *err)
{ {
JsonPath *path = json_path_new(); JsonPath *path = json_path_new();
JsonNode *result; JsonNode *result;
GMainLoop *loop = data;
if (err) {
g_printf("ERROR: %s\n", err->message);
g_main_loop_quit(loop);
return;
}
json_path_compile(path, "$.user_id", NULL); json_path_compile(path, "$.user_id", NULL);
@ -92,7 +101,7 @@ main(int argc, char *argv[])
"password", password, "password", password,
NULL); NULL);
matrix_http_api_login(MATRIX_API(api), matrix_http_api_login(MATRIX_API(api),
login_finished, NULL, login_finished, loop,
"m.login.password", "m.login.password",
params); params);