diff --git a/src/matrix-api-types.c b/src/matrix-api-types.c index b4d568c..ca85713 100644 --- a/src/matrix-api-types.c +++ b/src/matrix-api-types.c @@ -31,14 +31,19 @@ /** * MatrixAPIError: * @MATRIX_API_ERROR_NONE: no error + * @MATRIX_API_ERROR_COMMUNICATION_ERROR: there was a problem in + * communication + * (e.g. connection error) + * @MATRIX_API_ERROR_INCOMPLETE: the passed/generated data is + * incomplete + * @MATRIX_API_ERROR_BAD_RESPONSE: malformed response, or the response + * is not a JSON object * @MATRIX_API_ERROR_MISSING_TOKEN: authorization token is missing * from the request * @MATRIX_API_ERROR_FORBIDDEN: access was forbidden (e.g. due to a * missing/invalid token, or using a bad * password during login) * @MATRIX_API_ERROR_UNKNOWN: an error unknown to the Matrix server - * @MATRIX_API_ERROR_INCOMPLETE: the passed/generated data is - * incomplete * @MATRIX_API_ERROR_UNKNOWN_ERROR: an error unknown to this library * * Value mappings from Matrix.org API error codes diff --git a/src/matrix-api-types.h b/src/matrix-api-types.h index affa9c4..792edc4 100644 --- a/src/matrix-api-types.h +++ b/src/matrix-api-types.h @@ -26,10 +26,17 @@ G_BEGIN_DECLS typedef enum { MATRIX_API_ERROR_NONE, - MATRIX_API_ERROR_MISSING_TOKEN, + MATRIX_API_ERROR_COMMUNICATION_ERROR, + MATRIX_API_ERROR_INCOMPLETE, + MATRIX_API_ERROR_BAD_RESPONSE, + + /* Add Matrix-defined error codes under here, changing `M_` to + * `MATRIX_API_ERROR`, i.e. `M_FORBIDDEN` => + * `MATRIX_API_ERROR_FORBIDDEN` */ + MATRIX_API_ERROR_MISSING_TOKEN = 500, MATRIX_API_ERROR_FORBIDDEN, MATRIX_API_ERROR_UNKNOWN, - MATRIX_API_ERROR_INCOMPLETE, + /* Allow for a lot of Matrix.org defined codes Do not define error codes after this! */ MATRIX_API_ERROR_UNKNOWN_ERROR = 16384 diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index 1f61d61..f4b9bc3 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -416,16 +416,20 @@ _response_callback(SoupSession *session, { MatrixHTTPAPI *api = request->api; MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api); + GError *err = NULL; + JsonNode *content = NULL; if (msg->status_code < SOUP_STATUS_CONTINUE) { - g_info("Request failed: %d: %s", msg->status_code, msg->reason_phrase); - } else { + err = g_error_new(MATRIX_API_ERROR, + MATRIX_API_ERROR_COMMUNICATION_ERROR, + "%s %d: %s", + (msg->status_code < 100) ? "Network error" : "HTTP", + msg->status_code, msg->reason_phrase); + } else { // No error SoupBuffer *buffer; const guint8 *data; gsize datalen; JsonParser *parser; - GError *err = NULL; - JsonNode *content; buffer = soup_message_body_flatten(msg->response_body); soup_buffer_get_data(buffer, &data, &datalen); @@ -525,25 +529,31 @@ _response_callback(SoupSession *session, 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, - err); - g_clear_error(&err); - } - } else { - g_debug("Invalid response: %s", data); + } else { // Not a JSON object + err = g_error_new(MATRIX_API_ERROR, + MATRIX_API_ERROR_BAD_RESPONSE, + "Bad response (not a JSON object)"); + g_debug("Bad response: %s", data); } - } else { + } else { // Invalid JSON + err = g_error_new(MATRIX_API_ERROR, + MATRIX_API_ERROR_BAD_RESPONSE, + "Malformed response (invalid JSON)"); g_debug("Malformed response: %s", data); } } + + /* Call the assigned function, if any */ + if (request->callback) { + request->callback( + MATRIX_API(api), + content, + request->callback_data, + err); + } + + g_clear_error(&err); } /**