{"record":{"id":"d89e442f2b5275bd","repo":"gchq/CyberChef","slug":"error-generating-qr-code","errorCode":null,"errorMessage":"Error generating QR code.","messagePattern":"Error generating QR code\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/QRCode.mjs","lineNumber":100,"sourceCode":"    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":82,"sourceCodeEnd":114,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/QRCode.mjs#L82-L114","documentation":"Thrown by generateQrCode when `qr.imageSync` returns a falsy value (null/undefined/empty) without throwing — qr-image signalled failure silently. Treated as a generation failure distinct from the thrown-exception case.","triggerScenarios":"An input/option combination that qr-image handles by returning nothing rather than throwing — e.g. empty string input, or an option set the installed qr-image version refuses.","commonSituations":"Empty input string; qr-image version regression; mismatch between the `type` code passed and what qr-image expects (case sensitivity of `type`).","solutions":["Ensure input is a non-empty string.","Pass the format in the case qr-image expects (the call forwards `type: format` verbatim — see also error 134).","Pin a known-good qr-image version."],"exampleFix":"// before\ngenerateQrCode('', 'PNG', 4, 4, 'M');\n// after\ngenerateQrCode('https://example.com', 'PNG', 4, 4, 'M');","handlingStrategy":"validation","validationCode":"if (typeof input !== 'string' || input.length === 0) {\n  throw new Error('QR input must be a non-empty string');\n}","typeGuard":"function isNonEmptyString(v) { return typeof v === 'string' && v.length > 0; }","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    // qr-image returned nothing — try SVG format or different EC\n  }\n  throw e;\n}","preventionTips":["Never pass empty input to generateQrCode.","Pass the format in the exact case qr-image expects.","Pin a known-good qr-image version."],"tags":["qrcode","generation","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}