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) {
|
if (getFastifyError(error)?.statusCode === 400) {
|
||||||
return generateResponse(400);
|
return generateResponse(400);
|
||||||
}
|
}
|
||||||
@ -45,10 +41,6 @@ function apiErrorHandler(error: Error, reply: FastifyReply) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) {
|
function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) {
|
||||||
if (error instanceof NotHtmlMimetypeError) {
|
|
||||||
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,
|
url,
|
||||||
@ -62,6 +54,7 @@ function htmlErrorHandler(error: Error, reply: FastifyReply, url: string) {
|
|||||||
url,
|
url,
|
||||||
code: error.code,
|
code: error.code,
|
||||||
description: error.description,
|
description: error.description,
|
||||||
|
proxyBtn: error instanceof NotHtmlMimetypeError,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,11 @@ export abstract class TxtDotError extends Error {
|
|||||||
name: string;
|
name: string;
|
||||||
description: string;
|
description: string;
|
||||||
|
|
||||||
constructor(code: number, name: string, description: string) {
|
constructor(
|
||||||
|
code: number,
|
||||||
|
name: string,
|
||||||
|
description: string,
|
||||||
|
) {
|
||||||
super(description);
|
super(description);
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -13,22 +17,30 @@ export abstract class TxtDotError extends Error {
|
|||||||
|
|
||||||
export class EngineParseError extends TxtDotError {
|
export class EngineParseError extends TxtDotError {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(422, "EngineParseError", `Parse error: ${message}`);
|
super(
|
||||||
|
422,
|
||||||
|
"EngineParseError",
|
||||||
|
`Parse error: ${message}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LocalResourceError extends TxtDotError {
|
export class LocalResourceError extends TxtDotError {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(403, "LocalResourceError", "Proxying local resources is forbidden.");
|
super(
|
||||||
|
403,
|
||||||
|
"LocalResourceError",
|
||||||
|
"Proxying local resources is forbidden.",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NotHtmlMimetypeError extends Error {
|
export class NotHtmlMimetypeError extends TxtDotError {
|
||||||
name: string = "NotHtmlMimetypeError";
|
constructor() {
|
||||||
url: string;
|
super(
|
||||||
|
421,
|
||||||
constructor(url: string) {
|
"NotHtmlMimetypeError",
|
||||||
super();
|
"Received non-HTML content, use proxy",
|
||||||
this.url = url;
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ export default async function handlePage(
|
|||||||
const mime: string | undefined = response.headers["content-type"]?.toString();
|
const mime: string | undefined = response.headers["content-type"]?.toString();
|
||||||
|
|
||||||
if (mime && mime.indexOf("text/html") === -1) {
|
if (mime && mime.indexOf("text/html") === -1) {
|
||||||
throw new NotHtmlMimetypeError(url);
|
throw new NotHtmlMimetypeError();
|
||||||
}
|
}
|
||||||
|
|
||||||
return getFallbackEngine(urlObj.hostname, engine)(
|
return getFallbackEngine(urlObj.hostname, engine)(
|
||||||
|
@ -15,6 +15,14 @@
|
|||||||
<h1>txt<span class="dot-err">.</span></h1>
|
<h1>txt<span class="dot-err">.</span></h1>
|
||||||
<p class="menu">
|
<p class="menu">
|
||||||
<a href="/" class="button">Home</a>
|
<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>
|
<a href="<%= url %>" class="button secondary">Original page</a>
|
||||||
</p>
|
</p>
|
||||||
<p><%= description %></p>
|
<p><%= description %></p>
|
||||||
|
Loading…
Reference in New Issue
Block a user