@@ -125,20 +125,19 @@ export const register = async (
125
125
throw new HttpError ( "Not Found" , HttpCode . NotFound )
126
126
} )
127
127
128
- const errorHandler : express . ErrorRequestHandler = async ( err , req , res ) => {
128
+ const errorHandler : express . ErrorRequestHandler = async ( err , req , res , next ) => {
129
129
if ( err . code === "ENOENT" || err . code === "EISDIR" ) {
130
130
err . status = HttpCode . NotFound
131
131
}
132
132
133
133
const status = err . status ?? err . statusCode ?? 500
134
134
res . status ( status )
135
135
136
- if ( req . accepts ( "application/json" ) ) {
137
- res . json ( {
138
- error : err . message ,
139
- ...( err . details || { } ) ,
140
- } )
141
- } else {
136
+ // Assume anything that explicitly accepts text/html is a user browsing a
137
+ // page (as opposed to an xhr request). Don't use `req.accepts()` since
138
+ // *every* request that I've seen (in Firefox and Chromium at least)
139
+ // includes `*/*` making it always truthy. Even for css/javascript.
140
+ if ( req . headers . accept && req . headers . accept . includes ( "text/html" ) ) {
142
141
const resourcePath = path . resolve ( rootPath , "src/browser/pages/error.html" )
143
142
res . set ( "Content-Type" , getMediaMime ( resourcePath ) )
144
143
const content = await fs . readFile ( resourcePath , "utf8" )
@@ -148,6 +147,11 @@ export const register = async (
148
147
. replace ( / { { ERROR_ H E A D E R } } / g, status )
149
148
. replace ( / { { ERROR_ B O D Y } } / g, err . message ) ,
150
149
)
150
+ } else {
151
+ res . json ( {
152
+ error : err . message ,
153
+ ...( err . details || { } ) ,
154
+ } )
151
155
}
152
156
}
153
157
0 commit comments