refactor: Error Handler

This commit is contained in:
Artemy 2023-08-16 13:20:24 +03:00
parent 3b82b1232b
commit 9e29561ad3
8 changed files with 41 additions and 32 deletions

View File

@ -16,6 +16,7 @@ import indexRoute from "./routes/index";
import rawHtml from "./routes/raw-html"; import rawHtml from "./routes/raw-html";
import publicConfig from "./publicConfig"; import publicConfig from "./publicConfig";
import errorHandler from "./errors/handler";
class App { class App {
config: IConfigService; config: IConfigService;
@ -56,6 +57,8 @@ class App {
fastify.register(parseRoute); fastify.register(parseRoute);
fastify.register(rawHtml); fastify.register(rawHtml);
fastify.setErrorHandler(errorHandler);
fastify.listen({ port: Number(this.config.get("PORT")) }, (err) => { fastify.listen({ port: Number(this.config.get("PORT")) }, (err) => {
err && console.log(err); err && console.log(err);
}); });

View File

@ -1,3 +0,0 @@
export class EngineParseError extends Error {}
export class InvalidParameterError extends Error {}
export class NotHtmlMimetypeError extends Error {}

14
src/errors/handler.ts Normal file
View File

@ -0,0 +1,14 @@
import { FastifyReply, FastifyRequest } from "fastify";
import { NotHtmlMimetypeError } from "./main";
export default function errorHandler(
error: Error,
_: FastifyRequest,
reply: FastifyReply
) {
if (error instanceof NotHtmlMimetypeError) {
return reply.redirect(301, error.url);
} else {
return error;
}
}

9
src/errors/main.ts Normal file
View File

@ -0,0 +1,9 @@
export class EngineParseError extends Error {}
export class InvalidParameterError extends Error {}
export class NotHtmlMimetypeError extends Error {
url: string;
constructor(params: { url: string }) {
super();
this.url = params?.url;
}
}

View File

@ -1,6 +1,6 @@
import { DOMWindow } from "jsdom"; import { DOMWindow } from "jsdom";
import { IHandlerOutput } from "./handler.interface"; import { IHandlerOutput } from "./handler.interface";
import { EngineParseError } from "../errors"; import { EngineParseError } from "../errors/main";
export default async function google( export default async function google(
window: DOMWindow window: DOMWindow

View File

@ -8,25 +8,22 @@ import readability from "./readability";
import google from "./google"; import google from "./google";
import { generateProxyUrl } from "../utils"; import { generateProxyUrl } from "../utils";
import { InvalidParameterError, NotHtmlMimetypeError } from "../errors"; import { InvalidParameterError, NotHtmlMimetypeError } from "../errors/main";
export default async function handlePage( export default async function handlePage(
url: string, url: string,
requestUrl: URL, requestUrl: URL,
engine?: string, engine?: string
): Promise<IHandlerOutput> { ): Promise<IHandlerOutput> {
if (engine && engineList.indexOf(engine) === -1) { if (engine && engineList.indexOf(engine) === -1) {
throw new InvalidParameterError("Invalid engine"); throw new InvalidParameterError("Invalid engine");
} }
const response = await axios.get(url); const response = await axios.get(url);
const mime: string | undefined = ( const mime: string | undefined = response.headers["content-type"]?.toString();
response.headers["content-type"]?.toString()
);
if (mime && mime.indexOf("text/html") === -1) { if (mime && mime.indexOf("text/html") === -1) {
throw new NotHtmlMimetypeError(); throw new NotHtmlMimetypeError({ url });
} }
const window = new JSDOM(response.data, { url: url }).window; const window = new JSDOM(response.data, { url: url }).window;

View File

@ -1,7 +1,7 @@
import { Readability } from "@mozilla/readability"; import { Readability } from "@mozilla/readability";
import { IHandlerOutput } from "./handler.interface"; import { IHandlerOutput } from "./handler.interface";
import { DOMWindow } from "jsdom"; import { DOMWindow } from "jsdom";
import { EngineParseError } from "../errors"; import { EngineParseError } from "../errors/main";
export default async function readability( export default async function readability(
window: DOMWindow window: DOMWindow

View File

@ -4,8 +4,6 @@ import { GetSchema, IGetSchema } from "../types/requests";
import handlePage from "../handlers/main"; import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils"; import { generateRequestUrl } from "../utils";
import { NotHtmlMimetypeError } from "../errors";
export default async function getRoute(fastify: FastifyInstance) { export default async function getRoute(fastify: FastifyInstance) {
fastify.get<IGetSchema>( fastify.get<IGetSchema>(
"/get", "/get",
@ -14,9 +12,7 @@ export default async function getRoute(fastify: FastifyInstance) {
const remoteUrl = request.query.url; const remoteUrl = request.query.url;
const engine = request.query.engine; const engine = request.query.engine;
let parsed; const parsed = await handlePage(
try {
parsed = await handlePage(
remoteUrl, remoteUrl,
generateRequestUrl( generateRequestUrl(
request.protocol, request.protocol,
@ -25,13 +21,6 @@ export default async function getRoute(fastify: FastifyInstance) {
), ),
engine engine
); );
} catch (err) {
if (err instanceof NotHtmlMimetypeError) {
return reply.redirect(301, remoteUrl);
} else {
throw err;
}
}
if (request.query.format === "text") { if (request.query.format === "text") {
reply.type("text/plain; charset=utf-8"); reply.type("text/plain; charset=utf-8");