refactor: Error Handler
This commit is contained in:
parent
3b82b1232b
commit
9e29561ad3
@ -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);
|
||||
});
|
||||
|
@ -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
14
src/errors/handler.ts
Normal 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
9
src/errors/main.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user