From 822fc2bdc2663a79c8090a6634a1ba2dc0274af6 Mon Sep 17 00:00:00 2001 From: Birk Skyum Date: Tue, 21 Jan 2025 22:37:17 +0100 Subject: [PATCH] add error handling --- src/libs/codemirror-mgl.ts | 42 +++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/libs/codemirror-mgl.ts b/src/libs/codemirror-mgl.ts index 168eb556..24d5a0a4 100644 --- a/src/libs/codemirror-mgl.ts +++ b/src/libs/codemirror-mgl.ts @@ -16,12 +16,27 @@ CodeMirror.defineMode("mgl", (config, parserConfig) => { CodeMirror.registerHelper("lint", "json", (text: string) => { const found: MarkerRangeWithMessage[] = []; - try { parse(text); } - catch(_e) { - // Do nothing we catch the error above + catch(err: any) { + + const errorMatch = err.toString().match(/line (\d+), column (\d+)/); + if (errorMatch) { + const loc = { + first_line: parseInt(errorMatch[1], 10), + first_column: parseInt(errorMatch[2], 10), + last_line: parseInt(errorMatch[1], 10), + last_column: parseInt(errorMatch[2], 10) + }; + + // const loc = hash.loc; + found.push({ + from: CodeMirror.Pos(loc.first_line - 1, loc.first_column), + to: CodeMirror.Pos(loc.last_line - 1, loc.last_column), + message: err + }); + } } return found; }); @@ -33,8 +48,25 @@ CodeMirror.registerHelper("lint", "mgl", (text: string, opts: any, doc: any) => try { parse(text); } - catch (_e) { - // ignore errors + catch (err: any) { + + const errorMatch = err.toString().match(/line (\d+), column (\d+)/); + if (errorMatch) { + const loc = { + first_line: parseInt(errorMatch[1], 10), + first_column: parseInt(errorMatch[2], 10), + last_line: parseInt(errorMatch[1], 10), + last_column: parseInt(errorMatch[2], 10) + }; + + // const loc = hash.loc; + found.push({ + from: CodeMirror.Pos(loc.first_line - 1, loc.first_column), + to: CodeMirror.Pos(loc.last_line - 1, loc.last_column), + message: err + }); + } + } if (found.length > 0) {