{"record":{"id":"838d4b7b5010ecba","repo":"gchq/CyberChef","slug":"invalid-encoding-838d4b","errorCode":null,"errorMessage":"Invalid encoding","messagePattern":"Invalid encoding","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/EncodeText.mjs","lineNumber":53,"sourceCode":"        this.outputType = \"ArrayBuffer\";\n        this.args = [\n            {\n                \"name\": \"Encoding\",\n                \"type\": \"option\",\n                \"value\": Object.keys(CHR_ENC_CODE_PAGES)\n            }\n        ];\n    }\n\n    /**\n     * @param {string} input\n     * @param {Object[]} args\n     * @returns {ArrayBuffer}\n     */\n    run(input, args) {\n        const format = CHR_ENC_CODE_PAGES[args[0]];\n        if (!format) {\n            throw new OperationError(\"Invalid encoding\");\n        }\n        const encoded = cptable.utils.encode(format, input);\n        return new Uint8Array(encoded).buffer;\n    }\n\n}\n\n\nexport default EncodeText;\n","sourceCodeStart":35,"sourceCodeEnd":63,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/EncodeText.mjs#L35-L63","documentation":"Thrown in EncodeText.run when CHR_ENC_CODE_PAGES[args[0]] is falsy. The Encoding arg is an `option` dropdown whose values are exactly Object.keys(CHR_ENC_CODE_PAGES), so through the normal UI this error is effectively unreachable. It becomes reachable when a recipe is built or imported programmatically (Node API or hand-edited JSON) and references an encoding name absent from the table (typo, removed alias, or a name from a different CyberChef version).","triggerScenarios":"args[0] is a string not present as a key in CHR_ENC_CODE_PAGES - a typo ('UTF-8 ' trailing space), a case mismatch, a name removed in this build, or an arbitrary string injected via recipe JSON / Node API bypassing the dropdown.","commonSituations":"Sharing a recipe across CyberChef versions where the encoding list changed; importing recipe JSON with a hand-typed encoding name; programmatic Node usage passing an unvalidated encoding string.","solutions":["Use an encoding name exactly as it appears in the operation's dropdown (it enumerates CHR_ENC_CODE_PAGES keys).","For programmatic use, validate the name against Object.keys(CHR_ENC_CODE_PAGES) before calling.","When importing recipes, confirm each encoding still exists in the target version.","Avoid trailing spaces / case drift in encoding names."],"exampleFix":"// before\nchef.encodeText(text, ['UTF8 ']);        // typo/whitespace -> throws\n// after\nchef.encodeText(text, ['UTF-8']);         // exact key from CHR_ENC_CODE_PAGES","handlingStrategy":"validation","validationCode":"import { CHR_ENC_CODE_PAGES } from \"../core/lib/ChrEnc.mjs\";\nconst validEncodings = Object.keys(CHR_ENC_CODE_PAGES);\nif (!validEncodings.includes(encoding)) throw new Error(`unknown encoding; pick one of: ${validEncodings.join(\", \")}`);","typeGuard":"const isKnownEncoding = (name) => Object.prototype.hasOwnProperty.call(CHR_ENC_CODE_PAGES, name);","tryCatchPattern":null,"preventionTips":["When using the Node API, choose encoding names from Object.keys(CHR_ENC_CODE_PAGES).","Validate imported recipe JSON encoding names against the current version's list.","Avoid typos, trailing spaces, and case drift in encoding names."],"tags":["encoding","text","codepage","input-validation","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}