11import type { H3Event , HTTPError , HTTPEvent } from "h3" ;
2- import { getRequestURL } from "h3" ;
3- import { defineNitroErrorHandler } from "./utils" ;
42import type { InternalHandlerResponse } from "./utils" ;
53import { FastResponse } from "srvx" ;
4+ import type { NitroErrorHandler } from "nitro/types" ;
65
7- export default defineNitroErrorHandler (
8- function defaultNitroErrorHandler ( error , event ) {
9- const res = defaultHandler ( error , event ) ;
10- return new FastResponse ( JSON . stringify ( res . body , null , 2 ) , res ) ;
11- }
12- ) ;
6+ const errorHandler : NitroErrorHandler = ( error , event ) => {
7+ const res = defaultHandler ( error , event ) ;
8+ return new FastResponse (
9+ typeof res . body === "string" ? res . body : JSON . stringify ( res . body , null , 2 ) ,
10+ res
11+ ) ;
12+ } ;
13+
14+ export default errorHandler ;
1315
1416export function defaultHandler (
1517 error : HTTPError ,
@@ -18,8 +20,7 @@ export function defaultHandler(
1820) : InternalHandlerResponse {
1921 const isSensitive = error . unhandled ;
2022 const status = error . status || 500 ;
21- // prettier-ignore
22- const url = getRequestURL ( event , { xForwardedHost : true , xForwardedProto : true } )
23+ const url = ( event as H3Event ) . url || new URL ( event . req . url ) ;
2324
2425 if ( status === 404 ) {
2526 const baseURL = import . meta. baseURL || "/" ;
@@ -47,13 +48,9 @@ export function defaultHandler(
4748 // Send response
4849 const headers : HeadersInit = {
4950 "content-type" : "application/json" ,
50- // Prevent browser from guessing the MIME types of resources.
5151 "x-content-type-options" : "nosniff" ,
52- // Prevent error page from being embedded in an iframe
5352 "x-frame-options" : "DENY" ,
54- // Prevent browsers from sending the Referer header
5553 "referrer-policy" : "no-referrer" ,
56- // Disable the execution of any js
5754 "content-security-policy" : "script-src 'none'; frame-ancestors 'none';" ,
5855 } ;
5956
0 commit comments