Unverified Commit 6652d783 authored by Meno Abels's avatar Meno Abels Committed by GitHub

proper error if middleware or api/route not return a Response (#41336)

## Bug

The error which is thrown if the fetch method returns not a falsy or
Response value
is misleading.
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
parent 7b7fae5a
...@@ -106,6 +106,11 @@ export async function adapter(params: { ...@@ -106,6 +106,11 @@ export async function adapter(params: {
const event = new NextFetchEvent({ request, page: params.page }) const event = new NextFetchEvent({ request, page: params.page })
let response = await params.handler(request, event) let response = await params.handler(request, event)
// check if response is a Response object
if (response && !(response instanceof Response)) {
throw new TypeError('Expected an instance of Response to be returned')
}
/** /**
* For rewrites we must always include the locale in the final pathname * For rewrites we must always include the locale in the final pathname
* so we re-create the NextURL forcing it to include it when the it is * so we re-create the NextURL forcing it to include it when the it is
......
// populated with tests
export default () => {
return 'Boom'
}
export const config = { matcher: '/' }
export default async function handler(request) {
return 'Boom'
}
export const config = { runtime: 'experimental-edge' }
export default function Page() {
return <div>ok</div>
}
/* eslint-disable jest/no-identical-title */
/* eslint-env jest */
import { remove } from 'fs-extra'
import { join } from 'path'
import {
fetchViaHTTP,
File,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const context = {
appDir: join(__dirname, '../'),
logs: { output: '', stdout: '', stderr: '' },
api: new File(join(__dirname, '../pages/api/route.js')),
lib: new File(join(__dirname, '../lib.js')),
middleware: new File(join(__dirname, '../middleware.js')),
page: new File(join(__dirname, '../pages/index.js')),
}
const appOption = {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
onStdout(msg) {
context.logs.output += msg
context.logs.stdout += msg
},
onStderr(msg) {
context.logs.output += msg
context.logs.stderr += msg
},
}
const routeUrl = '/api/route'
const middlewareUrl = '/'
describe('Edge runtime code with imports', () => {
beforeEach(async () => {
context.appPort = await findPort()
context.logs = { output: '', stdout: '', stderr: '' }
await remove(join(__dirname, '../.next'))
})
afterEach(() => {
if (context.app) {
killApp(context.app)
}
context.api.restore()
context.middleware.restore()
context.lib.restore()
context.page.restore()
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
},
{
title: 'Middleware',
url: middlewareUrl,
},
])('test error if response is not Response type', ({ title, url }) => {
it(`${title} dev test Response`, async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(context.logs.stderr).toContain(
'Expected an instance of Response to be returned'
)
expect(res.status).toBe(500)
})
it(`${title} build test Response`, async () => {
await nextBuild(context.appDir, undefined, {
stderr: true,
})
context.app = await nextStart(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(context.logs.stderr).toContain(
'Expected an instance of Response to be returned'
)
expect(res.status).toBe(500)
})
})
})
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