"Original page" button in error.ejs

This commit is contained in:
DarkCat09 2023-09-20 14:40:57 +04:00
parent 2245e73c7f
commit 5b7ce0bb3c
No known key found for this signature in database
GPG Key ID: 0A26CD5B3345D6E3
2 changed files with 13 additions and 3 deletions

View File

@ -2,6 +2,8 @@ import { FastifyReply, FastifyRequest } from "fastify";
import { NotHtmlMimetypeError, TxtDotError } from "./main"; import { NotHtmlMimetypeError, TxtDotError } from "./main";
import { getFastifyError } from "./validation"; import { getFastifyError } from "./validation";
import { IGetSchema } from "../types/requests/browser";
export default function errorHandler( export default function errorHandler(
error: Error, error: Error,
req: FastifyRequest, req: FastifyRequest,
@ -10,7 +12,9 @@ export default function errorHandler(
if (req.originalUrl.startsWith("/api/")) { if (req.originalUrl.startsWith("/api/")) {
return apiErrorHandler(error, reply); return apiErrorHandler(error, reply);
} }
return htmlErrorHandler(error, reply);
const url = (req as FastifyRequest<IGetSchema>).query.url;
return htmlErrorHandler(error, reply, url);
} }
function apiErrorHandler(error: Error, reply: FastifyReply) { function apiErrorHandler(error: Error, reply: FastifyReply) {
@ -40,13 +44,14 @@ function apiErrorHandler(error: Error, reply: FastifyReply) {
return generateResponse(500); return generateResponse(500);
} }
function htmlErrorHandler(error: Error, reply: FastifyReply) { function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) {
if (error instanceof NotHtmlMimetypeError) { if (error instanceof NotHtmlMimetypeError) {
return reply.redirect(301, error.url); return reply.redirect(301, error.url);
} }
if (getFastifyError(error)?.statusCode === 400) { if (getFastifyError(error)?.statusCode === 400) {
return reply.code(400).view("/templates/error.ejs", { return reply.code(400).view("/templates/error.ejs", {
url,
code: 400, code: 400,
description: `Invalid parameter specified: ${error.message}`, description: `Invalid parameter specified: ${error.message}`,
}) })
@ -54,12 +59,14 @@ function htmlErrorHandler(error: Error, reply: FastifyReply) {
if (error instanceof TxtDotError) { if (error instanceof TxtDotError) {
return reply.code(error.code).view("/templates/error.ejs", { return reply.code(error.code).view("/templates/error.ejs", {
url,
code: error.code, code: error.code,
description: error.description, description: error.description,
}); });
} }
return reply.code(500).view("/templates/error.ejs", { return reply.code(500).view("/templates/error.ejs", {
url,
code: 500, code: 500,
description: `${error.name}: ${error.message}`, description: `${error.name}: ${error.message}`,
}); });

View File

@ -13,9 +13,12 @@
<main> <main>
<header> <header>
<h1>txt<span class="dot-err">.</span></h1> <h1>txt<span class="dot-err">.</span></h1>
<p class="menu">
<a href="/" class="button">Home</a>
<a href="<%= url %>" class="button secondary">Original page</a>
</p>
<p><%= description %></p> <p><%= description %></p>
</header> </header>
<a href="/" class="button secondary">Home</a>
</main> </main>
</body> </body>
</html> </html>