Get callback function called even if there is an error
This commit is contained in:
parent
4847f73668
commit
b71bb84920
@ -31,14 +31,19 @@
|
|||||||
/**
|
/**
|
||||||
* MatrixAPIError:
|
* MatrixAPIError:
|
||||||
* @MATRIX_API_ERROR_NONE: no error
|
* @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
|
* @MATRIX_API_ERROR_MISSING_TOKEN: authorization token is missing
|
||||||
* from the request
|
* from the request
|
||||||
* @MATRIX_API_ERROR_FORBIDDEN: access was forbidden (e.g. due to a
|
* @MATRIX_API_ERROR_FORBIDDEN: access was forbidden (e.g. due to a
|
||||||
* missing/invalid token, or using a bad
|
* missing/invalid token, or using a bad
|
||||||
* password during login)
|
* password during login)
|
||||||
* @MATRIX_API_ERROR_UNKNOWN: an error unknown to the Matrix server
|
* @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
|
* @MATRIX_API_ERROR_UNKNOWN_ERROR: an error unknown to this library
|
||||||
*
|
*
|
||||||
* Value mappings from Matrix.org API error codes
|
* Value mappings from Matrix.org API error codes
|
||||||
|
@ -26,10 +26,17 @@ G_BEGIN_DECLS
|
|||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MATRIX_API_ERROR_NONE,
|
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_FORBIDDEN,
|
||||||
MATRIX_API_ERROR_UNKNOWN,
|
MATRIX_API_ERROR_UNKNOWN,
|
||||||
MATRIX_API_ERROR_INCOMPLETE,
|
|
||||||
/* Allow for a lot of Matrix.org defined codes
|
/* Allow for a lot of Matrix.org defined codes
|
||||||
Do not define error codes after this! */
|
Do not define error codes after this! */
|
||||||
MATRIX_API_ERROR_UNKNOWN_ERROR = 16384
|
MATRIX_API_ERROR_UNKNOWN_ERROR = 16384
|
||||||
|
@ -416,16 +416,20 @@ _response_callback(SoupSession *session,
|
|||||||
{
|
{
|
||||||
MatrixHTTPAPI *api = request->api;
|
MatrixHTTPAPI *api = request->api;
|
||||||
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
|
MatrixHTTPAPIPrivate *priv = matrix_http_api_get_instance_private(api);
|
||||||
|
GError *err = NULL;
|
||||||
|
JsonNode *content = NULL;
|
||||||
|
|
||||||
if (msg->status_code < SOUP_STATUS_CONTINUE) {
|
if (msg->status_code < SOUP_STATUS_CONTINUE) {
|
||||||
g_info("Request failed: %d: %s", msg->status_code, msg->reason_phrase);
|
err = g_error_new(MATRIX_API_ERROR,
|
||||||
} else {
|
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;
|
SoupBuffer *buffer;
|
||||||
const guint8 *data;
|
const guint8 *data;
|
||||||
gsize datalen;
|
gsize datalen;
|
||||||
JsonParser *parser;
|
JsonParser *parser;
|
||||||
GError *err = NULL;
|
|
||||||
JsonNode *content;
|
|
||||||
|
|
||||||
buffer = soup_message_body_flatten(msg->response_body);
|
buffer = soup_message_body_flatten(msg->response_body);
|
||||||
soup_buffer_get_data(buffer, &data, &datalen);
|
soup_buffer_get_data(buffer, &data, &datalen);
|
||||||
@ -525,25 +529,31 @@ _response_callback(SoupSession *session,
|
|||||||
err = g_error_new_literal(MATRIX_API_ERROR,
|
err = g_error_new_literal(MATRIX_API_ERROR,
|
||||||
error_code,
|
error_code,
|
||||||
message);
|
message);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} else { // Not a JSON object
|
||||||
/* Call the assigned function, if any */
|
err = g_error_new(MATRIX_API_ERROR,
|
||||||
if (request->callback) {
|
MATRIX_API_ERROR_BAD_RESPONSE,
|
||||||
request->callback(
|
"Bad response (not a JSON object)");
|
||||||
MATRIX_API(api),
|
g_debug("Bad response: %s", data);
|
||||||
content,
|
|
||||||
request->callback_data,
|
|
||||||
err);
|
|
||||||
g_clear_error(&err);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
g_debug("Invalid 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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user