| 340 | } |
| 341 | |
| 342 | char *cbm_jsonrpc_format_error(int64_t id, int code, const char *message) { |
| 343 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 344 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 345 | yyjson_mut_doc_set_root(doc, root); |
| 346 | |
| 347 | yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0"); |
| 348 | yyjson_mut_obj_add_int(doc, root, "id", id); |
| 349 | |
| 350 | char *encoded_message = NULL; |
| 351 | size_t encoded_message_len = 0; |
| 352 | const char *raw_message = message ? message : ""; |
| 353 | if (!cbm_output_encode_text(raw_message, strlen(raw_message), &encoded_message, |
| 354 | &encoded_message_len)) { |
| 355 | yyjson_mut_doc_free(doc); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | yyjson_mut_val *err = yyjson_mut_obj(doc); |
| 360 | yyjson_mut_obj_add_int(doc, err, "code", code); |
| 361 | yyjson_mut_obj_add_str(doc, err, "message", encoded_message ? encoded_message : raw_message); |
| 362 | yyjson_mut_obj_add_val(doc, root, "error", err); |
| 363 | |
| 364 | char *out = yy_final_doc_to_str(doc); |
| 365 | yyjson_mut_doc_free(doc); |
| 366 | free(encoded_message); |
| 367 | return out; |
| 368 | } |
| 369 | |
| 370 | /* ══════════════════════════════════════════════════════════════════ |
| 371 | * MCP PROTOCOL HELPERS |
no test coverage detected