| 1049 | } |
| 1050 | |
| 1051 | static void handle_rpc_reply(const tal_t *working_ctx, |
| 1052 | struct plugin *plugin, const char *buf, const jsmntok_t *toks) |
| 1053 | { |
| 1054 | const jsmntok_t *idtok, *contenttok; |
| 1055 | struct out_req *out; |
| 1056 | struct command_result *res; |
| 1057 | bool cmd_freed; |
| 1058 | |
| 1059 | idtok = json_get_member(buf, toks, "id"); |
| 1060 | if (!idtok) |
| 1061 | /* FIXME: Don't simply ignore notifications! */ |
| 1062 | return; |
| 1063 | |
| 1064 | out = strmap_getn(&plugin->out_reqs, |
| 1065 | json_tok_full(buf, idtok), |
| 1066 | json_tok_full_len(idtok)); |
| 1067 | if (!out) { |
| 1068 | /* This can actually happen, if they free req! */ |
| 1069 | plugin_log(plugin, LOG_DBG, "JSON reply with unknown id '%.*s'", |
| 1070 | json_tok_full_len(toks), |
| 1071 | json_tok_full(buf, toks)); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | /* We want to free this if callback doesn't. */ |
| 1076 | tal_steal(working_ctx, out); |
| 1077 | |
| 1078 | /* If they return complete, cmd should have been freed! */ |
| 1079 | cmd_freed = false; |
| 1080 | tal_add_destructor2(out->cmd, destroy_cmd_mark_freed, &cmd_freed); |
| 1081 | |
| 1082 | trace_span_resume(out); |
| 1083 | contenttok = json_get_member(buf, toks, "error"); |
| 1084 | |
| 1085 | /* Annotate the JSON-RPC span whether it succeeded or failed, |
| 1086 | * and then emit it. */ |
| 1087 | trace_span_tag(out, "error", contenttok ? "true" : "false"); |
| 1088 | trace_span_end(out); |
| 1089 | |
| 1090 | if (contenttok) { |
| 1091 | if (out->errcb) |
| 1092 | res = out->errcb(out->cmd, out->method, buf, contenttok, out->arg); |
| 1093 | else |
| 1094 | res = out->cb(out->cmd, out->method, buf, toks, out->arg); |
| 1095 | } else { |
| 1096 | contenttok = json_get_member(buf, toks, "result"); |
| 1097 | if (!contenttok) |
| 1098 | plugin_err(plugin, "Bad JSONRPC, no 'error' nor 'result': '%.*s'", |
| 1099 | json_tok_full_len(toks), |
| 1100 | json_tok_full(buf, toks)); |
| 1101 | /* errcb is NULL if it's a single whole-object callback */ |
| 1102 | if (out->errcb) |
| 1103 | res = out->cb(out->cmd, out->method, buf, contenttok, out->arg); |
| 1104 | else |
| 1105 | res = out->cb(out->cmd, out->method, buf, toks, out->arg); |
| 1106 | } |
| 1107 | |
| 1108 | if (res == &complete) { |
no test coverage detected