diff --git a/src/errors/handler.ts b/src/errors/handler.ts index b23560b..1b0e472 100644 --- a/src/errors/handler.ts +++ b/src/errors/handler.ts @@ -2,6 +2,8 @@ import { FastifyReply, FastifyRequest } from "fastify"; import { NotHtmlMimetypeError, TxtDotError } from "./main"; import { getFastifyError } from "./validation"; +import { IGetSchema } from "../types/requests/browser"; + export default function errorHandler( error: Error, req: FastifyRequest, @@ -10,7 +12,9 @@ export default function errorHandler( if (req.originalUrl.startsWith("/api/")) { return apiErrorHandler(error, reply); } - return htmlErrorHandler(error, reply); + + const url = (req as FastifyRequest).query.url; + return htmlErrorHandler(error, reply, url); } function apiErrorHandler(error: Error, reply: FastifyReply) { @@ -40,13 +44,14 @@ function apiErrorHandler(error: Error, reply: FastifyReply) { return generateResponse(500); } -function htmlErrorHandler(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, code: 400, description: `Invalid parameter specified: ${error.message}`, }) @@ -54,12 +59,14 @@ function htmlErrorHandler(error: Error, reply: FastifyReply) { if (error instanceof TxtDotError) { return reply.code(error.code).view("/templates/error.ejs", { + url, code: error.code, description: error.description, }); } return reply.code(500).view("/templates/error.ejs", { + url, code: 500, description: `${error.name}: ${error.message}`, }); diff --git a/templates/error.ejs b/templates/error.ejs index 10aba6b..127c7da 100644 --- a/templates/error.ejs +++ b/templates/error.ejs @@ -13,9 +13,12 @@

txt.

+

<%= description %>

- Home