{"record":{"id":"e7b84c4c281edb1f","repo":"mermaid-js/mermaid","slug":"packet-block-start-end-is-invalid-end-mus","errorCode":null,"errorMessage":"Packet block ${start} - ${end} is invalid. End must be greater than start.","messagePattern":"Packet block (.+?) - (.+?) is invalid\\. End must be greater than start\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mermaid/src/diagrams/packet/parser.ts","lineNumber":20,"sourceCode":"import { parse } from '@mermaid-js/parser';\nimport type { ParserDefinition } from '../../diagram-api/types.js';\nimport { log } from '../../logger.js';\nimport { populateCommonDb } from '../common/populateCommonDb.js';\nimport { PacketDB } from './db.js';\nimport type { PacketBlock, PacketWord } from './types.js';\n\nconst maxPacketSize = 10_000;\n\nconst populate = (ast: Packet, db: PacketDB) => {\n  populateCommonDb(ast, db);\n  let lastBit = -1;\n  let word: PacketWord = [];\n  let row = 1;\n  const { bitsPerRow } = db.getConfig();\n\n  for (let { start, end, bits, label } of ast.blocks) {\n    if (start !== undefined && end !== undefined && end < start) {\n      throw new Error(`Packet block ${start} - ${end} is invalid. End must be greater than start.`);\n    }\n    start ??= lastBit + 1;\n    if (start !== lastBit + 1) {\n      throw new Error(\n        `Packet block ${start} - ${end ?? start} is not contiguous. It should start from ${\n          lastBit + 1\n        }.`\n      );\n    }\n    if (bits === 0) {\n      throw new Error(`Packet block ${start} is invalid. Cannot have a zero bit field.`);\n    }\n    end ??= start + (bits ?? 1) - 1;\n    bits ??= end - start + 1;\n    lastBit = end;\n    log.debug(`Packet block ${start} - ${lastBit} with label ${label}`);\n\n    while (word.length <= bitsPerRow + 1 && db.getPacket().length < maxPacketSize) {","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/mermaid-js/mermaid/blob/d93e9c88c01a599c062ee6a3f1462e3558ac6b90/packages/mermaid/src/diagrams/packet/parser.ts#L2-L38","documentation":"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).","triggerScenarios":"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.","commonSituations":"Authoring packet diagrams by hand and swapping the range endpoints; copy-paste errors; misunderstanding that the syntax expects low-to-high ordering.","solutions":["Swap the start and end values so start < end for every block.","Omit end and let mermaid compute it from start + bits.","Use the bits-only shorthand ('N: label') to avoid specifying ranges manually.","Re-read the packet diagram syntax docs for the correct field order."],"exampleFix":"// before\npacket-beta\n  15-10: Inverted label\n\n// after\npacket-beta\n  10-15: Inverted label","handlingStrategy":"validation","validationCode":"// Validate packet block ranges before render.\nfunction validatePacketBlocks(blocks: {start?:number;end?:number}[]): string[] {\n  const errors: string[] = [];\n  for (const b of blocks) {\n    if (b.start !== undefined && b.end !== undefined && b.end < b.start) {\n      errors.push(`Block ${b.start}-${b.end}: end must be >= start`);\n    }\n  }\n  return errors;\n}","typeGuard":"function isValidBlockRange(start: number | undefined, end: number | undefined): boolean {\n  if (start === undefined || end === undefined) return true;\n  return end >= start;\n}","tryCatchPattern":"try {\n  await mermaid.render('g', diagramText);\n} catch (e) {\n  if (e instanceof Error && /is invalid\\. End must be greater than start/.test(e.message)) {\n    showUserError('A packet block has its range reversed. Ensure start <= end.');\n  } else {\n    throw e;\n  }\n}","preventionTips":["Always write block ranges low-to-high.","Omit end to let mermaid compute it from start + bits.","Use the bits-only shorthand to avoid manual ranges."],"tags":["mermaid","packet","parsing","validation"],"backgroundTag":null,"analyzedSha":"d93e9c88c01a599c062ee6a3f1462e3558ac6b90","analyzedAt":"2026-08-12T06:23:11.304Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}