mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
refactor(error_handlers): use newly added ForbiddenError class
This commit is contained in:
parent
2c91f6e7bc
commit
0c8df7f885
12
src/errors/forbidden_error.ts
Normal file
12
src/errors/forbidden_error.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import HttpError from "./http_error.js";
|
||||||
|
|
||||||
|
class ForbiddenError extends HttpError {
|
||||||
|
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message, 403);
|
||||||
|
this.name = "ForbiddenError";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ForbiddenError;
|
@ -1,6 +1,7 @@
|
|||||||
import type { Application, NextFunction, Request, Response } from "express";
|
import type { Application, NextFunction, Request, Response } from "express";
|
||||||
import log from "../services/log.js";
|
import log from "../services/log.js";
|
||||||
import NotFoundError from "../errors/not_found_error.js";
|
import NotFoundError from "../errors/not_found_error.js";
|
||||||
|
import ForbiddenError from "../errors/forbidden_error.js";
|
||||||
|
|
||||||
function register(app: Application) {
|
function register(app: Application) {
|
||||||
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
|
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
|
||||||
@ -9,10 +10,7 @@ function register(app: Application) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.error(`Invalid CSRF token: ${req.headers["x-csrf-token"]}, secret: ${req.cookies["_csrf"]}`);
|
log.error(`Invalid CSRF token: ${req.headers["x-csrf-token"]}, secret: ${req.cookies["_csrf"]}`);
|
||||||
|
next(new ForbiddenError("Invalid CSRF token"));
|
||||||
err = new Error("Invalid CSRF token");
|
|
||||||
err.status = 403;
|
|
||||||
next(err);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user