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";
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function display_stacktrace(error: string, stack: string): Promise<void> {
|
2020-12-22 11:23:33 +01:00
|
|
|
const ex = new Error("dummy");
|
2019-06-20 11:59:55 +02:00
|
|
|
ex.stack = stack;
|
|
|
|
|
|
|
|
const stackframes: CleanStackFrame[] = await Promise.all(
|
2022-03-02 06:45:32 +01:00
|
|
|
ErrorStackParser.parse(ex).map(async (location: StackFrame) => {
|
2020-07-27 23:49:30 +02:00
|
|
|
try {
|
|
|
|
location = await stack_trace_gps.getMappedLocation(location);
|
|
|
|
} catch {
|
|
|
|
// Use unmapped location
|
|
|
|
}
|
2019-06-20 11:59:55 +02:00
|
|
|
return {
|
|
|
|
full_path: location.getFileName(),
|
|
|
|
show_path: clean_path(location.getFileName()),
|
|
|
|
line_number: location.getLineNumber(),
|
|
|
|
function_name: clean_function_name(location.getFunctionName()),
|
2020-02-04 00:23:38 +01:00
|
|
|
context: await get_context(location),
|
2019-06-20 11:59:55 +02:00
|
|
|
};
|
2020-07-02 02:16:03 +02:00
|
|
|
}),
|
2019-06-20 11:59:55 +02:00
|
|
|
);
|
|
|
|
|
2022-08-16 06:50:20 +02:00
|
|
|
const $alert = $("<div>")
|
|
|
|
.addClass("stacktrace")
|
|
|
|
.html(render_blueslip_stacktrace({error, stackframes}));
|
2019-06-20 11:59:55 +02:00
|
|
|
$(".alert-box").append($alert);
|
|
|
|
$alert.addClass("show");
|
|
|
|
}
|