Skip to content

Commit

Permalink
Merge pull request #4427 from garlick/respond_error_0
Browse files Browse the repository at this point in the history
libflux: handle flux_respond_error (errnum=0)
  • Loading branch information
mergify[bot] authored Jul 26, 2022
2 parents d1c1075 + d696136 commit e4b232f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/man3/flux_respond.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ building the payload using variable arguments with a format string in
the style of jansson's ``json_pack()`` (used internally).

``flux_respond_error()`` returns an error response to the sender.
*errnum* must be non-zero. If *errmsg* is non-NULL, an error string
If *errnum* is zero, EINVAL is used. If *errmsg* is non-NULL, an error string
payload is included in the response. The error string may be used to
provide a more detailed error message than can be conveyed via *errnum*.

Expand Down
4 changes: 3 additions & 1 deletion src/common/libflux/response.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,10 @@ int flux_respond_error (flux_t *h, const flux_msg_t *request,
{
flux_msg_t *msg = NULL;

if (!h || !request || errnum == 0)
if (!h || !request)
goto inval;
if (errnum == 0)
errnum = EINVAL;
if (flux_msg_is_noresponse (request))
return 0;
msg = flux_response_derive (request, errnum);
Expand Down
3 changes: 2 additions & 1 deletion src/common/libflux/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ int flux_respond_raw (flux_t *h, const flux_msg_t *request,
const void *data, int len);

/* Create an error response to the provided request message with optional
* error string payload (if errstr is non-NULL).
* error string payload (if errstr is non-NULL). If errnum is zero, EINVAL
* is substituted.
*/
int flux_respond_error (flux_t *h, const flux_msg_t *request,
int errnum, const char *errstr);
Expand Down
18 changes: 18 additions & 0 deletions src/common/libflux/test/response.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ int main (int argc, char *argv[])
{
flux_t *h;
flux_msg_t *msg;
flux_msg_t *msg2;
const char *topic, *s;
const char *json_str = "{\"a\":42}";
const void *d;
const char data[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
int l, len = strlen (data);
int errnum;

plan (NO_PLAN);

Expand Down Expand Up @@ -168,6 +170,22 @@ int main (int argc, char *argv[])
"flux_respond_error msg=NULL fails with EINVAL");
flux_close (h);

/* errnum=0 */
h = loopback_create (0);
if (!h)
BAIL_OUT ("loopback_create");
msg = flux_request_encode ("foo", NULL);
if (!msg)
BAIL_OUT ("flux_request_encode failed");
ok (flux_respond_error (h, msg, 0, NULL) == 0,
"flux_respond_error errno=0 works");
msg2 = flux_recv (h, FLUX_MATCH_ANY, 0);
ok (flux_msg_get_errnum (msg2, &errnum) == 0 && errnum == EINVAL,
"and send a response message with errnum=EINVAL");
flux_msg_destroy (msg2);
flux_msg_destroy (msg);
flux_close (h);

done_testing();
return (0);
}
Expand Down

0 comments on commit e4b232f

Please sign in to comment.