Unverified Commit 469c5030 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Display stack trace when error occurs in API route (#38289)


## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent f716f704
...@@ -831,17 +831,23 @@ export default class DevServer extends Server { ...@@ -831,17 +831,23 @@ export default class DevServer extends Server {
const { originalCodeFrame, originalStackFrame } = originalFrame const { originalCodeFrame, originalStackFrame } = originalFrame
const { file, lineNumber, column, methodName } = originalStackFrame const { file, lineNumber, column, methodName } = originalStackFrame
console.error( Log[type === 'warning' ? 'warn' : 'error'](
(type === 'warning' ? chalk.yellow('warn') : chalk.red('error')) + `${file} (${lineNumber}:${column}) @ ${methodName}`
' - ' +
`${file} (${lineNumber}:${column}) @ ${methodName}`
) )
console.error( if (src === 'edge-server') {
`${(type === 'warning' ? chalk.yellow : chalk.red)(err.name)}: ${ console[type === 'warning' ? 'warn' : 'error'](
err.message `${(type === 'warning' ? chalk.yellow : chalk.red)(
}` err.name
) )}: ${err.message}`
console.error(originalCodeFrame) )
} else if (type === 'warning') {
Log.warn(err)
} else if (type) {
Log.error(`${type}:`, err)
} else {
Log.error(err)
}
console[type === 'warning' ? 'warn' : 'error'](originalCodeFrame)
usedOriginalStack = true usedOriginalStack = true
} }
} }
......
...@@ -115,13 +115,16 @@ async function createModuleContext(options: ModuleContextOptions) { ...@@ -115,13 +115,16 @@ async function createModuleContext(options: ModuleContextOptions) {
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)) {
const warning = new Error( const warning = getServerError(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware` new Error(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Middleware`
),
'edge-server'
) )
warning.name = 'DynamicCodeEvaluationWarning' warning.name = 'DynamicCodeEvaluationWarning'
Error.captureStackTrace(warning, __next_eval__) Error.captureStackTrace(warning, __next_eval__)
warnedEvals.add(key) warnedEvals.add(key)
options.onWarning(getServerError(warning, 'edge-server')) options.onWarning(warning)
} }
return fn() return fn()
} }
......
export default function error(req, res) {
throw new Error()
}
export default function uncaughtException(req, res) {
setTimeout(() => {
throw new Error('uncaught exception')
}, 0)
res.send('hello')
}
export default function unhandledRejection(req, res) {
Promise.reject(new Error('unhandled rejection'))
res.send('hello')
}
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { check, renderViaHTTP } from 'next-test-utils'
import { join } from 'path'
describe('api-route-errors cli output', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
},
dependencies: {},
})
})
afterAll(() => next.destroy())
test('error', async () => {
const outputIndex = next.cliOutput.length
await renderViaHTTP(next.url, '/api/error')
await check(() => next.cliOutput.slice(outputIndex), /pages\/api/)
const output = stripAnsi(next.cliOutput.slice(outputIndex))
// Location
expect(output).toContain('error - (api)/pages/api/error.js (2:8) @ error')
// Stack
expect(output).toContain('pages/api/error.js:6:11')
// Source code
expect(output).toContain('1 | export default function error(req, res) {')
})
test('uncaught exception', async () => {
const outputIndex = next.cliOutput.length
await renderViaHTTP(next.url, '/api/uncaught-exception')
await check(() => next.cliOutput.slice(outputIndex), /pages\/api/)
const output = stripAnsi(next.cliOutput.slice(outputIndex))
// Location
expect(output).toContain(
'error - (api)/pages/api/uncaught-exception.js (3:10) @ Timeout'
)
// Stack
expect(output).toContain('pages/api/uncaught-exception.js:7:15')
// Source code
expect(output).toContain(
'1 | export default function uncaughtException(req, res) {'
)
})
test('unhandled rejection', async () => {
const outputIndex = next.cliOutput.length
await renderViaHTTP(next.url, '/api/unhandled-rejection')
await check(() => next.cliOutput.slice(outputIndex), /pages\/api/)
const output = stripAnsi(next.cliOutput.slice(outputIndex))
// Location
expect(output).toContain(
'error - (api)/pages/api/unhandled-rejection.js (2:17) @ unhandledRejection'
)
// Stack
expect(output).toContain('pages/api/unhandled-rejection.js:6:20')
// Source code
expect(output).toContain(
'1 | export default function unhandledRejection(req, res) '
)
})
})
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