{"record":{"id":"44282770cf71472b","repo":"gchq/CyberChef","slug":"error-generating-qr-code-err","errorCode":null,"errorMessage":"Error generating QR code. (${err})","messagePattern":"Error generating QR code\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/QRCode.mjs","lineNumber":96,"sourceCode":"    margin,\n    errorCorrection,\n) {\n    const formats = [\"SVG\", \"EPS\", \"PDF\", \"PNG\"];\n    if (!formats.includes(format.toUpperCase())) {\n        throw new OperationError(\"Unsupported QR code format.\");\n    }\n\n    let qrImage;\n    try {\n        qrImage = qr.imageSync(input, {\n            type: format,\n            size: moduleSize,\n            margin: margin,\n            // eslint-disable-next-line camelcase\n            ec_level: errorCorrection.charAt(0).toUpperCase(),\n        });\n    } catch (err) {\n        throw new OperationError(`Error generating QR code. (${err})`);\n    }\n\n    if (!qrImage) {\n        throw new OperationError(\"Error generating QR code.\");\n    }\n\n    switch (format) {\n        case \"SVG\":\n        case \"EPS\":\n        case \"PDF\":\n            return Utils.strToArrayBuffer(qrImage);\n        case \"PNG\":\n            return qrImage.buffer.slice(qrImage.byteOffset, qrImage.byteLength + qrImage.byteOffset);\n        default:\n            throw new OperationError(\"Unsupported QR code format.\");\n    }\n}\n","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/QRCode.mjs#L78-L114","documentation":"Thrown by generateQrCode when `qr.imageSync` raises — typically because the input string is too long for the selected error-correction level, contains data the qr-image encoder rejects, or an unsupported option combination. The qr-image error is appended.","triggerScenarios":"Calling generateQrCode with a very long input that exceeds QR capacity at the chosen ec_level; invalid ec_level char (errorCorrection.charAt(0) yields a non-M/L/H/Q letter); passing an object/undefined as input.","commonSituations":"Encoding a URL or blob over ~2,953 bytes (max for version 40, low EC); feeding binary that qr-image cannot encode; ec_level dropdown misconfigured.","solutions":["Shorten the input or lower error correction to 'L'.","Ensure errorCorrection starts with one of 'L','M','Q','H'.","Pass a plain string (convert binary to base64 first if needed)."],"exampleFix":"// before\ngenerateQrCode(hugeString, 'PNG', 4, 4, 'H');\n// after\ngenerateQrCode(hugeString.slice(0, 1000), 'PNG', 4, 4, 'L');","handlingStrategy":"validation","validationCode":"const EC_LEVELS = new Set(['L','M','Q','H']);\nconst ec = String(errorCorrection || '').charAt(0).toUpperCase();\nif (!EC_LEVELS.has(ec)) throw new Error('Invalid error correction level');\nif (typeof input !== 'string' || !input) throw new Error('Input must be a non-empty string');\ngenerateQrCode(input, format, moduleSize, margin, ec);","typeGuard":"function isEcLevel(v) {\n  return ['L','M','Q','H'].includes(String(v).charAt(0).toUpperCase());\n}","tryCatchPattern":"try {\n  return generateQrCode(input, format, moduleSize, margin, errorCorrection);\n} catch (e) {\n  if (e instanceof OperationError && /Error generating QR code/.test(e.message)) {\n    // shorten input or lower EC to 'L' and retry\n  }\n  throw e;\n}","preventionTips":["Keep input under the QR capacity for the chosen EC level.","Use one of L/M/Q/H for errorCorrection.","Convert binary to text (e.g. base64) before passing."],"tags":["qrcode","generation","capacity","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}