Render error pages as HTML

This commit is contained in:
2024-10-22 12:49:14 +02:00
parent ea58c1f60b
commit ba7336b4ee
9 changed files with 53 additions and 38 deletions

View File

@@ -2,6 +2,7 @@ import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import notFoundHandler from "../handlers/not-found.ts";
const naddrHandler = async function (ctx: Context) {
const naddr = ctx.params.path;
@@ -13,13 +14,11 @@ const naddrHandler = async function (ctx: Context) {
if (username && r.data.identifier) {
ctx.response.redirect(`/@${username}/${r.data.identifier}`);
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
};

10
handlers/not-found.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Context } from "@oak/oak";
import { errorPageHtml } from "../html.ts";
const notFoundHandler = function (ctx: Context) {
const html = errorPageHtml(404, "Not found");
ctx.response.body = html;
ctx.response.status = 404;
};
export default notFoundHandler;

View File

@@ -2,6 +2,7 @@ import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import notFoundHandler from "../handlers/not-found.ts";
const nprofileHandler = async function (ctx: Context) {
const nprofile = ctx.params.path;
@@ -13,13 +14,11 @@ const nprofileHandler = async function (ctx: Context) {
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
};

View File

@@ -2,6 +2,7 @@ import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import notFoundHandler from "../handlers/not-found.ts";
const npubHandler = async function (ctx: Context) {
const npub = ctx.params.path;
@@ -13,13 +14,11 @@ const npubHandler = async function (ctx: Context) {
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
};

View File

@@ -5,14 +5,14 @@ import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts";
import { profileAtomFeed } from "../feeds.ts";
import Article from "../models/article.ts";
import Profile from "../models/profile.ts";
import notFoundHandler from "../handlers/not-found.ts";
const userAtomFeedHandler = async function (ctx: Context) {
const username = ctx.params.user.replace(/^(@|~)/, "");
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
return;
}
@@ -33,8 +33,7 @@ const userAtomFeedHandler = async function (ctx: Context) {
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
};

View File

@@ -5,6 +5,7 @@ import { fetchProfileEvent, fetchReplaceableEvent } from "../nostr.ts";
import Article from "../models/article.ts";
import Profile from "../models/profile.ts";
import { articleHtml } from "../html.ts";
import notFoundHandler from "../handlers/not-found.ts";
const userEventHandler = async function (ctx: Context) {
const username = ctx.params.user.replace(/^(@|~)/, "");
@@ -12,8 +13,7 @@ const userEventHandler = async function (ctx: Context) {
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
return;
}
@@ -31,13 +31,11 @@ const userEventHandler = async function (ctx: Context) {
ctx.response.body = html;
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
};

View File

@@ -5,14 +5,14 @@ import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts";
import Article from "../models/article.ts";
import Profile from "../models/profile.ts";
import { profilePageHtml } from "../html.ts";
import notFoundHandler from "../handlers/not-found.ts";
const userProfileHandler = async function (ctx: Context) {
const username = ctx.params.path.replace(/^(@|~)/, "");
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
return;
}
@@ -27,13 +27,11 @@ const userProfileHandler = async function (ctx: Context) {
ctx.response.body = html;
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
notFoundHandler(ctx);
}
};