{"record":{"id":"97478ea1313dd1c6","repo":"usebruno/bruno","slug":"failed-to-parse-context-error-message","errorCode":null,"errorMessage":"Failed to parse ${context}: ${error.message}","messagePattern":"Failed to parse (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-requests/src/grpc/grpc-client.js","lineNumber":67,"sourceCode":"    return data;\n  }\n  return Buffer.from(data, 'utf-8');\n};\n\n/**\n * Safely parse JSON string with error handling\n * @param {string} jsonString - The JSON string to parse\n * @param {string} context - Context for error messages (e.g., 'message content', 'request body')\n * @returns {Object} Parsed object or throws error with context\n * @throws {Error} If JSON parsing fails\n */\nconst safeJsonParse = (jsonString, context = 'JSON string') => {\n  try {\n    return JSON.parse(jsonString);\n  } catch (error) {\n    const errorMessage = `Failed to parse ${context}: ${error.message}`;\n    console.error(errorMessage, { originalString: jsonString, parseError: error });\n    throw new Error(errorMessage);\n  }\n};\n\nconst processGrpcMetadata = (metadata) => {\n  return Object.entries(metadata).map(([name, value]) => {\n    if (Array.isArray(value)) {\n      return {\n        name,\n        value: value\n          .map((v) => {\n            if (v && typeof v === 'object' && v.type === 'Buffer' && Array.isArray(v.data)) {\n              return Buffer.from(v.data).toString('base64');\n            }\n            return v.toString();\n          })\n          .join(', ')\n      };\n    }","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-requests/src/grpc/grpc-client.js#L49-L85","documentation":"Thrown by safeJsonParse when JSON.parse fails on a string passed into the gRPC client (message content, request body, response metadata). The helper wraps the raw parse error with the context label and also console.errors the original string and parse error before re-throwing.","triggerScenarios":"Invoking a gRPC method whose message body or metadata value is declared/passed as JSON but contains malformed JSON — unclosed braces, single quotes, trailing commas, or non-JSON plain text in a field the client tries to parse.","commonSituations":"User typed a request body in the Bruno gRPC UI that isn't valid JSON; protobuf JSON representation uses a field the user formatted incorrectly (e.g. Int64Value, Timestamp); a streaming message chunk was truncated mid-payload.","solutions":["Validate the JSON in the request body with a linter before sending.","If the field is genuinely plain text, do not route it through the JSON parser — check the proto definition for the field type.","Catch the error and surface the original string (already logged to console) to locate the syntax error.","For streaming, ensure you are not feeding the parser half a buffered message."],"exampleFix":"// before — body field contains malformed JSON\nclient.invoke('/pkg.Svc/Method', { body: \"{name: 'abc'}\" });  // unquoted key, single quotes\n\n// after\nclient.invoke('/pkg.Svc/Method', { body: JSON.stringify({ name: 'abc' }) });","handlingStrategy":"try-catch","validationCode":"function safeJsonParseLocal(s, ctx='JSON string') {\n  try { return JSON.parse(s); }\n  catch (e) { throw new Error(`Failed to parse ${ctx}: ${e.message}`); }\n}\n// pre-validate user-supplied body before sending to the gRPC client\nif (typeof body === 'string') safeJsonParseLocal(body, 'request body');","typeGuard":"function isJsonString(s) {\n  if (typeof s !== 'string') return false;\n  try { JSON.parse(s); return true; } catch { return false; }\n}","tryCatchPattern":"try { client.invoke(path, { body }); }\ncatch (e) {\n  if (e.message.startsWith('Failed to parse')) { /* show original string, fix JSON */ }\n  else throw e;\n}","preventionTips":["Run request bodies through a JSON validator in the UI before send.","For text-typed proto fields, do not mark them as JSON.","When streaming, buffer complete messages before parsing."],"tags":["grpc","json","validation","runtime"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}