Weird error matching

This commit is contained in:
DarkCat09 2023-08-21 17:03:28 +04:00
parent f39f3d9225
commit 7cb254f523
No known key found for this signature in database
GPG Key ID: 0A26CD5B3345D6E3
4 changed files with 34 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import { FastifyReply, FastifyRequest } from "fastify"; import { FastifyReply, FastifyRequest } from "fastify";
import { NotHtmlMimetypeError } from "./main"; import { EngineParseError, IFastifyValidationError, InvalidParameterError, LocalResourceError, NotHtmlMimetypeError } from "./main";
export default function errorHandler( export default function errorHandler(
error: Error, error: Error,
@ -8,7 +8,28 @@ export default function errorHandler(
) { ) {
if (error instanceof NotHtmlMimetypeError) { if (error instanceof NotHtmlMimetypeError) {
return reply.redirect(301, error.url); 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 { } else {
console.log(error);
return error; return error;
} }
} }

View File

@ -1,10 +1,17 @@
export class EngineParseError extends Error {} export class EngineParseError extends Error {}
export class InvalidParameterError extends Error {} export class InvalidParameterError extends Error {}
export class LocalResourceError extends Error {} export class LocalResourceError extends Error {}
export class NotHtmlMimetypeError extends Error { export class NotHtmlMimetypeError extends Error {
url: string; url: string;
constructor(params: { url: string }) { constructor(url: string) {
super(); super();
this.url = params?.url; this.url = url;
} }
} }
export interface IFastifyValidationError {
statusCode: number;
code: string;
validation: any;
}

View File

@ -29,14 +29,14 @@ export default async function handlePage(
} }
if (engine && engineList.indexOf(engine) === -1) { if (engine && engineList.indexOf(engine) === -1) {
throw new InvalidParameterError("Invalid engine"); throw new InvalidParameterError("engine");
} }
const response = await axios.get(url); const response = await axios.get(url);
const mime: string | undefined = response.headers["content-type"]?.toString(); const mime: string | undefined = response.headers["content-type"]?.toString();
if (mime && mime.indexOf("text/html") === -1) { if (mime && mime.indexOf("text/html") === -1) {
throw new NotHtmlMimetypeError({ url }); throw new NotHtmlMimetypeError(url);
} }
const window = new JSDOM(response.data, { url }).window; const window = new JSDOM(response.data, { url }).window;

View File

@ -3,11 +3,7 @@ import { handlerSchema } from "../handlers/handler.interface";
import { engineList } from "../handlers/main"; import { engineList } from "../handlers/main";
export type GetRequest = FastifyRequest<{ export type GetRequest = FastifyRequest<{
Querystring: { Querystring: IGetQuery;
url: string;
format?: string;
engine?: string;
};
}>; }>;
export interface IGetQuery { export interface IGetQuery {