Fix issue where 404 errors are mistaken for server errors. (#4534)
* Fix issue where 404 errors are mistaken for server errors. * Bump vendor.
This commit is contained in:
parent
db35c47315
commit
bd09533f40
@ -8,14 +8,36 @@ import { rootPath } from "../constants"
|
|||||||
import { replaceTemplates } from "../http"
|
import { replaceTemplates } from "../http"
|
||||||
import { escapeHtml, getMediaMime } from "../util"
|
import { escapeHtml, getMediaMime } from "../util"
|
||||||
|
|
||||||
const notFoundCodes = ["ENOENT", "EISDIR"]
|
interface ErrorWithStatusCode {
|
||||||
export const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
|
statusCode: number
|
||||||
if (notFoundCodes.includes(err.code)) {
|
|
||||||
err.status = HttpCode.NotFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const status = err.status ?? err.statusCode ?? 500
|
interface ErrorWithCode {
|
||||||
res.status(status)
|
code: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Error is network related. */
|
||||||
|
export const errorHasStatusCode = (error: any): error is ErrorWithStatusCode => {
|
||||||
|
return error && "statusCode" in error
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Error originates from file system. */
|
||||||
|
export const errorHasCode = (error: any): error is ErrorWithCode => {
|
||||||
|
return error && "code" in error
|
||||||
|
}
|
||||||
|
|
||||||
|
const notFoundCodes = [404, "ENOENT", "EISDIR"]
|
||||||
|
|
||||||
|
export const errorHandler: express.ErrorRequestHandler = async (err, req, res, next) => {
|
||||||
|
let statusCode = 500
|
||||||
|
|
||||||
|
if (errorHasStatusCode(err)) {
|
||||||
|
statusCode = err.statusCode
|
||||||
|
} else if (errorHasCode(err) && notFoundCodes.includes(err.code)) {
|
||||||
|
statusCode = HttpCode.NotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(statusCode)
|
||||||
|
|
||||||
// Assume anything that explicitly accepts text/html is a user browsing a
|
// Assume anything that explicitly accepts text/html is a user browsing a
|
||||||
// page (as opposed to an xhr request). Don't use `req.accepts()` since
|
// page (as opposed to an xhr request). Don't use `req.accepts()` since
|
||||||
@ -27,8 +49,8 @@ export const errorHandler: express.ErrorRequestHandler = async (err, req, res, n
|
|||||||
const content = await fs.readFile(resourcePath, "utf8")
|
const content = await fs.readFile(resourcePath, "utf8")
|
||||||
res.send(
|
res.send(
|
||||||
replaceTemplates(req, content)
|
replaceTemplates(req, content)
|
||||||
.replace(/{{ERROR_TITLE}}/g, status)
|
.replace(/{{ERROR_TITLE}}/g, statusCode.toString())
|
||||||
.replace(/{{ERROR_HEADER}}/g, status)
|
.replace(/{{ERROR_HEADER}}/g, statusCode.toString())
|
||||||
.replace(/{{ERROR_BODY}}/g, escapeHtml(err.message)),
|
.replace(/{{ERROR_BODY}}/g, escapeHtml(err.message)),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
2
vendor/package.json
vendored
2
vendor/package.json
vendored
@ -7,6 +7,6 @@
|
|||||||
"postinstall": "./postinstall.sh"
|
"postinstall": "./postinstall.sh"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"code-oss-dev": "cdr/vscode#8db6c9bb0bc065bdb905dc076f4d4234f126aff7"
|
"code-oss-dev": "cdr/vscode#a1d3f915454bb88e508c269a3c5bafb3cfa6f9f6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
vendor/yarn.lock
vendored
4
vendor/yarn.lock
vendored
@ -296,9 +296,9 @@ clone-response@^1.0.2:
|
|||||||
dependencies:
|
dependencies:
|
||||||
mimic-response "^1.0.0"
|
mimic-response "^1.0.0"
|
||||||
|
|
||||||
code-oss-dev@cdr/vscode#8db6c9bb0bc065bdb905dc076f4d4234f126aff7:
|
code-oss-dev@cdr/vscode#a1d3f915454bb88e508c269a3c5bafb3cfa6f9f6:
|
||||||
version "1.61.1"
|
version "1.61.1"
|
||||||
resolved "https://codeload.github.com/cdr/vscode/tar.gz/8db6c9bb0bc065bdb905dc076f4d4234f126aff7"
|
resolved "https://codeload.github.com/cdr/vscode/tar.gz/a1d3f915454bb88e508c269a3c5bafb3cfa6f9f6"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@microsoft/applicationinsights-web" "^2.6.4"
|
"@microsoft/applicationinsights-web" "^2.6.4"
|
||||||
"@vscode/sqlite3" "4.0.12"
|
"@vscode/sqlite3" "4.0.12"
|
||||||
|
Reference in New Issue
Block a user