2019-06-20 11:59:55 +02:00
|
|
|
import ErrorStackParser from "error-stack-parser";
|
2020-07-24 06:02:07 +02:00
|
|
|
import $ from "jquery";
|
2021-03-19 00:56:46 +01:00
|
|
|
import type StackFrame from "stackframe";
|
2019-06-20 11:59:55 +02:00
|
|
|
import StackTraceGPS from "stacktrace-gps";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2019-06-20 11:59:55 +02:00
|
|
|
import render_blueslip_stacktrace from "../templates/blueslip_stacktrace.hbs";
|
|
|
|
|
2023-04-28 23:05:20 +02:00
|
|
|
export class BlueslipError extends Error {
|
|
|
|
override name = "BlueslipError";
|
|
|
|
more_info?: object;
|
|
|
|
constructor(msg: string, more_info?: object | undefined, cause?: unknown | undefined) {
|
|
|
|
super(msg, {cause});
|
|
|
|
if (more_info !== undefined) {
|
|
|
|
this.more_info = more_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:59:55 +02:00
|
|
|
type FunctionName = {
|
|
|
|
scope: string;
|
|
|
|
name: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type NumberedLine = {
|
|
|
|
line_number: number;
|
|
|
|
line: string;
|
|
|
|
focus: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type CleanStackFrame = {
|
2020-07-26 22:19:12 +02:00
|
|
|
full_path?: string;
|
|
|
|
show_path?: string;
|
|
|
|
function_name?: FunctionName;
|
|
|
|
line_number?: number;
|
|
|
|
context?: NumberedLine[];
|
2019-06-20 11:59:55 +02:00
|
|
|
};
|
|
|
|
|
2023-05-02 20:18:57 +02:00
|
|
|
export function exception_msg(
|
|
|
|
ex: Error & {
|
|
|
|
// Unsupported properties available on some browsers
|
|
|
|
fileName?: string;
|
|
|
|
lineNumber?: number;
|
|
|
|
},
|
|
|
|
): string {
|
|
|
|
let message = ex.message;
|
|
|
|
if (ex.fileName !== undefined) {
|
|
|
|
message += " at " + ex.fileName;
|
|
|
|
if (ex.lineNumber !== undefined) {
|
|
|
|
message += `:${ex.lineNumber}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2020-07-26 22:19:12 +02:00
|
|
|
export function clean_path(full_path?: string): string | undefined {
|
2019-06-20 11:59:55 +02:00
|
|
|
// If the file is local, just show the filename.
|
|
|
|
// Otherwise, show the full path starting from node_modules.
|
2020-07-26 22:19:12 +02:00
|
|
|
if (full_path === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-06-20 11:59:55 +02:00
|
|
|
const idx = full_path.indexOf("/node_modules/");
|
|
|
|
if (idx !== -1) {
|
|
|
|
return full_path.slice(idx + "/node_modules/".length);
|
|
|
|
}
|
|
|
|
if (full_path.startsWith("webpack://")) {
|
|
|
|
return full_path.slice("webpack://".length);
|
|
|
|
}
|
|
|
|
return full_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clean_function_name(
|
2020-07-02 02:16:03 +02:00
|
|
|
function_name: string | undefined,
|
2020-07-15 00:34:28 +02:00
|
|
|
): {scope: string; name: string} | undefined {
|
2019-06-20 11:59:55 +02:00
|
|
|
if (function_name === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const idx = function_name.lastIndexOf(".");
|
|
|
|
return {
|
|
|
|
scope: function_name.slice(0, idx + 1),
|
|
|
|
name: function_name.slice(idx + 1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-19 23:46:25 +02:00
|
|
|
const sourceCache: Record<string, string | Promise<string>> = {};
|
2019-06-20 11:59:55 +02:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
const stack_trace_gps = new StackTraceGPS({sourceCache});
|
2019-06-20 11:59:55 +02:00
|
|
|
|
2020-02-05 07:11:39 +01:00
|
|
|
async function get_context(location: StackFrame): Promise<NumberedLine[] | undefined> {
|
2020-07-26 22:19:12 +02:00
|
|
|
const {fileName, lineNumber} = location;
|
|
|
|
if (fileName === undefined || lineNumber === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2023-05-02 20:21:30 +02:00
|
|
|
let sourceContent: string;
|
2020-07-27 23:49:30 +02:00
|
|
|
try {
|
|
|
|
sourceContent = await sourceCache[fileName];
|
|
|
|
} catch {
|
|
|
|
return undefined;
|
|
|
|
}
|
2019-06-20 11:59:55 +02:00
|
|
|
if (sourceContent === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const lines = sourceContent.split("\n");
|
2020-07-26 22:19:12 +02:00
|
|
|
const lo_line_num = Math.max(lineNumber - 5, 0);
|
|
|
|
const hi_line_num = Math.min(lineNumber + 4, lines.length);
|
2019-06-20 11:59:55 +02:00
|
|
|
return lines.slice(lo_line_num, hi_line_num).map((line: string, i: number) => ({
|
|
|
|
line_number: lo_line_num + i + 1,
|
|
|
|
line,
|
2020-07-26 22:19:12 +02:00
|
|
|
focus: lo_line_num + i + 1 === lineNumber,
|
2019-06-20 11:59:55 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-05-02 20:18:57 +02:00
|
|
|
export async function display_stacktrace(ex: Error): Promise<void> {
|
2023-04-21 22:03:34 +02:00
|
|
|
const errors = [];
|
|
|
|
while (true) {
|
|
|
|
const stackframes: CleanStackFrame[] = await Promise.all(
|
|
|
|
ErrorStackParser.parse(ex).map(async (location: StackFrame) => {
|
|
|
|
try {
|
|
|
|
location = await stack_trace_gps.getMappedLocation(location);
|
|
|
|
} catch {
|
|
|
|
// Use unmapped location
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
full_path: location.getFileName(),
|
|
|
|
show_path: clean_path(location.getFileName()),
|
|
|
|
line_number: location.getLineNumber(),
|
|
|
|
function_name: clean_function_name(location.getFunctionName()),
|
|
|
|
context: await get_context(location),
|
|
|
|
};
|
2023-05-02 20:18:57 +02:00
|
|
|
}),
|
|
|
|
);
|
2023-04-21 22:03:34 +02:00
|
|
|
errors.push({
|
|
|
|
message: exception_msg(ex),
|
|
|
|
stackframes,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ex.cause !== undefined && ex.cause instanceof Error) {
|
|
|
|
ex = ex.cause;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const $alert = $("<div>").addClass("stacktrace").html(render_blueslip_stacktrace({errors}));
|
2019-06-20 11:59:55 +02:00
|
|
|
$(".alert-box").append($alert);
|
|
|
|
$alert.addClass("show");
|
|
|
|
}
|