diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-05 16:40:04 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-05 16:40:04 +0200 |
commit | bd8d4e99a97d7a6cc3931af52b36324920620223 (patch) | |
tree | fb95ea4c8923f525181844edcb45efc594d1c498 | |
parent | b44457be7c0b42b0cd7f247b71367e776e69332f (diff) | |
download | merchant-bd8d4e99a97d7a6cc3931af52b36324920620223.tar.gz merchant-bd8d4e99a97d7a6cc3931af52b36324920620223.tar.bz2 merchant-bd8d4e99a97d7a6cc3931af52b36324920620223.zip |
nicer replies
-rw-r--r-- | src/backend/taler-merchant-httpd_responses.c | 40 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_responses.h | 25 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_track-deposit.c | 16 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_track-transaction.c | 20 |
4 files changed, 81 insertions, 20 deletions
diff --git a/src/backend/taler-merchant-httpd_responses.c b/src/backend/taler-merchant-httpd_responses.c index 32e4d24c..3aa94974 100644 --- a/src/backend/taler-merchant-httpd_responses.c +++ b/src/backend/taler-merchant-httpd_responses.c @@ -235,6 +235,46 @@ TMH_RESPONSE_reply_invalid_json (struct MHD_Connection *connection) "invalid json"); } + +/** + * Send a response indicating that we did not find the @a object + * needed for the reply. + * + * @param connection the MHD connection to use + * @param object name of the object we did not find + * @return a MHD result code + */ +int +TMH_RESPONSE_reply_not_found (struct MHD_Connection *connection, + const char *object) +{ + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_NOT_FOUND, + "{s:s}", + "error", + object); +} + + +/** + * Send a response indicating that the request was malformed. + * + * @param connection the MHD connection to use + * @param issue description of what was wrong with the request + * @return a MHD result code + */ +int +TMH_RESPONSE_reply_bad_request (struct MHD_Connection *connection, + const char *issue) +{ + return TMH_RESPONSE_reply_json_pack (connection, + MHD_HTTP_BAD_REQUEST, + "{s:s}", + "error", + issue); +} + + /** * Add headers we want to return in every response. * Useful for testing, like if we want to always close diff --git a/src/backend/taler-merchant-httpd_responses.h b/src/backend/taler-merchant-httpd_responses.h index 7314e0bb..19690cc3 100644 --- a/src/backend/taler-merchant-httpd_responses.h +++ b/src/backend/taler-merchant-httpd_responses.h @@ -94,6 +94,31 @@ TMH_RESPONSE_reply_invalid_json (struct MHD_Connection *connection); /** + * Send a response indicating that we did not find the @a object + * needed for the reply. + * + * @param connection the MHD connection to use + * @param object name of the object we did not find + * @return a MHD result code + */ +int +TMH_RESPONSE_reply_not_found (struct MHD_Connection *connection, + const char *object); + + +/** + * Send a response indicating that the request was malformed. + * + * @param connection the MHD connection to use + * @param issue description of what was wrong with the request + * @return a MHD result code + */ +int +TMH_RESPONSE_reply_bad_request (struct MHD_Connection *connection, + const char *issue); + + +/** * Send a response indicating an internal error. * * @param connection the MHD connection to use diff --git a/src/backend/taler-merchant-httpd_track-deposit.c b/src/backend/taler-merchant-httpd_track-deposit.c index 2654ac68..c7bcc638 100644 --- a/src/backend/taler-merchant-httpd_track-deposit.c +++ b/src/backend/taler-merchant-httpd_track-deposit.c @@ -29,7 +29,7 @@ #include "taler-merchant-httpd_auditors.h" #include "taler-merchant-httpd_exchanges.h" #include "taler-merchant-httpd_responses.h" -#include "taler-merchant-httpd_track_deposit.h" +#include "taler-merchant-httpd_track-deposit.h" /** @@ -367,7 +367,7 @@ proof_cb (void *cls, /** - * Manages a /track/wtid call, thus it calls the /track/deposit + * Manages a /track/deposit call, thus it calls the /track/wtid * offered by the exchange in order to return the set of deposits * (of coins) associated with a given wire transfer. * @@ -430,24 +430,24 @@ MH_handler_track_deposit (struct TMH_RequestHandler *rh, MHD_GET_ARGUMENT_KIND, "exchange"); if (NULL == uri) - return TMH_RESPONSE_reply_external_error (connection, - "exchange argument missing"); + return TMH_RESPONSE_reply_bad_request (connection, + "exchange argument missing"); rctx->uri = GNUNET_strdup (uri); str = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "wtid"); if (NULL == str) - return TMH_RESPONSE_reply_external_error (connection, - "wtid argument missing"); + return TMH_RESPONSE_reply_bad_request (connection, + "wtid argument missing"); if (GNUNET_OK != GNUNET_STRINGS_string_to_data (str, strlen (str), &rctx->wtid, sizeof (rctx->wtid))) { - return TMH_RESPONSE_reply_external_error (connection, - "wtid argument malformed"); + return TMH_RESPONSE_reply_bad_request (connection, + "wtid argument malformed"); } /* Check if reply is already in database! */ diff --git a/src/backend/taler-merchant-httpd_track-transaction.c b/src/backend/taler-merchant-httpd_track-transaction.c index 6744840f..f87cb84b 100644 --- a/src/backend/taler-merchant-httpd_track-transaction.c +++ b/src/backend/taler-merchant-httpd_track-transaction.c @@ -617,14 +617,14 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, MHD_GET_ARGUMENT_KIND, "id"); if (NULL == str) - return TMH_RESPONSE_reply_external_error (connection, - "id argument missing"); + return TMH_RESPONSE_reply_bad_request (connection, + "id argument missing"); if (1 != sscanf (str, "%llu", &transaction_id)) - return TMH_RESPONSE_reply_external_error (connection, - "id argument must be a number"); + return TMH_RESPONSE_reply_bad_request (connection, + "id argument must be a number"); ret = db->find_transaction_by_id (db->cls, transaction_id, @@ -632,10 +632,8 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, tctx); if (GNUNET_NO == ret) { - /* FIXME: generate proper 404 */ - GNUNET_break (0); - return TMH_RESPONSE_reply_external_error (connection, - "Unknown transaction ID"); + return TMH_RESPONSE_reply_not_found (connection, + "id"); } if ( (GNUNET_SYSERR == ret) || (tctx->transaction_id != (uint64_t) transaction_id) || @@ -657,10 +655,8 @@ MH_handler_track_transaction (struct TMH_RequestHandler *rh, } if (GNUNET_NO == ret) { - /* FIXME: generate proper 404 */ - GNUNET_break (0); - return TMH_RESPONSE_reply_external_error (connection, - "No deposits found for transaction ID"); + return TMH_RESPONSE_reply_not_found (connection, + "deposits"); } *connection_cls = tctx; |