{"record":{"id":"ca242da57f26ddf6","repo":"gchq/CyberChef","slug":"err-tostring-ca242d","errorCode":null,"errorMessage":"${err.toString()}","messagePattern":"\\$\\{err\\.toString\\(\\)\\}","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/BSONSerialise.mjs","lineNumber":44,"sourceCode":"        this.inputType = \"string\";\n        this.outputType = \"ArrayBuffer\";\n        this.args = [];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {ArrayBuffer}\n     */\n    run(input, args) {\n        if (!input) return new ArrayBuffer();\n\n        try {\n            const data = JSON.parse(input);\n            const result = serialize(data);\n            return result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength);\n        } catch (err) {\n            throw new OperationError(err.toString());\n        }\n    }\n\n}\n\nexport default BSONSerialise;\n","sourceCodeStart":26,"sourceCodeEnd":51,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/BSONSerialise.mjs#L26-L51","documentation":"Generic catch-all thrown by BSONSerialise.run when either JSON.parse(input) or the bson serialize() throws. The operation first parses the input as JSON, then serialises the resulting value to BSON; a JSON syntax error or a value BSON cannot represent (e.g. deeply nested object, unsupported type, key containing a null byte) surfaces as the original Error.toString() in the message.","triggerScenarios":"Input that is not valid JSON (trailing comma, unquoted keys, single quotes), or valid JSON whose values are not BSON-representable (e.g. very large numbers losing precision, undefined, functions, or symbol-keyed objects).","commonSituations":"Pasting hand-written JSON with syntax errors; feeding a JS object literal instead of JSON; numbers exceeding BSON's 64-bit integer range; keys with null bytes.","solutions":["Validate the input as well-formed JSON first (e.g. with a JSON linter or JSON.parse in isolation).","Ensure all values are BSON-compatible types (use strings/dates for large numbers).","Inspect the forwarded Error.toString() message for the exact parse or serialise failure."],"exampleFix":"// before - single quotes / unquoted keys\nchef.bSONSerialise(\"{a: '1'}\");\n\n// after - valid JSON\nchef.bSONSerialise('{\"a\":1}');","handlingStrategy":"validation","validationCode":"function assertJsonForBson(input) {\n  let data;\n  try { data = JSON.parse(input); } catch (e) {\n    throw new Error(`Input is not valid JSON: ${e.message}`);\n  }\n  if (data === undefined) throw new Error(\"JSON parsed to undefined; not BSON-representable\");\n  return data;\n}\nassertJsonForBson(input);","typeGuard":"function isParsableJson(s) {\n  try { JSON.parse(s); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  chef.bSONSerialise(input);\n} catch (e) {\n  if (/JSON|position|Unexpected/i.test(e.message)) {\n    throw new Error(`Invalid JSON for BSON: ${e.message}`);\n  }\n  throw e;\n}","preventionTips":["Run the input through a JSON linter or JSON.parse first.","Use JSON (double quotes), not JS object literals.","Avoid values BSON cannot represent (undefined, functions, oversized integers)."],"tags":["bson","serialization","format","json"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}