Skip to main content

Overview

Mermaid provides comprehensive error handling mechanisms to help you identify and resolve issues with diagram syntax, rendering, and configuration. This guide covers error detection, handling strategies, and debugging techniques.

Error types

UnknownDiagramError

Thrown when Mermaid cannot detect the diagram type (errors.ts:1):
This error occurs when the diagram text doesn’t match any registered diagram detector:

Parse errors

Parse errors occur when diagram syntax is invalid. These are caught during the parsing phase.

Render errors

Render errors happen during diagram rendering and may be caused by configuration issues or browser limitations.

The parse function

Use mermaid.parse() to validate diagram syntax without rendering (mermaidAPI.ts:67):

Basic validation

Suppressing errors

Use suppressErrors: true to return false instead of throwing:

Examples from source

From mermaid.ts:336:

Error handling in render

The render function provides built-in error handling (mermaidAPI.ts:392):

Suppress error rendering

Control whether error diagrams are displayed:

Custom error handlers

Setting a parse error handler

Set a global error handler for parse errors (mermaid.ts:304):

DetailedError interface

Mermaid provides detailed error information:

Error handling in run()

The run() function handles errors from multiple diagrams (mermaid.ts:64):

Using suppressErrors in run()

When rendering multiple diagrams with mermaid.run(), suppress errors to continue processing:
From mermaid.ts:109:

Debugging techniques

Enable debug logging

Log levels: 'fatal', 'error', 'warn', 'info', 'debug', 'trace'

Check diagram type detection

Verify which diagram type Mermaid detects:

Validate before rendering

1

Parse the diagram

2

Check validation result

3

Render the diagram

Common errors and solutions

Text size exceeded

Error: Maximum text size in diagram exceeded Cause: Diagram text exceeds maxTextSize limit (default: 50,000 characters) Solution:
Large diagrams may cause performance issues. Consider breaking them into smaller diagrams.

Unknown diagram type

Error: UnknownDiagramError: No diagram type detected Cause: The diagram syntax doesn’t match any registered diagram type Solutions:
  1. Check the diagram keyword (e.g., flowchart, sequenceDiagram)
  2. Ensure the diagram type is registered
  3. Remove comments and directives that may interfere with detection

Parse errors with directives

Error: Parse fails with %%init%% directive Cause: Directive syntax is incorrect Solution:

Error recovery patterns

Graceful degradation

Retry with fallback

User-friendly error messages

Testing and validation

Automated validation

Logging and monitoring

Custom logger

Error tracking integration