{"record":{"id":"3dcc41b163a17ab5","repo":"can1357/oh-my-pi","slug":"data-too-long-for-a-qr-code-data-length-bytes","errorCode":null,"errorMessage":"data too long for a QR code (${data.length} bytes, EC ${ecLevel})","messagePattern":"data too long for a QR code \\((.+?) bytes, EC (.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/utils/qrcode.ts","lineNumber":196,"sourceCode":"\n\t/** Encode a string in byte mode (UTF-8). Throws if it exceeds version 40. */\n\tstatic encodeText(text: string, ecLevel: QrEcLevel = \"M\", options?: QrEncodeOptions): QrCode {\n\t\treturn QrCode.encodeBytes(new TextEncoder().encode(text), ecLevel, options);\n\t}\n\n\t/** Encode raw bytes in byte mode. Throws if they exceed version 40 at this EC level. */\n\tstatic encodeBytes(data: Uint8Array, ecLevel: QrEcLevel = \"M\", options?: QrEncodeOptions): QrCode {\n\t\tconst ec = EC_LEVELS[ecLevel];\n\t\tconst minVersion = Math.max(MIN_VERSION, options?.minVersion ?? MIN_VERSION);\n\t\tconst maxVersion = Math.min(MAX_VERSION, options?.maxVersion ?? MAX_VERSION);\n\n\t\tlet version = minVersion;\n\t\tfor (; ; version++) {\n\t\t\tconst capacityBits = dataCodewords(version, ec.table) * 8;\n\t\t\tconst usedBits = 4 + charCountBits(version) + data.length * 8;\n\t\t\tif (usedBits <= capacityBits) break;\n\t\t\tif (version >= maxVersion) {\n\t\t\t\tthrow new Error(`data too long for a QR code (${data.length} bytes, EC ${ecLevel})`);\n\t\t\t}\n\t\t}\n\n\t\tconst bits = new BitBuffer();\n\t\tbits.append(BYTE_MODE_INDICATOR, 4);\n\t\tbits.append(data.length, charCountBits(version));\n\t\tfor (const b of data) bits.append(b, 8);\n\n\t\tconst capacityBits = dataCodewords(version, ec.table) * 8;\n\t\tbits.append(0, Math.min(4, capacityBits - bits.length)); // terminator\n\t\tbits.append(0, (8 - (bits.length % 8)) % 8); // byte-align\n\t\tfor (let pad = 0; bits.length < capacityBits; pad ^= 1) bits.append(PAD_BYTES[pad]!, 8);\n\n\t\tconst codewords = QrCode.#interleave(bits.toBytes(), version, ec.table);\n\t\tconst mask = options?.mask ?? -1;\n\t\tif (mask < -1 || mask > 7) throw new Error(`invalid mask ${mask}`);\n\t\treturn new QrCode(version, ecLevel, codewords, mask);\n\t}","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/utils/qrcode.ts#L178-L214","documentation":"encodeBytes walks QR versions up to maxVersion looking for one whose data capacity fits the payload; if even the maximum version with the chosen error-correction level cannot hold the bytes, it throws a descriptive length error.","triggerScenarios":"Calling the QR encoder (encodeBytes) with data longer than the maximum QR version's byte capacity at the given EC level — e.g. more than ~1,273 bytes at EC H, or ~2,953 bytes at EC L in byte mode.","commonSituations":"Encoding long URLs with huge query strings, vCards with many fields, Wi-Fi payloads with long keys, or embedding a file/blob into a QR code.","solutions":["Shorten the payload (URL shortener, trim fields, drop optional data).","Lower the error-correction level (H to Q to M to L) to gain capacity.","Split the data across multiple QR codes or use a different transport.","If the library exposes it, increase maxVersion — otherwise the data physically cannot fit one QR code."],"exampleFix":"// before: encodeBytes(longVCard, { ecLevel: \"H\" }); // 2KB payload  // after: shorten the payload (or use ecLevel \"L\"), then encodeBytes(shortened, { ecLevel: \"L\" });","handlingStrategy":"validation","validationCode":"const MAX_BYTES = { L: 2953, M: 2331, Q: 1663, H: 1273 } as const; if (payload.length > MAX_BYTES[ecLevel]) throw new Error(`payload too long for QR at EC ${ecLevel}`);","typeGuard":null,"tryCatchPattern":"try { const qr = encodeBytes(data, { ecLevel }); } catch (err) { if (err instanceof Error && err.message.startsWith(\"data too long\")) { return encodeBytes(shorten(data), { ecLevel: \"L\" }); } throw err; }","preventionTips":["Cap payloads well under ~1.2KB; prefer linking to content over embedding it.","Use URL shorteners for long links destined for QR codes.","Choose the lowest EC level your use case tolerates for maximum capacity.","Split very large data across multiple codes rather than forcing one code."],"tags":["qrcode","capacity-limit","validation"],"backgroundTag":"qr-payload-too-large","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}