add convenience function TALER_TEMPLATING_reply_error

This commit is contained in:
Christian Grothoff 2023-05-06 20:55:40 +02:00
parent 1f9427e1d9
commit 404b2b78f1
No known key found for this signature in database
GPG Key ID: 939E6BE1E29FC3CC
3 changed files with 69 additions and 1 deletions

View File

@ -91,6 +91,26 @@ TALER_TEMPLATING_reply (struct MHD_Connection *connection,
const char *taler_uri, const char *taler_uri,
const json_t *root); const json_t *root);
/**
* Load a @a template and substitute an error message based on @a ec and @a
* detail, returning the result to the @a connection with the given @a
* http_status code.
*
* @param connection the connection we act upon
* @param template basename of the template to load
* @param http_status code to use on success
* @param ec error code to return
* @param detail optional text to add to the template
* @return #MHD_YES on success, #MHD_NO to just close the connection
*/
MHD_RESULT
TALER_TEMPLATING_reply_error (struct MHD_Connection *connection,
const char *template_basename,
unsigned int http_status,
enum TALER_ErrorCode ec,
const char *detail);
/** /**
* Preload templates. * Preload templates.
* *

View File

@ -364,7 +364,16 @@ TALER_MHD_check_content_length_ (struct MHD_Connection *connection,
MHD_HEADER_KIND, MHD_HEADER_KIND,
MHD_HTTP_HEADER_CONTENT_LENGTH); MHD_HTTP_HEADER_CONTENT_LENGTH);
if (NULL == cl) if (NULL == cl)
return GNUNET_OK; {
GNUNET_break_op (0);
return (MHD_YES ==
TALER_MHD_reply_with_error (connection,
MHD_HTTP_BAD_REQUEST,
TALER_EC_GENERIC_PARAMETER_MISSING,
MHD_HTTP_HEADER_CONTENT_LENGTH))
? GNUNET_NO
: GNUNET_SYSERR;
}
if (1 != sscanf (cl, if (1 != sscanf (cl,
"%llu%c", "%llu%c",
&cv, &cv,

View File

@ -428,6 +428,45 @@ load_template (void *cls,
} }
MHD_RESULT
TALER_TEMPLATING_reply_error (struct MHD_Connection *connection,
const char *template_basename,
unsigned int http_status,
enum TALER_ErrorCode ec,
const char *detail)
{
json_t *data;
enum GNUNET_GenericReturnValue ret;
data = GNUNET_JSON_PACK (
GNUNET_JSON_pack_uint64 ("ec",
ec),
GNUNET_JSON_pack_string ("hint",
TALER_ErrorCode_get_hint (ec)),
GNUNET_JSON_pack_string ("detail",
detail)
);
ret = TALER_TEMPLATING_reply (connection,
http_status,
template_basename,
NULL,
NULL,
data);
json_decref (data);
switch (ret)
{
case GNUNET_OK:
return MHD_YES;
case GNUNET_NO:
return MHD_YES;
case GNUNET_SYSERR:
return MHD_NO;
}
GNUNET_assert (0);
return MHD_NO;
}
enum GNUNET_GenericReturnValue enum GNUNET_GenericReturnValue
TALER_TEMPLATING_init (const char *subsystem) TALER_TEMPLATING_init (const char *subsystem)
{ {