From 4bba7263564303f45eed86cc68e54bbbf00856a6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Tue, 25 Aug 2020 15:11:56 +0200 Subject: nicer generic error reporting logic, for #6432 --- src/mhd/mhd_legal.c | 1 + src/mhd/mhd_parsing.c | 78 +++++++++++++++++++++++++------------------------ src/mhd/mhd_responses.c | 18 +++++++----- 3 files changed, 51 insertions(+), 46 deletions(-) (limited to 'src/mhd') diff --git a/src/mhd/mhd_legal.c b/src/mhd/mhd_legal.c index 0f2433c2c..d505f662f 100644 --- a/src/mhd/mhd_legal.c +++ b/src/mhd/mhd_legal.c @@ -24,6 +24,7 @@ #include #include #include +#include "taler_util.h" #include "taler_mhd_lib.h" diff --git a/src/mhd/mhd_parsing.c b/src/mhd/mhd_parsing.c index 2a086e117..a9f627491 100644 --- a/src/mhd/mhd_parsing.c +++ b/src/mhd/mhd_parsing.c @@ -76,11 +76,11 @@ TALER_MHD_parse_post_json (struct MHD_Connection *connection, case GNUNET_JSON_PR_OUT_OF_MEMORY: GNUNET_break (NULL == *json); return (MHD_NO == - TALER_MHD_reply_with_error - (connection, + TALER_MHD_reply_with_error ( + connection, MHD_HTTP_INTERNAL_SERVER_ERROR, TALER_EC_PARSER_OUT_OF_MEMORY, - "out of memory")) ? GNUNET_SYSERR : GNUNET_NO; + NULL)) ? GNUNET_SYSERR : GNUNET_NO; case GNUNET_JSON_PR_CONTINUE: GNUNET_break (NULL == *json); @@ -96,7 +96,7 @@ TALER_MHD_parse_post_json (struct MHD_Connection *connection, TALER_MHD_reply_with_error (connection, MHD_HTTP_BAD_REQUEST, TALER_EC_JSON_INVALID, - "invalid JSON uploaded")) + NULL)) ? GNUNET_NO : GNUNET_SYSERR; case GNUNET_JSON_PR_SUCCESS: GNUNET_break (NULL != *json); @@ -204,15 +204,15 @@ TALER_MHD_parse_json_data (struct MHD_Connection *connection, if (NULL == error_json_name) error_json_name = ""; ret = (MHD_YES == - TALER_MHD_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:s, s:I}", - "hint", "JSON parse error", - "code", - (json_int_t) - TALER_EC_JSON_INVALID_WITH_DETAILS, - "field", error_json_name, - "line", (json_int_t) error_line)) + TALER_MHD_reply_json_pack ( + connection, + MHD_HTTP_BAD_REQUEST, + "{s:s, s:I, s:s, s:I}", + "hint", TALER_ErrorCode_get_hint ( + TALER_EC_JSON_INVALID_WITH_DETAILS), + "code", (json_int_t) TALER_EC_JSON_INVALID_WITH_DETAILS, + "field", error_json_name, + "line", (json_int_t) error_line)) ? GNUNET_NO : GNUNET_SYSERR; return ret; } @@ -256,15 +256,15 @@ TALER_MHD_parse_internal_json_data (struct MHD_Connection *connection, if (NULL == error_json_name) error_json_name = ""; ret = (MHD_YES == - TALER_MHD_reply_json_pack (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, - "{s:s, s:I, s:s, s:I}", - "hint", "JSON parse error", - "code", - (json_int_t) - TALER_EC_INTERNAL_INVARIANT_FAILURE, - "field", error_json_name, - "line", (json_int_t) error_line)) + TALER_MHD_reply_json_pack ( + connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + "{s:s, s:I, s:s, s:I}", + "hint", TALER_ErrorCode_get_hint ( + TALER_EC_INTERNAL_INVARIANT_FAILURE), + "code", (json_int_t) TALER_EC_INTERNAL_INVARIANT_FAILURE, + "field", error_json_name, + "line", (json_int_t) error_line)) ? GNUNET_NO : GNUNET_SYSERR; return ret; } @@ -311,14 +311,15 @@ TALER_MHD_parse_json_array (struct MHD_Connection *connection, if (NULL == root) { ret = (MHD_YES == - TALER_MHD_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:I}", - "hint", "expected array", - "code", - (json_int_t) - TALER_EC_JSON_INVALID_WITH_DETAILS, - "dimension", dim)) + TALER_MHD_reply_json_pack ( + connection, + MHD_HTTP_BAD_REQUEST, + "{s:s, s:I, s:s, s:I}", + "hint", TALER_ErrorCode_get_hint ( + TALER_EC_JSON_INVALID_WITH_DETAILS), + "code", (json_int_t) TALER_EC_JSON_INVALID_WITH_DETAILS, + "detail", "expected array", + "dimension", dim)) ? GNUNET_NO : GNUNET_SYSERR; return ret; } @@ -331,14 +332,15 @@ TALER_MHD_parse_json_array (struct MHD_Connection *connection, if (NULL == error_json_name) error_json_name = ""; ret = (MHD_YES == - TALER_MHD_reply_json_pack (connection, - MHD_HTTP_BAD_REQUEST, - "{s:s, s:I, s:I}", - "hint", error_json_name, - "code", - (json_int_t) - TALER_EC_JSON_INVALID_WITH_DETAILS, - "line", (json_int_t) error_line)) + TALER_MHD_reply_json_pack ( + connection, + MHD_HTTP_BAD_REQUEST, + "{s:s, s:s, s:I, s:I}", + "detail", error_json_name, + "hint", TALER_ErrorCode_get_hint ( + TALER_EC_JSON_INVALID_WITH_DETAILS), + "code", (json_int_t) TALER_EC_JSON_INVALID_WITH_DETAILS, + "line", (json_int_t) error_line)) ? GNUNET_NO : GNUNET_SYSERR; return ret; } diff --git a/src/mhd/mhd_responses.c b/src/mhd/mhd_responses.c index 45a9932d3..5ed82cd2a 100644 --- a/src/mhd/mhd_responses.c +++ b/src/mhd/mhd_responses.c @@ -404,16 +404,17 @@ TALER_MHD_make_json_pack (const char *fmt, * Create a response indicating an internal error. * * @param ec error code to return - * @param hint hint about the internal error's nature + * @param detail additional optional detail about the error, can be NULL * @return a MHD response object */ struct MHD_Response * TALER_MHD_make_error (enum TALER_ErrorCode ec, - const char *hint) + const char *detail) { - return TALER_MHD_make_json_pack ("{s:I, s:s}", + return TALER_MHD_make_json_pack ("{s:I, s:s, s:s?}", "code", (json_int_t) ec, - "hint", hint); + "hint", TALER_ErrorCode_get_hint (ec), + "detail", detail); } @@ -423,20 +424,21 @@ TALER_MHD_make_error (enum TALER_ErrorCode ec, * @param connection the MHD connection to use * @param ec error code uniquely identifying the error * @param http_status HTTP status code to use - * @param hint human readable hint about the error + * @param detail additional optional detail about the error, can be NULL * @return a MHD result code */ MHD_RESULT TALER_MHD_reply_with_error (struct MHD_Connection *connection, unsigned int http_status, enum TALER_ErrorCode ec, - const char *hint) + const char *detail) { return TALER_MHD_reply_json_pack (connection, http_status, - "{s:I, s:s}", + "{s:I, s:s, s:s?}", "code", (json_int_t) ec, - "hint", hint); + "hint", TALER_ErrorCode_get_hint (ec), + "detail", detail); } -- cgit v1.2.3