Unverified Commit f9aec900 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Turbopack: Trace edge runtime app render errors through source maps (#62901)

Similar to #62611, this implements error stack translation for edge
runtime app render errors.

Test Plan: `TURBOPACK=1 pnpm test-dev
test/development/app-render-error-log/app-render-error-log.test.ts`

Closes PACK-2665
parent 0d3481b6
...@@ -4,6 +4,10 @@ import { SpanStatusCode, getTracer } from '../lib/trace/tracer' ...@@ -4,6 +4,10 @@ import { SpanStatusCode, getTracer } from '../lib/trace/tracer'
import { isAbortError } from '../pipe-readable' import { isAbortError } from '../pipe-readable'
import { isDynamicUsageError } from '../../export/helpers/is-dynamic-usage-error' import { isDynamicUsageError } from '../../export/helpers/is-dynamic-usage-error'
declare global {
var __next_log_error__: undefined | ((err: unknown) => void)
}
export type ErrorHandler = ( export type ErrorHandler = (
err: unknown, err: unknown,
errorInfo: unknown errorInfo: unknown
...@@ -98,12 +102,10 @@ export function createErrorHandler({ ...@@ -98,12 +102,10 @@ export function createErrorHandler({
errorLogger(err).catch(() => {}) errorLogger(err).catch(() => {})
} else { } else {
// The error logger is currently not provided in the edge runtime. // The error logger is currently not provided in the edge runtime.
// Use `log-app-dir-error` instead. // Use the exposed `__next_log_error__` instead.
// It won't log the source code, but the error will be more useful. // This will trace error traces to the original source code.
if (process.env.NODE_ENV !== 'production') { if (typeof __next_log_error__ === 'function') {
const { logAppDirError } = __next_log_error__(err)
require('../dev/log-app-dir-error') as typeof import('../dev/log-app-dir-error')
logAppDirError(err)
} else { } else {
console.error(err) console.error(err)
} }
......
...@@ -432,6 +432,7 @@ export default class DevServer extends Server { ...@@ -432,6 +432,7 @@ export default class DevServer extends Server {
try { try {
return super.runEdgeFunction({ return super.runEdgeFunction({
...params, ...params,
onError: (err) => this.logErrorWithOriginalStack(err, 'app-dir'),
onWarning: (warn) => { onWarning: (warn) => {
this.logErrorWithOriginalStack(warn, 'warning') this.logErrorWithOriginalStack(warn, 'warning')
}, },
......
...@@ -1815,6 +1815,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1815,6 +1815,7 @@ export default class NextNodeServer extends BaseServer {
page: string page: string
appPaths: string[] | null appPaths: string[] | null
match?: RouteMatch match?: RouteMatch
onError?: (err: unknown) => void
onWarning?: (warning: Error) => void onWarning?: (warning: Error) => void
}): Promise<FetchEventResult | null> { }): Promise<FetchEventResult | null> {
if (process.env.NEXT_MINIMAL) { if (process.env.NEXT_MINIMAL) {
...@@ -1890,6 +1891,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1890,6 +1891,7 @@ export default class NextNodeServer extends BaseServer {
), ),
}, },
useCache: true, useCache: true,
onError: params.onError,
onWarning: params.onWarning, onWarning: params.onWarning,
incrementalCache: incrementalCache:
(globalThis as any).__incrementalCache || (globalThis as any).__incrementalCache ||
......
...@@ -264,6 +264,12 @@ async function createModuleContext(options: ModuleContextOptions) { ...@@ -264,6 +264,12 @@ async function createModuleContext(options: ModuleContextOptions) {
}, },
}) })
if (process.env.NODE_ENV !== 'production') {
context.__next_log_error__ = function (err: unknown) {
options.onError(err)
}
}
context.__next_eval__ = function __next_eval__(fn: Function) { context.__next_eval__ = function __next_eval__(fn: Function) {
const key = fn.toString() const key = fn.toString()
if (!warnedEvals.has(key)) { if (!warnedEvals.has(key)) {
...@@ -458,6 +464,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), ...@@ -458,6 +464,7 @@ Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`),
interface ModuleContextOptions { interface ModuleContextOptions {
moduleName: string moduleName: string
onError: (err: unknown) => void
onWarning: (warn: Error) => void onWarning: (warn: Error) => void
useCache: boolean useCache: boolean
distDir: string distDir: string
......
...@@ -15,6 +15,7 @@ const FORBIDDEN_HEADERS = [ ...@@ -15,6 +15,7 @@ const FORBIDDEN_HEADERS = [
type RunnerFn = (params: { type RunnerFn = (params: {
name: string name: string
onError?: (err: unknown) => void
onWarning?: (warn: Error) => void onWarning?: (warn: Error) => void
paths: string[] paths: string[]
request: NodejsRequestData request: NodejsRequestData
...@@ -54,6 +55,7 @@ function withTaggedErrors(fn: RunnerFn): RunnerFn { ...@@ -54,6 +55,7 @@ function withTaggedErrors(fn: RunnerFn): RunnerFn {
export async function getRuntimeContext(params: { export async function getRuntimeContext(params: {
name: string name: string
onWarning?: any onWarning?: any
onError?: (err: unknown) => void
useCache: boolean useCache: boolean
edgeFunctionEntry: any edgeFunctionEntry: any
distDir: string distDir: string
...@@ -63,6 +65,7 @@ export async function getRuntimeContext(params: { ...@@ -63,6 +65,7 @@ export async function getRuntimeContext(params: {
const { runtime, evaluateInContext } = await getModuleContext({ const { runtime, evaluateInContext } = await getModuleContext({
moduleName: params.name, moduleName: params.name,
onWarning: params.onWarning ?? (() => {}), onWarning: params.onWarning ?? (() => {}),
onError: params.onError ?? (() => {}),
useCache: params.useCache !== false, useCache: params.useCache !== false,
edgeFunctionEntry: params.edgeFunctionEntry, edgeFunctionEntry: params.edgeFunctionEntry,
distDir: params.distDir, distDir: params.distDir,
......
...@@ -33,7 +33,7 @@ createNextDescribe( ...@@ -33,7 +33,7 @@ createNextDescribe(
await check(() => cliOutput, /digest:/) await check(() => cliOutput, /digest:/)
expect(cliOutput).toInclude('Error: boom') expect(cliOutput).toInclude('Error: boom')
expect(cliOutput).toInclude('at fn2 (./app/fn.ts') expect(cliOutput).toInclude('at fn2 (./app/fn.ts')
expect(cliOutput).toInclude('at fn1 (./app/fn.ts') expect(cliOutput).toMatch(/at (Module\.)?fn1 \(\.\/app\/fn\.ts/)
expect(cliOutput).toInclude('at EdgePage (./app/edge/page.tsx') expect(cliOutput).toInclude('at EdgePage (./app/edge/page.tsx')
expect(cliOutput).not.toInclude('webpack-internal') expect(cliOutput).not.toInclude('webpack-internal')
......
...@@ -1514,11 +1514,10 @@ ...@@ -1514,11 +1514,10 @@
}, },
"test/development/app-render-error-log/app-render-error-log.test.ts": { "test/development/app-render-error-log/app-render-error-log.test.ts": {
"passed": [ "passed": [
"app-render-error-log should log the correct values on app-render error" "app-render-error-log should log the correct values on app-render error",
],
"failed": [
"app-render-error-log should log the correct values on app-render error with edge runtime" "app-render-error-log should log the correct values on app-render error with edge runtime"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment