From 419d9d55df807e9ea2ce6afacedd925ebc061118 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 19 Oct 2026 19:16:49 +0000 Subject: [PATCH] - WIP on negundo's JS client --- asset/negundo/js/debug.js | 63 +++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/asset/negundo/js/debug.js b/asset/negundo/js/debug.js index cda9144..ea91edf 100644 --- a/asset/negundo/js/debug.js +++ b/asset/negundo/js/debug.js @@ -1,4 +1,7 @@ class ErrorHandler { + + #unhandledRection = false + constructor(options) { if (!options) throw new Error("Options not provided"); if (!options.url) throw new Error("URL is required"); @@ -6,7 +9,8 @@ class ErrorHandler { this.url = options.url; this.apikey = options.apikey; - + this.catchError(); + } getErrorPosition(script) { let totalLines = 0; @@ -36,34 +40,43 @@ class ErrorHandler { }; window.addEventListener('unhandledrejection', event => { - this.reportError(event.reason.toString(), document.location.href, 0, 0, event.reason.stack, 'Promise Rejection'); + if (! this.#unhandledRection) { + this.#unhandledRection = true; + this.reportError(event.reason.toString(), document.location.href, 0, 0, event.reason.stack, 'Promise Rejection'); + } }); - - - - this.createErrorAgain(); } async reportError(message, url, line, column, stack, type = 'JavaScript Error') { await new Promise(r => setTimeout(r, 2000)); - fetch(this.url ? `${this.url}/${this.apikey}` : window.location.href, { - method: "POST", - headers: { - 'Accept': "application/json", - 'Content-Type': "application/json" - }, - body: JSON.stringify({ - type, - message, - url, - line, - code: column, - backtrace: stack ? stack.split("\n").filter((e) => !!e).map((e) => this.parseErrorLine(e)) : [], - file: window.location.href, - source: await this.getSourceCode(url, line), - }) - }).then(response => response.json()).then(console.info); + try { + const response = await fetch(this.url ? `${this.url}/${this.apikey}` : window.location.href, { + method: "PUT", + headers: { + 'Accept': "application/json", + 'Content-Type': "application/json" + }, + body: JSON.stringify({ + type, + message, + url, + line, + code: column, + backtrace: stack ? stack.split("\n").filter((e) => !!e).map((e) => this.parseErrorLine(e)) : [], + file: window.location.href, + source: await this.getSourceCode(url, line), + }) + }); + + if (response.ok) { + this.#unhandledRection = false; + + return await response.json() + } + } catch (error) { + console.error('CORS or network error:', error.message); + } } async getSourceCode(url, errorLineNumber) { @@ -94,8 +107,6 @@ class ErrorHandler { obj.file = match[2]; obj.line = match[3]; // column would be match[4] if needed - - console.log(match); } // Remove undefined/empty values @@ -105,4 +116,4 @@ class ErrorHandler { return obj; } -} \ No newline at end of file +}