Redesign error code handling

`err` is always set now, even if the response is not valid.
This commit is contained in:
Gergely Polonkai 2016-01-12 14:09:03 +01:00
parent b11d5b2eb5
commit a959141b52

View File

@ -16,6 +16,7 @@
* <http://www.gnu.org/licenses/>. * <http://www.gnu.org/licenses/>.
*/ */
#include "config.h"
#include "matrix-http-api.h" #include "matrix-http-api.h"
#include "matrix-enumtypes.h" #include "matrix-enumtypes.h"
@ -498,39 +499,65 @@ _response_callback(SoupSession *session,
g_debug("We are reported to be logged in as %s", user_id); g_debug("We are reported to be logged in as %s", user_id);
} }
/* Check if the response holds an error code */ { // Check if the response holds an error
if ((node = json_object_get_member( JsonNode *errcode_node = json_object_get_member(root_object,
root_object, "errcode")) != NULL) { "errcode");
const gchar *errcode = json_node_get_string(node); JsonNode *error_node = json_object_get_member(root_object,
gchar *message = NULL; "error");
MatrixAPIError error_code = MATRIX_API_ERROR_UNKNOWN_ERROR;
/* Set the message as M_CODE: message */ if (errcode_node || error_node) {
if ((node = json_object_get_member( gchar *message;
root_object, "error")) != NULL) { const gchar *errcode = NULL;
message = g_strdup_printf("%s: %s", const gchar *error = NULL;
errcode, MatrixAPIError error_code = MATRIX_API_ERROR_UNKNOWN_ERROR;
json_node_get_string(node));
} else { if (errcode_node) {
/* If there is no message, issue a warning and GEnumClass *error_class;
* set up the message as plain M_CODE */ GEnumValue *value;
message = g_strdup(errcode);
errcode = json_node_get_string(errcode_node);
if (strncmp("M_", errcode, 2) == 0) {
gchar *matrix_error_code = g_strdup_printf(
"MATRIX_API_ERROR_%s", errcode + 2);
error_class = g_type_class_ref(
MATRIX_TYPE_API_ERROR);
value = g_enum_get_value_by_name(
error_class, matrix_error_code);
g_free(matrix_error_code);
g_type_class_unref(error_class);
if (value) {
error_code = value->value;
} else {
g_info("An unknown error code '%s' was sent by the homeserver. You may want to report it to the %s developers", errcode, PACKAGE_NAME);
}
}
} else {
g_info("An error was sent by the homeserver, but no error code was specified. You may want to report this to the homeserver administrators.");
error_code = MATRIX_API_ERROR_UNSPECIFIED;
}
if (error_node) {
error = json_node_get_string(error_node);
}
if (errcode_node && error_node) {
message = g_strdup_printf("%s: %s", errcode, error);
} else if (errcode_node) {
message = g_strdup(errcode);
} else {
message = g_strdup_printf(
"(No errcode given) %s", error);
}
err = g_error_new_literal(MATRIX_API_ERROR,
error_code,
message);
} }
/* 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);
} }
} else { // Not a JSON object } else { // Not a JSON object
err = g_error_new(MATRIX_API_ERROR, err = g_error_new(MATRIX_API_ERROR,
MATRIX_API_ERROR_BAD_RESPONSE, MATRIX_API_ERROR_BAD_RESPONSE,
"Bad response (not a JSON object)"); "Bad response (not a JSON object)");