Add response error handling to the HTTP API
This commit is contained in:
parent
9552136059
commit
2dbc3ea316
@ -48,7 +48,7 @@ typedef struct _MatrixAPI MatrixAPI;
|
||||
typedef void (*MatrixAPICallback)(MatrixAPI *api,
|
||||
JsonNode *content,
|
||||
gpointer data,
|
||||
GError **err);
|
||||
GError *err);
|
||||
|
||||
struct _MatrixAPIInterface {
|
||||
/*< private >*/
|
||||
|
@ -348,6 +348,7 @@ response_callback(SoupSession *session,
|
||||
if (JSON_NODE_HOLDS_OBJECT(content)) {
|
||||
JsonObject *root_object;
|
||||
JsonNode *node;
|
||||
GError *err = NULL;
|
||||
|
||||
root_object = json_node_get_object(content);
|
||||
|
||||
@ -372,13 +373,48 @@ response_callback(SoupSession *session,
|
||||
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 */
|
||||
if (request->callback) {
|
||||
request->callback(
|
||||
MATRIX_API(api),
|
||||
content,
|
||||
request->callback_data,
|
||||
NULL);
|
||||
err);
|
||||
g_clear_error(&err);
|
||||
}
|
||||
} else {
|
||||
g_debug("Invalid response: %s", data);
|
||||
|
@ -31,10 +31,19 @@ static GOptionEntry entries[] = {
|
||||
};
|
||||
|
||||
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();
|
||||
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);
|
||||
|
||||
@ -92,7 +101,7 @@ main(int argc, char *argv[])
|
||||
"password", password,
|
||||
NULL);
|
||||
matrix_http_api_login(MATRIX_API(api),
|
||||
login_finished, NULL,
|
||||
login_finished, loop,
|
||||
"m.login.password",
|
||||
params);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user