mermaid-js/mermaid · error · Error

Packet block ${start} - ${end} is invalid. End must be great

Error message

Packet block ${start} - ${end} is invalid. End must be greater than start.

What it means

Thrown by the packet diagram populate loop when a block explicitly declares both start and end and end is less than start. Packet blocks represent bit ranges and must be ordered; an inverted range is structurally invalid. Only blocks where both bounds are user-supplied are checked here (auto-computed bounds are handled later).

Source

Thrown at packages/mermaid/src/diagrams/packet/parser.ts:20

import { parse } from '@mermaid-js/parser';
import type { ParserDefinition } from '../../diagram-api/types.js';
import { log } from '../../logger.js';
import { populateCommonDb } from '../common/populateCommonDb.js';
import { PacketDB } from './db.js';
import type { PacketBlock, PacketWord } from './types.js';

const maxPacketSize = 10_000;

const populate = (ast: Packet, db: PacketDB) => {
  populateCommonDb(ast, db);
  let lastBit = -1;
  let word: PacketWord = [];
  let row = 1;
  const { bitsPerRow } = db.getConfig();

  for (let { start, end, bits, label } of ast.blocks) {
    if (start !== undefined && end !== undefined && end < start) {
      throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);
    }
    start ??= lastBit + 1;
    if (start !== lastBit + 1) {
      throw new Error(
        `Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${
          lastBit + 1
        }.`
      );
    }
    if (bits === 0) {
      throw new Error(`Packet block ${start} is invalid. Cannot have a zero bit field.`);
    }
    end ??= start + (bits ?? 1) - 1;
    bits ??= end - start + 1;
    lastBit = end;
    log.debug(`Packet block ${start} - ${lastBit} with label ${label}`);

    while (word.length <= bitsPerRow + 1 && db.getPacket().length < maxPacketSize) {

View on GitHub (pinned to d93e9c88c0)

Solutions

  1. Swap the start and end values so start < end for every block.
  2. Omit end and let mermaid compute it from start + bits.
  3. Use the bits-only shorthand ('N: label') to avoid specifying ranges manually.
  4. Re-read the packet diagram syntax docs for the correct field order.

Example fix

// before
packet-beta
  15-10: Inverted label

// after
packet-beta
  10-15: Inverted label
Defensive patterns

Strategy: validation

Validate before calling

// Validate packet block ranges before render.
function validatePacketBlocks(blocks: {start?:number;end?:number}[]): string[] {
  const errors: string[] = [];
  for (const b of blocks) {
    if (b.start !== undefined && b.end !== undefined && b.end < b.start) {
      errors.push(`Block ${b.start}-${b.end}: end must be >= start`);
    }
  }
  return errors;
}

Type guard

function isValidBlockRange(start: number | undefined, end: number | undefined): boolean {
  if (start === undefined || end === undefined) return true;
  return end >= start;
}

Try / catch

try {
  await mermaid.render('g', diagramText);
} catch (e) {
  if (e instanceof Error && /is invalid\. End must be greater than start/.test(e.message)) {
    showUserError('A packet block has its range reversed. Ensure start <= end.');
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Writing a packet block with start-end fields reversed, e.g. bits field syntax '10-15: label' where the first number is larger; or YAML/JSON-style block with start: 15, end: 10.

Common situations: Authoring packet diagrams by hand and swapping the range endpoints; copy-paste errors; misunderstanding that the syntax expects low-to-high ordering.

Related errors


AI-assisted analysis of mermaid-js/mermaid@d93e9c88c0 (2026-08-12). Data as JSON: /api/errors/e7b84c4c281edb1f. Report an issue: GitHub.