mirror of
https://github.com/txtdot/txtdot
synced 2024-10-18 14:40:19 +03:00
NotHtmlMimetype error handling
This commit is contained in:
parent
e6b61fb814
commit
1fc1d8e88b
@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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)(
|
||||
|
@ -15,6 +15,14 @@
|
||||
<h1>txt<span class="dot-err">.</span></h1>
|
||||
<p class="menu">
|
||||
<a href="/" class="button">Home</a>
|
||||
<% if (proxyBtn) { %>
|
||||
<a
|
||||
href="/proxy?url=<%= encodeURIComponent(url) %>"
|
||||
class="button secondary"
|
||||
>
|
||||
Proxy
|
||||
</a>
|
||||
<% } %>
|
||||
<a href="<%= url %>" class="button secondary">Original page</a>
|
||||
</p>
|
||||
<p><%= description %></p>
|
||||
|
Loading…
Reference in New Issue
Block a user