fix(nuxt): use h3 utilities to set response status/code (#19713)

This commit is contained in:
Daniel Roe 2023-03-19 23:20:04 +00:00 committed by GitHub
parent bf0cabb000
commit 3055ce35b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 21 deletions

View File

@ -1,5 +1,6 @@
import type { H3Event } from 'h3' import type { H3Event } from 'h3'
import { setResponseStatus as _setResponseStatus } from 'h3'
import type { NuxtApp } from '../nuxt' import type { NuxtApp } from '../nuxt'
import { useNuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt'
@ -25,11 +26,6 @@ export function useRequestFetch (): typeof global.$fetch {
} }
export function setResponseStatus (code: number, message?: string) { export function setResponseStatus (code: number, message?: string) {
const event = process.server && useRequestEvent() if (process.client) { return }
if (event) { _setResponseStatus(useRequestEvent(), code, message)
event.node.res.statusCode = code
if (message) {
event.node.res.statusMessage = message
}
}
} }

View File

@ -1,7 +1,7 @@
import { joinURL, withQuery } from 'ufo' import { joinURL, withQuery } from 'ufo'
import type { NitroErrorHandler } from 'nitropack' import type { NitroErrorHandler } from 'nitropack'
import type { H3Error } from 'h3' import type { H3Error } from 'h3'
import { setResponseHeader, getRequestHeaders } from 'h3' import { setResponseHeader, getRequestHeaders, setResponseStatus } from 'h3'
import { useNitroApp, useRuntimeConfig } from '#internal/nitro' import { useNitroApp, useRuntimeConfig } from '#internal/nitro'
import { normalizeError, isJsonRequest } from '#internal/nitro/utils' import { normalizeError, isJsonRequest } from '#internal/nitro/utils'
@ -22,10 +22,8 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
} }
// Set response code and message // Set response code and message
event.node.res.statusCode = (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500 setResponseStatus(event, (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500, errorObject.statusMessage)
if (errorObject.statusMessage) {
event.node.res.statusMessage = errorObject.statusMessage
}
// Console output // Console output
if (error.unhandled || error.fatal) { if (error.unhandled || error.fatal) {
const tags = [ const tags = [
@ -40,7 +38,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// JSON response // JSON response
if (isJsonRequest(event)) { if (isJsonRequest(event)) {
event.node.res.setHeader('Content-Type', 'application/json') setResponseHeader(event, 'Content-Type', 'application/json')
event.node.res.end(JSON.stringify(errorObject)) event.node.res.end(JSON.stringify(errorObject))
return return
} }
@ -65,7 +63,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// TODO: Support `message` in template // TODO: Support `message` in template
(errorObject as any).description = errorObject.message (errorObject as any).description = errorObject.message
} }
event.node.res.setHeader('Content-Type', 'text/html;charset=UTF-8') setResponseHeader(event, 'Content-Type', 'text/html;charset=UTF-8')
event.node.res.end(template(errorObject)) event.node.res.end(template(errorObject))
return return
} }
@ -74,13 +72,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
setResponseHeader(event, header, value) setResponseHeader(event, header, value)
} }
if (res.status && res.status !== 200) { setResponseStatus(event, res.status && res.status !== 200 ? res.status : undefined, res.statusText)
event.node.res.statusCode = res.status
}
if (res.statusText) {
event.node.res.statusMessage = res.statusText
}
event.node.res.end(await res.text()) event.node.res.end(await res.text())
} }

View File

@ -490,6 +490,7 @@ describe('errors', () => {
} }
}) })
expect(res.status).toBe(422) expect(res.status).toBe(422)
expect(res.statusText).toBe('This is a custom error')
const error = await res.json() const error = await res.json()
delete error.stack delete error.stack
expect(error).toMatchObject({ expect(error).toMatchObject({