mirror of
https://github.com/txtdot/txtdot
synced 2024-10-18 14:40:19 +03:00
Weird error matching
This commit is contained in:
parent
f39f3d9225
commit
7cb254f523
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user