Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log uncaught source-mapped errors to the console #446

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/tre/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"glob-to-regexp": "^0.4.1",
"http-cache-semantics": "^4.1.0",
"kleur": "^4.1.5",
"source-map": "^0.7.4",
"source-map-support": "0.5.21",
"stoppable": "^1.1.0",
"undici": "^5.12.0",
"workerd": "^1.20221111.5",
Expand All @@ -49,6 +49,7 @@
"@types/estree": "^1.0.0",
"@types/glob-to-regexp": "^0.4.1",
"@types/http-cache-semantics": "^4.0.1",
"@types/source-map-support": "^0.5.6",
"@types/stoppable": "^1.1.1",
"@types/ws": "^8.5.3"
},
Expand Down
6 changes: 5 additions & 1 deletion packages/tre/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ export class Miniflare {
const workerSrcOpts = this.#workerOpts.map<SourceOptions>(
({ core }) => core
);
response = await handlePrettyErrorRequest(workerSrcOpts, request);
response = await handlePrettyErrorRequest(
this.#log,
workerSrcOpts,
request
);
} else {
// TODO: check for proxying/outbound fetch header first (with plans for fetch mocking)
response = await this.#handleLoopbackPlugins(request, url);
Expand Down
157 changes: 157 additions & 0 deletions packages/tre/src/plugins/core/errors/callsite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Lifted from `node-stack-trace`:
// https://github.com/felixge/node-stack-trace/blob/4c41a4526e74470179b3b6dd5d75191ca8c56c17/index.js
// Ideally, we'd just use this package as-is, but it has a strict
// `engines.node == 16` constraint in its `package.json`. There's a PR open to
// fix this (https://github.com/felixge/node-stack-trace/pull/39), but it's been
// open for a while. As soon as it's merged, we should just depend on it.

/*!
* Copyright (c) 2011 Felix Geisendörfer (felix@debuggable.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

export function parseStack(stack: string): CallSite[] {
return stack
.split("\n")
.slice(1)
.map(parseCallSite)
.filter((site): site is CallSite => site !== undefined);
}

function parseCallSite(line: string): CallSite | undefined {
const lineMatch = line.match(
/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/
);
if (!lineMatch) {
return;
}

let object = null;
let method = null;
let functionName = null;
let typeName = null;
let methodName = null;
const isNative = lineMatch[5] === "native";

if (lineMatch[1]) {
functionName = lineMatch[1];
let methodStart = functionName.lastIndexOf(".");
if (functionName[methodStart - 1] == ".") methodStart--;
if (methodStart > 0) {
object = functionName.substring(0, methodStart);
method = functionName.substring(methodStart + 1);
const objectEnd = object.indexOf(".Module");
if (objectEnd > 0) {
functionName = functionName.substring(objectEnd + 1);
object = object.substring(0, objectEnd);
}
}
}

if (method) {
typeName = object;
methodName = method;
}

if (method === "<anonymous>") {
methodName = null;
functionName = null;
}

return new CallSite({
typeName,
functionName,
methodName,
fileName: lineMatch[2] || null,
lineNumber: parseInt(lineMatch[3]) || null,
columnNumber: parseInt(lineMatch[4]) || null,
native: isNative,
});
}

export interface CallSiteOptions {
typeName: string | null;
functionName: string | null;
methodName: string | null;
fileName: string | null;
lineNumber: number | null;
columnNumber: number | null;
native: boolean;
}

// https://v8.dev/docs/stack-trace-api#customizing-stack-traces
// This class supports the subset of options implemented by `node-stack-trace`:
// https://github.com/felixge/node-stack-trace/blob/4c41a4526e74470179b3b6dd5d75191ca8c56c17/index.js
export class CallSite implements NodeJS.CallSite {
constructor(private readonly opts: CallSiteOptions) {}

getThis(): unknown {
return null;
}
getTypeName(): string | null {
return this.opts.typeName;
}
// eslint-disable-next-line @typescript-eslint/ban-types
getFunction(): Function | undefined {
return undefined;
}
getFunctionName(): string | null {
return this.opts.functionName;
}
getMethodName(): string | null {
return this.opts.methodName;
}
getFileName(): string | null {
return this.opts.fileName;
}
getLineNumber(): number | null {
return this.opts.lineNumber;
}
getColumnNumber(): number | null {
return this.opts.columnNumber;
}
getEvalOrigin(): string | undefined {
return undefined;
}
isToplevel(): boolean {
return false;
}
isEval(): boolean {
return false;
}
isNative(): boolean {
return this.opts.native;
}
isConstructor(): boolean {
return false;
}
isAsync(): boolean {
return false;
}
isPromiseAll(): boolean {
return false;
}
isPromiseAny(): boolean {
return false;
}
getPromiseIndex(): number | null {
return null;
}
}
Loading