Commit 09cab2df authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Fix typo in serverless loader (#9431)

* Fix typo in serverless loader

* Add test for top level error in API route

* Fix type error
parent fefe453e
...@@ -68,10 +68,10 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -68,10 +68,10 @@ const nextServerlessLoader: loader.Loader = function() {
: `{}` : `{}`
} }
const resolver = require('${absolutePagePath}') const resolver = require('${absolutePagePath}')
apiResolver(req, res, params, resolver) apiResolver(req, res, params, resolver, onError)
} catch (error) { } catch (err) {
await onError(err)
console.error(err) console.error(err)
await onError(err)
res.statusCode = 500 res.statusCode = 500
res.end('Internal Server Error') res.end('Internal Server Error')
} }
......
...@@ -14,7 +14,8 @@ export async function apiResolver( ...@@ -14,7 +14,8 @@ export async function apiResolver(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse, res: NextApiResponse,
params: any, params: any,
resolverModule: any resolverModule: any,
onError?: ({ err }: { err: any }) => Promise<void>
) { ) {
try { try {
let config: PageConfig = {} let config: PageConfig = {}
...@@ -51,11 +52,12 @@ export async function apiResolver( ...@@ -51,11 +52,12 @@ export async function apiResolver(
const resolver = interopDefault(resolverModule) const resolver = interopDefault(resolverModule)
resolver(req, res) resolver(req, res)
} catch (e) { } catch (err) {
if (e instanceof ApiError) { if (err instanceof ApiError) {
sendError(res, e.statusCode, e.message) sendError(res, err.statusCode, err.message)
} else { } else {
console.error(e) console.error(err)
if (onError) await onError({ err })
sendError(res, 500, 'Internal Server Error') sendError(res, 500, 'Internal Server Error')
} }
} }
......
...@@ -100,7 +100,7 @@ export default class Server { ...@@ -100,7 +100,7 @@ export default class Server {
dev?: boolean dev?: boolean
} }
private compression?: Middleware private compression?: Middleware
private onErrorMiddleware?: ({ err }: { err: Error }) => void private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
router: Router router: Router
protected dynamicRoutes?: Array<{ page: string; match: RouteMatch }> protected dynamicRoutes?: Array<{ page: string; match: RouteMatch }>
protected customRoutes?: { protected customRoutes?: {
...@@ -498,7 +498,8 @@ export default class Server { ...@@ -498,7 +498,8 @@ export default class Server {
req, req,
res, res,
params, params,
resolverFunction ? require(resolverFunction) : undefined resolverFunction ? require(resolverFunction) : undefined,
this.onErrorMiddleware
) )
} }
......
throw new Error('top-level-oops')
// eslint-disable-next-line
export default (req, res) => {
res.end('hi')
}
...@@ -6,6 +6,7 @@ import cheerio from 'cheerio' ...@@ -6,6 +6,7 @@ import cheerio from 'cheerio'
import { existsSync, readdirSync, readFileSync } from 'fs' import { existsSync, readdirSync, readFileSync } from 'fs'
import { import {
killApp, killApp,
waitFor,
findPort, findPort,
nextBuild, nextBuild,
nextStart, nextStart,
...@@ -20,6 +21,7 @@ const appDir = join(__dirname, '../') ...@@ -20,6 +21,7 @@ const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages') const serverlessDir = join(appDir, '.next/serverless/pages')
const chunksDir = join(appDir, '.next/static/chunks') const chunksDir = join(appDir, '.next/static/chunks')
const buildIdFile = join(appDir, '.next/BUILD_ID') const buildIdFile = join(appDir, '.next/BUILD_ID')
let stderr = ''
let appPort let appPort
let app let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
...@@ -28,7 +30,11 @@ describe('Serverless', () => { ...@@ -28,7 +30,11 @@ describe('Serverless', () => {
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort, {
onStderr: msg => {
stderr += msg || ''
},
})
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -251,6 +257,12 @@ describe('Serverless', () => { ...@@ -251,6 +257,12 @@ describe('Serverless', () => {
expect(data.query).toEqual({ slug: paramRaw }) expect(data.query).toEqual({ slug: paramRaw })
}) })
it('should log error in API route correctly', async () => {
await renderViaHTTP(appPort, '/api/top-level-error')
await waitFor(1000)
expect(stderr).toContain('top-level-oops')
})
describe('With basic usage', () => { describe('With basic usage', () => {
it('should allow etag header support', async () => { it('should allow etag header support', async () => {
const url = `http://localhost:${appPort}/` const url = `http://localhost:${appPort}/`
......
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