diff --git a/src/errors/handler.ts b/src/errors/handler.ts index 1b0e472..909757a 100644 --- a/src/errors/handler.ts +++ b/src/errors/handler.ts @@ -29,10 +29,6 @@ function apiErrorHandler(error: Error, reply: FastifyReply) { }); } - if (error instanceof NotHtmlMimetypeError) { - return generateResponse(501); - } - if (getFastifyError(error)?.statusCode === 400) { return generateResponse(400); } @@ -45,10 +41,6 @@ function apiErrorHandler(error: Error, reply: FastifyReply) { } function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) { - if (error instanceof NotHtmlMimetypeError) { - return reply.redirect(301, error.url); - } - if (getFastifyError(error)?.statusCode === 400) { return reply.code(400).view("/templates/error.ejs", { url, @@ -62,6 +54,7 @@ function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) { url, code: error.code, description: error.description, + proxyBtn: error instanceof NotHtmlMimetypeError, }); } diff --git a/src/errors/main.ts b/src/errors/main.ts index 29190c9..bee68f3 100644 --- a/src/errors/main.ts +++ b/src/errors/main.ts @@ -3,7 +3,11 @@ export abstract class TxtDotError extends Error { name: string; description: string; - constructor(code: number, name: string, description: string) { + constructor( + code: number, + name: string, + description: string, + ) { super(description); this.code = code; this.name = name; @@ -13,22 +17,30 @@ export abstract class TxtDotError extends Error { export class EngineParseError extends TxtDotError { constructor(message: string) { - super(422, "EngineParseError", `Parse error: ${message}`); + super( + 422, + "EngineParseError", + `Parse error: ${message}`, + ); } } export class LocalResourceError extends TxtDotError { constructor() { - super(403, "LocalResourceError", "Proxying local resources is forbidden."); + super( + 403, + "LocalResourceError", + "Proxying local resources is forbidden.", + ); } } -export class NotHtmlMimetypeError extends Error { - name: string = "NotHtmlMimetypeError"; - url: string; - - constructor(url: string) { - super(); - this.url = url; +export class NotHtmlMimetypeError extends TxtDotError { + constructor() { + super( + 421, + "NotHtmlMimetypeError", + "Received non-HTML content, use proxy", + ); } } diff --git a/src/handlers/main.ts b/src/handlers/main.ts index d3fa83a..27875a2 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -32,7 +32,7 @@ export default async function handlePage( const mime: string | undefined = response.headers["content-type"]?.toString(); if (mime && mime.indexOf("text/html") === -1) { - throw new NotHtmlMimetypeError(url); + throw new NotHtmlMimetypeError(); } return getFallbackEngine(urlObj.hostname, engine)( diff --git a/templates/error.ejs b/templates/error.ejs index 127c7da..2094b13 100644 --- a/templates/error.ejs +++ b/templates/error.ejs @@ -15,6 +15,14 @@

txt.

<%= description %>