@brief Function to encode a single response(no batch) into an emitter. @param resp Response object @param json output yaml emitter
| 217 | /// @param json output yaml emitter |
| 218 | /// |
| 219 | static void |
| 220 | encode(const specs::RPCResponseInfo &resp, YAML::Emitter &json) |
| 221 | { |
| 222 | json << YAML::BeginMap; |
| 223 | json << YAML::Key << "jsonrpc" << YAML::Value << specs::JSONRPC_VERSION; |
| 224 | |
| 225 | // Important! As per specs, errors have preference over the result, we ignore result if error was set. |
| 226 | |
| 227 | if (resp.error.ec) { |
| 228 | // internal library detected error: Decoding, etc. |
| 229 | encode_error(resp.error.ec, resp.error.data, json); |
| 230 | } |
| 231 | // Registered handler error: They have set the error on the response from the registered handler. This uses ExecutionError as |
| 232 | // top error. |
| 233 | else if (!resp.callResult.errata.is_ok()) { |
| 234 | encode_error(resp.callResult.errata, json); |
| 235 | } |
| 236 | // A valid response: The registered handler have set the proper result and no error was flagged. |
| 237 | else { |
| 238 | json << YAML::Key << "result" << YAML::Value; |
| 239 | |
| 240 | // Could be the case that the registered handler did not set the result. Make sure it was set before inserting it. |
| 241 | if (!resp.callResult.result.IsNull()) { |
| 242 | json << resp.callResult.result; |
| 243 | } else { |
| 244 | // TODO: do we want to let it return null or by default we put success when there was no error and no result either. Maybe |
| 245 | // empty? |
| 246 | json << "success"; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // ID. Only if present. |
| 251 | if (!resp.id.empty()) { |
| 252 | json << YAML::Key << "id" << YAML::Value << resp.id; |
| 253 | } |
| 254 | // else: We do not insert null as it will break the json, we need literal null and not ~ (as per yaml) |
| 255 | // json << YAML::Null; |
| 256 | |
| 257 | json << YAML::EndMap; |
| 258 | } |
| 259 | |
| 260 | public: |
| 261 | /// |