From cf90d8b5e328e4402e3a686c1554ef70446079cb Mon Sep 17 00:00:00 2001 From: Brendan Maginnis Date: Tue, 22 Oct 2024 11:28:08 +0100 Subject: [PATCH 1/2] feat: return mermaid parse error in 400 response --- src/node_modules/renderImgOrSvg.js | 2 +- src/static/mermaid.mjs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/node_modules/renderImgOrSvg.js b/src/node_modules/renderImgOrSvg.js index eb93980..246eedc 100644 --- a/src/node_modules/renderImgOrSvg.js +++ b/src/node_modules/renderImgOrSvg.js @@ -95,7 +95,7 @@ module.exports = (render) => async (ctx, encodedCode, _next) => { debug('rendered SVG in DOM'); } catch (e) { debug('mermaid failed to render SVG: %o', e); - ctx.throw(400, 'invalid encoded code'); + ctx.throw(400, e); } await render(ctx, page, size); } catch (e) { diff --git a/src/static/mermaid.mjs b/src/static/mermaid.mjs index 22db0ce..41b2372 100644 --- a/src/static/mermaid.mjs +++ b/src/static/mermaid.mjs @@ -1,7 +1,7 @@ import mermaid from '../../node_modules/mermaid/dist/mermaid.esm.min.mjs'; -function isSyntaxErrorFromMermaid(code) { - return code.includes('Syntax error in graph') && code.includes('error-icon'); +function isUnknownDiagramError(code) { + return code.includes('UnknownDiagramError'); } function setBgColor(svgElement, bgColor) { @@ -54,8 +54,8 @@ async function render(definition, config, bgColor, size) { } } catch (error) { console.error('Failed to render', error); - if (isSyntaxErrorFromMermaid(error.toString())) { - throw new Error('Syntax error in graph'); + if (isUnknownDiagramError(error.toString())) { + throw new Error('Unknown diagram error'); } throw error; } From fae56a608b4cd204509ac80285842ae98a0ed444 Mon Sep 17 00:00:00 2001 From: Jihchi Lee Date: Mon, 4 Nov 2024 08:45:03 +0200 Subject: [PATCH 2/2] Don't include call stack in the response --- src/static/mermaid.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/static/mermaid.mjs b/src/static/mermaid.mjs index 41b2372..2d3add2 100644 --- a/src/static/mermaid.mjs +++ b/src/static/mermaid.mjs @@ -57,7 +57,7 @@ async function render(definition, config, bgColor, size) { if (isUnknownDiagramError(error.toString())) { throw new Error('Unknown diagram error'); } - throw error; + throw error.message; } }