Merge pull request #17 from TxtDot/error-handler

refactor: Error Handler
This commit is contained in:
Andrey 2023-08-16 14:29:17 +04:00 committed by GitHub
commit 34af751bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 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);
});

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 { IHandlerOutput } from "./handler.interface";
import { EngineParseError } from "../errors";
import { EngineParseError } from "../errors/main";
export default async function google(
window: DOMWindow

View File

@ -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<IHandlerOutput> {
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;

View File

@ -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

View File

@ -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<IGetSchema>(
"/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");