From 551dfedc4a4c55f58d5499f53eba52a1d0bbd4d0 Mon Sep 17 00:00:00 2001 From: ZmnSCPxj Date: Sat, 27 Jan 2018 01:39:46 +0000 Subject: [PATCH] jsonrpc: Internal: Support changing the error code internally. --- lightningd/jsonrpc.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index f5472cbd3..e1ecfcd5d 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -319,8 +319,11 @@ static const struct json_command *find_cmd(const char *buffer, return NULL; } -static void json_result(struct json_connection *jcon, - const char *id, const char *res, const char *err) +static void connection_result(struct json_connection *jcon, + const char *id, + const char *res, + const char *err, + int code) { struct json_output *out = tal(jcon, struct json_output); if (err == NULL) @@ -332,8 +335,11 @@ static void json_result(struct json_connection *jcon, else out->json = tal_fmt(out, "{ \"jsonrpc\": \"2.0\", " - " \"error\" : { \"code\" : -1, \"message\" : %s}," + " \"error\" : " + "{ \"code\" : %d," + " \"message\" : %s}," " \"id\" : %s }\n", + code, err, id); /* Queue for writing, and wake writer (and maybe reader). */ @@ -416,6 +422,8 @@ void json_add_address(struct json_result *response, const char *fieldname, json_object_end(response); } +#define JSONRPC2_INVALID_REQUEST -32600 + void command_success(struct command *cmd, struct json_result *result) { struct json_connection *jcon = cmd->jcon; @@ -427,7 +435,7 @@ void command_success(struct command *cmd, struct json_result *result) return; } assert(jcon->current == cmd); - json_result(jcon, cmd->id, json_result_string(result), NULL); + connection_result(jcon, cmd->id, json_result_string(result), NULL, 0); log_debug(jcon->log, "Success"); jcon->current = tal_free(cmd); } @@ -459,7 +467,7 @@ void command_fail(struct command *cmd, const char *fmt, ...) quote = tal_fmt(cmd, "\"%s\"", error); assert(jcon->current == cmd); - json_result(jcon, cmd->id, NULL, quote); + connection_result(jcon, cmd->id, NULL, quote, -1); jcon->current = tal_free(cmd); } @@ -474,7 +482,8 @@ static void json_command_malformed(struct json_connection *jcon, const char *id, const char *error) { - return json_result(jcon, id, NULL, error); + return connection_result(jcon, id, NULL, error, + JSONRPC2_INVALID_REQUEST); } static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])