gchq/CyberChef · error · OperationError
Error generating QR code. (${err})
Error message
Error generating QR code. (${err}) What it means
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.
Source
Thrown at src/core/lib/QRCode.mjs:96
margin,
errorCorrection,
) {
const formats = ["SVG", "EPS", "PDF", "PNG"];
if (!formats.includes(format.toUpperCase())) {
throw new OperationError("Unsupported QR code format.");
}
let qrImage;
try {
qrImage = qr.imageSync(input, {
type: format,
size: moduleSize,
margin: margin,
// eslint-disable-next-line camelcase
ec_level: errorCorrection.charAt(0).toUpperCase(),
});
} catch (err) {
throw new OperationError(`Error generating QR code. (${err})`);
}
if (!qrImage) {
throw new OperationError("Error generating QR code.");
}
switch (format) {
case "SVG":
case "EPS":
case "PDF":
return Utils.strToArrayBuffer(qrImage);
case "PNG":
return qrImage.buffer.slice(qrImage.byteOffset, qrImage.byteLength + qrImage.byteOffset);
default:
throw new OperationError("Unsupported QR code format.");
}
}
View on GitHub (pinned to 4290ea7539)
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).
Example fix
// before generateQrCode(hugeString, 'PNG', 4, 4, 'H'); // after generateQrCode(hugeString.slice(0, 1000), 'PNG', 4, 4, 'L');
Defensive patterns
Strategy: validation
Validate before calling
const EC_LEVELS = new Set(['L','M','Q','H']);
const ec = String(errorCorrection || '').charAt(0).toUpperCase();
if (!EC_LEVELS.has(ec)) throw new Error('Invalid error correction level');
if (typeof input !== 'string' || !input) throw new Error('Input must be a non-empty string');
generateQrCode(input, format, moduleSize, margin, ec); Type guard
function isEcLevel(v) {
return ['L','M','Q','H'].includes(String(v).charAt(0).toUpperCase());
} Try / catch
try {
return generateQrCode(input, format, moduleSize, margin, errorCorrection);
} catch (e) {
if (e instanceof OperationError && /Error generating QR code/.test(e.message)) {
// shorten input or lower EC to 'L' and retry
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Error generating QR code.
- Error opening image. (${err})
- Could not read a QR code from the image.
- Unsupported QR code format.
- Error normalising image. (${err})
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/44282770cf71472b.
Report an issue: GitHub.