{"record":{"id":"219eaa9259bf9f3f","repo":"gchq/CyberChef","slug":"error","errorCode":null,"errorMessage":"${error}","messagePattern":"\\$\\{error\\}","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ProtobufEncode.mjs","lineNumber":48,"sourceCode":"                name: \"Schema (.proto text)\",\n                type: \"text\",\n                value: \"\",\n                rows: 8,\n                hint: \"Drag and drop is enabled on this ingredient\"\n            }\n        ];\n    }\n\n    /**\n     * @param {Object} input\n     * @param {Object[]} args\n     * @returns {ArrayBuffer}\n     */\n    run(input, args) {\n        try {\n            return Protobuf.encode(input, args);\n        } catch (error) {\n            throw new OperationError(error);\n        }\n    }\n\n}\n\nexport default ProtobufEncode;\n","sourceCodeStart":30,"sourceCodeEnd":55,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ProtobufEncode.mjs#L30-L55","documentation":"Protobuf Encode delegates to Protobuf.encode and wraps any thrown error in an OperationError surfaced as ${error}. Encode failures come from JSON that does not conform to the .proto schema: wrong field types, unknown field numbers, missing required fields, an invalid schema, or values that cannot be encoded (e.g. non-integer for an int64 field). The wrapper normalizes internal library errors into CyberChef's OperationError channel.","triggerScenarios":"Passing a JSON object whose keys/types do not match the .proto schema; an unparseable schema; a field value of the wrong type (string for an int field); missing required fields; enum values outside the declared set.","commonSituations":"Schema/data mismatch (encoding against a different schema than the producer used); hand-written JSON with typos in field names or numbers; an empty or invalid .proto; values like floats where integers are required.","solutions":["Ensure the .proto schema is valid (validate with protoc).","Match JSON field names/numbers and types to the schema exactly.","Read the wrapped ${error} message — it identifies the offending field/type.","Start with 'Show Types' on a decode of a known-good message to learn the expected shape, then encode to match."],"exampleFix":"// before: string where int64 expected\nrun({ id: \"abc\" }, [schema]);\n\n// after: numeric value matching schema\nrun({ id: 123 }, [schema]);","handlingStrategy":"try-catch","validationCode":"// Validate schema parses and JSON keys match declared field numbers/types before encoding.\nimport protobuf from \"protobufjs\";\nasync function schemaIsValid(protoText) {\n  try { await protobuf.parse(protoText); return true; } catch { return false; }\n}","typeGuard":"function isPlainObject(v) {\n  return typeof v === \"object\" && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  return protobufEncode.run(inputObj, [schema]);\n} catch (e) {\n  // e.message is the wrapped Protobuf.encode error; fix the offending field\n  throw e;\n}","preventionTips":["Validate the .proto schema with protoc.","Match JSON field names/numbers/types to the schema.","Use 'Show Types' on a known-good decode to learn the expected shape."],"tags":["protobuf","serialization","schema","input-validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}