- Fixed client bug in js source and also on negundo server side
This commit is contained in:
parent
063aba6040
commit
27bfdd5902
@ -9,36 +9,6 @@ class ErrorHandler {
|
||||
this.catchError();
|
||||
}
|
||||
|
||||
getSourceCode(lineNumber) {
|
||||
return this.getInlineSourceCode(lineNumber) || this.getFullSourceWithHighlight(lineNumber);
|
||||
}
|
||||
|
||||
getInlineSourceCode(lineNumber) {
|
||||
const scripts = document.querySelectorAll('script');
|
||||
for (let script of scripts) {
|
||||
if (!script.src) {
|
||||
const scriptLines = script.textContent.split("\n");
|
||||
const scriptPosition = this.getErrorPosition(script);
|
||||
if (lineNumber >= scriptPosition.startLine && lineNumber <= scriptPosition.endLine) {
|
||||
return this.highlightSource(scriptLines, lineNumber - scriptPosition.startLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
getFullSourceWithHighlight(lineNumber) {
|
||||
const lines = document.documentElement.outerHTML.split("\n");
|
||||
return this.highlightSource(lines, lineNumber - 1);
|
||||
}
|
||||
|
||||
highlightSource(lines, lineNumber) {
|
||||
const start = Math.max(lineNumber - 2, 0);
|
||||
const end = Math.min(lineNumber + 2, lines.length - 1);
|
||||
lines[lineNumber] = '>> ' + lines[lineNumber] + ' <<';
|
||||
return lines.slice(start, end + 1).join("\n");
|
||||
}
|
||||
|
||||
getErrorPosition(script) {
|
||||
let totalLines = 0;
|
||||
let element = script.previousElementSibling;
|
||||
@ -54,18 +24,32 @@ class ErrorHandler {
|
||||
|
||||
catchError() {
|
||||
window.onerror = (message, url, lineNumber, column, error) => {
|
||||
this.reportError(message, url, lineNumber, column, error.stack, 'JavaScript Error');
|
||||
let type = 'JavaScript Error';
|
||||
|
||||
if (message.indexOf(':')) {
|
||||
type = message.substring(0, message.indexOf(':'));
|
||||
message = message.substring(message.indexOf(':') + 1).trim();
|
||||
}
|
||||
|
||||
this.reportError(message, url, lineNumber, column, error.stack, type);
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
window.addEventListener('unhandledrejection', event => {
|
||||
this.reportError(event.reason.toString(), document.location.href, 0, 0, event.reason.stack, 'Promise Rejection');
|
||||
});
|
||||
|
||||
|
||||
|
||||
this.createErrorAgain();
|
||||
}
|
||||
|
||||
reportError(message, url, lineNumber, column, stack, type = 'JavaScript Error') {
|
||||
fetch(this.url ? `${location.protocol}//${this.url}/${this.apikey}` : window.location.href, {
|
||||
method: "post",
|
||||
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"
|
||||
@ -74,12 +58,52 @@ class ErrorHandler {
|
||||
type,
|
||||
message,
|
||||
url,
|
||||
lineNumber,
|
||||
column,
|
||||
stack,
|
||||
location: window.location.href,
|
||||
source: this.getSourceCode(lineNumber)
|
||||
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);
|
||||
}
|
||||
|
||||
async getSourceCode(url, errorLineNumber) {
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const originalHtml = await response.text();
|
||||
const lines = originalHtml.split('\n');
|
||||
|
||||
const start = Math.max(0, errorLineNumber - 5);
|
||||
const end = Math.min(lines.length, errorLineNumber + 5);
|
||||
|
||||
return lines.slice(start, end).join('\n');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to fetch original HTML:', error);
|
||||
}
|
||||
}
|
||||
|
||||
parseErrorLine(errorLine) {
|
||||
const obj = {};
|
||||
|
||||
// Pattern for: functionName@url:line:column
|
||||
const pattern = /^(.*)@(.+?):(\d+):(\d+)$/;
|
||||
const match = errorLine.match(pattern);
|
||||
|
||||
if (match) {
|
||||
obj.function = match[1];
|
||||
obj.file = match[2];
|
||||
obj.line = match[3];
|
||||
// column would be match[4] if needed
|
||||
|
||||
console.log(match);
|
||||
}
|
||||
|
||||
// Remove undefined/empty values
|
||||
Object.keys(obj).forEach(key => {
|
||||
if (!obj[key]) delete obj[key];
|
||||
});
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
use function DI\autowire, DI\create, DI\get;
|
||||
|
||||
use Negundo\Client\{ SoftwareConfig, Dump, Task, NegundoMiddleware };
|
||||
|
||||
use function DI\{ autowire, create, get, add };
|
||||
|
||||
return [
|
||||
SoftwareConfig::class => create(SoftwareConfig::class)->constructor(
|
||||
serverUrl: getenvonce('NEGUNDO_SERVER'),
|
||||
@ -13,6 +13,11 @@ return [
|
||||
Dump::class => autowire(Dump::class),
|
||||
Task::class => autowire(Task::class),
|
||||
|
||||
'lean.autoload' => add([
|
||||
Dump::class,
|
||||
Task::class,
|
||||
]),
|
||||
|
||||
'negundo.client' => [
|
||||
'picea' => [
|
||||
'asset' => [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user