"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 { 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<IGetSchema>).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}`,
});

View File

@ -13,9 +13,12 @@
<main>
<header>
<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>
</header>
<a href="/" class="button secondary">Home</a>
</main>
</body>
</html>