txtdot/src/errors/handler.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-08-16 13:20:24 +03:00
import { FastifyReply, FastifyRequest } from "fastify";
2023-08-21 16:03:28 +03:00
import { EngineParseError, IFastifyValidationError, InvalidParameterError, LocalResourceError, NotHtmlMimetypeError } from "./main";
2023-08-16 13:20:24 +03:00
export default function errorHandler(
error: Error,
_: FastifyRequest,
reply: FastifyReply
) {
if (error instanceof NotHtmlMimetypeError) {
return reply.redirect(301, error.url);
2023-08-21 16:03:28 +03:00
} else if (error instanceof EngineParseError) {
return reply.code(500).view("/templates/error.ejs", {
title: "parse error",
description: "Engine can not parse the HTML page received from the remote server.",
});
} else if (
error instanceof InvalidParameterError ||
(
error as unknown as IFastifyValidationError
)?.statusCode === 400
) {
return reply.code(400).view("/templates/error.ejs", {
title: "invalid parameter",
description: `Specified parameters are invalid.`,
});
} else if (error instanceof LocalResourceError) {
return reply.code(403).view("/templates/error.ejs", {
title: "local resource",
description: "Proxying local resources is forbidden.",
});
2023-08-16 13:20:24 +03:00
} else {
2023-08-21 16:03:28 +03:00
console.log(error);
2023-08-16 13:20:24 +03:00
return error;
}
}