feat: update error handler to use Youch for HTML formatting

This commit is contained in:
Daniel Philip Johnson 2025-01-15 11:09:55 +00:00
parent 27a703af81
commit 5d74ae3c6b

View File

@ -1,7 +1,8 @@
import { joinURL, withQuery } from 'ufo'
import type { NitroErrorHandler } from 'nitro/types'
import type { H3Error, H3Event } from 'h3'
import { getRequestHeader, getRequestHeaders, send, setResponseHeader, setResponseStatus } from 'h3'
import { getRequestHeader, getRequestHeaders, getRequestURL, send, setResponseHeader, setResponseStatus } from 'h3'
import { Youch } from "youch";
import { useNitroApp, useRuntimeConfig } from 'nitro/runtime'
import type { NuxtPayload } from 'nuxt/app'
@ -9,6 +10,11 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// Parse and normalize error
const { stack, statusCode, statusMessage, message } = normalizeError(error)
const url = getRequestURL(event, { xForwardedHost: true, xForwardedProto: true }).toString()
// Create a Youch instance for HTML output
const youch = new Youch()
// Create an error object
const errorObject = {
url: event.path,
@ -74,7 +80,14 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
return send(event, template(errorObject))
}
const html = await res.text()
const html = await youch.toHTML(error, {
request: {
url,
method: event.method,
headers: getRequestHeaders(event),
},
})
if (event.handled) { return }
for (const [header, value] of res.headers.entries()) {
@ -82,7 +95,7 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
}
setResponseStatus(event, res.status && res.status !== 200 ? res.status : undefined, res.statusText)
return send(event, html)
return send(event, html,"text/html")
}
/**