All files / qr-code/src errors.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                  14x         14x 14x 14x 14x 14x                   11x 11x 11x      
import type { ErrorCorrectionLevel } from "./types";
 
/**
 * Thrown when the requested data cannot fit in a QR symbol at the requested
 * error-correction level (SPEC §3.1). Never a silent truncation — a truncated
 * QR code decodes to the wrong value, which is worse than an error.
 *
 * @example
 * ```ts
 * try {
 *   encodeQR(hugeString, { level: "H" });
 * } catch (error) {
 *   if (error instanceof QRCapacityError) {
 *     console.log(`needs ${error.needed} bits, max is ${error.capacity}`);
 *   }
 * }
 * ```
 */
export class QRCapacityError extends Error {
  /** Bits the data requires at the version that was attempted. */
  readonly needed: number;
  /** Bits available at that version and error-correction level. */
  readonly capacity: number;
  readonly level: ErrorCorrectionLevel;
  /** Highest version considered (40, or an explicit `version` option). */
  readonly maxVersion: number;
 
  constructor(details: {
    needed: number;
    capacity: number;
    level: ErrorCorrectionLevel;
    maxVersion: number;
  }) {
    super(
      `Data does not fit in a QR code: needs ${details.needed} bits but version ` +
        `${details.maxVersion} at level ${details.level} holds ${details.capacity}. ` +
        `Shorten the data or lower the error-correction level.`,
    );
    this.name = "QRCapacityError";
    this.needed = details.needed;
    this.capacity = details.capacity;
    this.level = details.level;
    this.maxVersion = details.maxVersion;
  }
}
 
/**
 * Thrown when a raster export cannot complete — most often because a
 * cross-origin logo tainted the canvas (SPEC §3.4). The message names the
 * remedy rather than leaving a blank image behind.
 */
export class QRExportError extends Error {
  constructor(message: string, readonly cause?: unknown) {
    super(message);
    this.name = "QRExportError";
  }
}