diff --git a/src/errors/handler.ts b/src/errors/handler.ts index 58365ae..fd2681b 100644 --- a/src/errors/handler.ts +++ b/src/errors/handler.ts @@ -1,5 +1,5 @@ import { FastifyReply, FastifyRequest } from "fastify"; -import { NotHtmlMimetypeError } from "./main"; +import { EngineParseError, IFastifyValidationError, InvalidParameterError, LocalResourceError, NotHtmlMimetypeError } from "./main"; export default function errorHandler( error: Error, @@ -8,7 +8,28 @@ export default function errorHandler( ) { if (error instanceof NotHtmlMimetypeError) { return reply.redirect(301, error.url); + } 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.", + }); } else { + console.log(error); return error; } } diff --git a/src/errors/main.ts b/src/errors/main.ts index bd528bd..07893e6 100644 --- a/src/errors/main.ts +++ b/src/errors/main.ts @@ -1,10 +1,17 @@ export class EngineParseError extends Error {} export class InvalidParameterError extends Error {} export class LocalResourceError extends Error {} + export class NotHtmlMimetypeError extends Error { url: string; - constructor(params: { url: string }) { + constructor(url: string) { super(); - this.url = params?.url; + this.url = url; } } + +export interface IFastifyValidationError { + statusCode: number; + code: string; + validation: any; +} diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 0fbb172..27863d9 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -29,14 +29,14 @@ export default async function handlePage( } if (engine && engineList.indexOf(engine) === -1) { - throw new InvalidParameterError("Invalid engine"); + throw new InvalidParameterError("engine"); } const response = await axios.get(url); const mime: string | undefined = response.headers["content-type"]?.toString(); if (mime && mime.indexOf("text/html") === -1) { - throw new NotHtmlMimetypeError({ url }); + throw new NotHtmlMimetypeError(url); } const window = new JSDOM(response.data, { url }).window; diff --git a/src/types/requests.ts b/src/types/requests.ts index e395149..37271f9 100644 --- a/src/types/requests.ts +++ b/src/types/requests.ts @@ -3,11 +3,7 @@ import { handlerSchema } from "../handlers/handler.interface"; import { engineList } from "../handlers/main"; export type GetRequest = FastifyRequest<{ - Querystring: { - url: string; - format?: string; - engine?: string; - }; + Querystring: IGetQuery; }>; export interface IGetQuery {