mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.52k stars 238 forks source link

No error raised with some chart types #778

Open skye0402 opened 3 weeks ago

skye0402 commented 3 weeks ago

Describe the bug If a mermaid code contains an error the CLI should throw an error. However, for some cases like below there is no error thrown. Instead it creates an SVG with a bomb and the error remains unknown to the calling program giving it no possibility to correct the error.

To Reproduce

Use this diagram code:

architecture-beta
    group user_group[User Interaction]
    service user[User 📱] in user_group
    service app[Mobile App 📲] in user_group

    group ai_group[AI Processing]
    service aihub[SAP BTP Generative AI Hub 🤖] in ai_group

    group s4hana_group[Backend]
    service s4hana[S/4HANA Product Master API 🛠️] in s4hana_group

    user:L -- R:app
    app:L -- R:aihub
    aihub:L -- R:app
    app:L -- R:s4hana

Expected behavior The command line tool should raise an error when called with mmdc -i error.mmd -o yest.svg -e svg -t dark -b transparent

Screenshots image

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

aloisklink commented 2 weeks ago

I've replicated this issue.

It looks like Puppeteer is failing to throw the error somewhere, since the error from architecture diagrams is not JSON-ifiable, since it has circular dependencies.

The following seems to work-around it:

diff --git a/src/index.js b/src/index.js
index 3b14e8a..5d268b0 100644
--- a/src/index.js
+++ b/src/index.js
@@ -264,7 +264,12 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
       mermaid.registerLayoutLoaders(elkLayouts)
       mermaid.initialize({ startOnLoad: false, ...mermaidConfig })
       // should throw an error if mmd diagram is invalid
-      const { svg: svgText } = await mermaid.render(svgId || 'my-svg', definition, container)
+      let svgText;
+      try {
+        ({ svg: svgText } = await mermaid.render(svgId || 'my-svg', definition, container))
+      } catch (error) {
+        throw new Error(error.message, {cause: error})
+      }
       container.innerHTML = svgText

       const svg = container.getElementsByTagName?.('svg')?.[0]

I might have a dig around the Mermaid source-code first, though, since I want to figure out why this Error has circular dependencies, since maybe we can fix the issue in the Mermaid project instead.