@@ -121,23 +121,29 @@ export const register = async (
121
121
throw new HttpError ( "Not Found" , HttpCode . NotFound )
122
122
} )
123
123
124
- const errorHandler : express . ErrorRequestHandler = async ( err , req , res , next ) => {
125
- const resourcePath = path . resolve ( rootPath , "src/browser/pages/error.html" )
126
- res . set ( "Content-Type" , getMediaMime ( resourcePath ) )
127
- try {
124
+ const errorHandler : express . ErrorRequestHandler = async ( err , req , res ) => {
125
+ if ( err . code === "ENOENT" || err . code === "EISDIR" ) {
126
+ err . status = HttpCode . NotFound
127
+ }
128
+
129
+ const status = err . status ?? err . statusCode ?? 500
130
+ res . status ( status )
131
+
132
+ if ( req . accepts ( "application/json" ) ) {
133
+ res . json ( {
134
+ error : err . message ,
135
+ ...( err . details || { } ) ,
136
+ } )
137
+ } else {
138
+ const resourcePath = path . resolve ( rootPath , "src/browser/pages/error.html" )
139
+ res . set ( "Content-Type" , getMediaMime ( resourcePath ) )
128
140
const content = await fs . readFile ( resourcePath , "utf8" )
129
- if ( err . code === "ENOENT" || err . code === "EISDIR" ) {
130
- err . status = HttpCode . NotFound
131
- }
132
- const status = err . status ?? err . statusCode ?? 500
133
- res . status ( status ) . send (
141
+ res . send (
134
142
replaceTemplates ( req , content )
135
143
. replace ( / { { ERROR_ T I T L E } } / g, status )
136
144
. replace ( / { { ERROR_ H E A D E R } } / g, status )
137
145
. replace ( / { { ERROR_ B O D Y } } / g, err . message ) ,
138
146
)
139
- } catch ( error ) {
140
- next ( error )
141
147
}
142
148
}
143
149
0 commit comments