diff --git a/src/app.ts b/src/app.ts index 2d9ea54..6f67873 100644 --- a/src/app.ts +++ b/src/app.ts @@ -16,6 +16,7 @@ import indexRoute from "./routes/index"; import rawHtml from "./routes/raw-html"; import publicConfig from "./publicConfig"; +import errorHandler from "./errors/handler"; class App { config: IConfigService; @@ -56,6 +57,8 @@ class App { fastify.register(parseRoute); fastify.register(rawHtml); + fastify.setErrorHandler(errorHandler); + fastify.listen({ port: Number(this.config.get("PORT")) }, (err) => { err && console.log(err); }); diff --git a/src/errors.ts b/src/errors.ts deleted file mode 100644 index f686ce2..0000000 --- a/src/errors.ts +++ /dev/null @@ -1,3 +0,0 @@ -export class EngineParseError extends Error {} -export class InvalidParameterError extends Error {} -export class NotHtmlMimetypeError extends Error {} diff --git a/src/errors/handler.ts b/src/errors/handler.ts new file mode 100644 index 0000000..58365ae --- /dev/null +++ b/src/errors/handler.ts @@ -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; + } +} diff --git a/src/errors/main.ts b/src/errors/main.ts new file mode 100644 index 0000000..679a2da --- /dev/null +++ b/src/errors/main.ts @@ -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; + } +} diff --git a/src/handlers/google.ts b/src/handlers/google.ts index 116e02c..d6bb780 100644 --- a/src/handlers/google.ts +++ b/src/handlers/google.ts @@ -1,6 +1,6 @@ import { DOMWindow } from "jsdom"; import { IHandlerOutput } from "./handler.interface"; -import { EngineParseError } from "../errors"; +import { EngineParseError } from "../errors/main"; export default async function google( window: DOMWindow diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 475d9b4..e14f7e9 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -8,25 +8,22 @@ import readability from "./readability"; import google from "./google"; import { generateProxyUrl } from "../utils"; -import { InvalidParameterError, NotHtmlMimetypeError } from "../errors"; +import { InvalidParameterError, NotHtmlMimetypeError } from "../errors/main"; export default async function handlePage( url: string, requestUrl: URL, - engine?: string, + engine?: string ): Promise { - if (engine && engineList.indexOf(engine) === -1) { throw new InvalidParameterError("Invalid engine"); } 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) { - throw new NotHtmlMimetypeError(); + throw new NotHtmlMimetypeError({ url }); } const window = new JSDOM(response.data, { url: url }).window; diff --git a/src/handlers/readability.ts b/src/handlers/readability.ts index 191a754..f629aa2 100644 --- a/src/handlers/readability.ts +++ b/src/handlers/readability.ts @@ -1,7 +1,7 @@ import { Readability } from "@mozilla/readability"; import { IHandlerOutput } from "./handler.interface"; import { DOMWindow } from "jsdom"; -import { EngineParseError } from "../errors"; +import { EngineParseError } from "../errors/main"; export default async function readability( window: DOMWindow diff --git a/src/routes/get.ts b/src/routes/get.ts index 8fd4f2e..b13251d 100644 --- a/src/routes/get.ts +++ b/src/routes/get.ts @@ -4,8 +4,6 @@ import { GetSchema, IGetSchema } from "../types/requests"; import handlePage from "../handlers/main"; import { generateRequestUrl } from "../utils"; -import { NotHtmlMimetypeError } from "../errors"; - export default async function getRoute(fastify: FastifyInstance) { fastify.get( "/get", @@ -14,24 +12,15 @@ export default async function getRoute(fastify: FastifyInstance) { const remoteUrl = request.query.url; const engine = request.query.engine; - let parsed; - try { - parsed = await handlePage( - remoteUrl, - generateRequestUrl( - request.protocol, - request.hostname, - request.originalUrl - ), - engine - ); - } catch (err) { - if (err instanceof NotHtmlMimetypeError) { - return reply.redirect(301, remoteUrl); - } else { - throw err; - } - } + const parsed = await handlePage( + remoteUrl, + generateRequestUrl( + request.protocol, + request.hostname, + request.originalUrl + ), + engine + ); if (request.query.format === "text") { reply.type("text/plain; charset=utf-8");