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,38 +499,64 @@ _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");
if (errcode_node || error_node) {
gchar *message;
const gchar *errcode = NULL;
const gchar *error = NULL;
MatrixAPIError error_code = MATRIX_API_ERROR_UNKNOWN_ERROR; MatrixAPIError error_code = MATRIX_API_ERROR_UNKNOWN_ERROR;
/* Set the message as M_CODE: message */ if (errcode_node) {
if ((node = json_object_get_member( GEnumClass *error_class;
root_object, "error")) != NULL) { GEnumValue *value;
message = g_strdup_printf("%s: %s",
errcode, errcode = json_node_get_string(errcode_node);
json_node_get_string(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 { } else {
/* If there is no message, issue a warning and 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);
* set up the message as plain M_CODE */ }
message = g_strdup(errcode); }
} 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;
} }
/* Set the actual GError code according to errcode */ if (error_node) {
if (strcmp("M_MISSING_TOKEN", errcode) == 0) { error = json_node_get_string(error_node);
error_code = MATRIX_API_ERROR_MISSING_TOKEN; }
} else if (strcmp("M_FORBIDDEN", errcode) == 0) {
error_code = MATRIX_API_ERROR_FORBIDDEN; if (errcode_node && error_node) {
} else if (strcmp("M_UNKNOWN", errcode) == 0) { message = g_strdup_printf("%s: %s", errcode, error);
error_code = MATRIX_API_ERROR_UNKNOWN; } 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, err = g_error_new_literal(MATRIX_API_ERROR,
error_code, error_code,
message); 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,