Finally fixed error handling. Also added 403 page. And now if something's wrong with DB or your post then your message will be printed to you, so you don't waste your time.

This commit is contained in:
Alexander Andreev 2021-11-28 04:29:44 +04:00
parent 0c12bf47d6
commit e1b9101042
Signed by: Arav
GPG Key ID: 610DF2574456329F
3 changed files with 56 additions and 8 deletions

View File

@ -122,11 +122,14 @@ function setRoutes() {
if (await guestbook.addPost(post))
ctx.redirect("/guestbook");
} catch(err) {
ctx.type = "text/plain";
if (err instanceof MysqlError)
ctx.body = `Database failed so your post wasn't added. So your time wasn't wasted here's your message:\n${post.message}`;
else
ctx.body = `Your post was rejected because of "${err}". So your time wasn't wasted here's your message:\n${post.message}`;
if (typeof err == 'object' && err instanceof MysqlError) {
ctx.response.status = 500;
ctx.response.body = { error: `Database failed so your post wasn't added. Here's your message:`, message: post.message };
} else if (typeof err == 'string') {
ctx.response.status = 403;
ctx.response.body = { error: `Reason why your post was rejected is "${err}". Here's your message:`, message: post.message };
}
}
});
}
@ -148,13 +151,15 @@ app
.use(async (ctx, next) => {
try {
await next();
if (ctx.status === 404) ctx.throw(404);
if (ctx.status !== 200) ctx.throw(ctx.status);
} catch (err) {
ctx.status = err.status || 500;
if (ctx.status === 404)
await ctx.render('404');
else if (ctx.status === 403)
await ctx.render('403', { error: ctx.body });
else
await ctx.render('500');
await ctx.render('500', { error: ctx.body });
}
})
.use(setRoutes().routes())

34
homepage/views/403.pug Normal file
View File

@ -0,0 +1,34 @@
extends base.pug
block head
style(type="text/css").
#error {
font-size: 3.5rem;
line-height: 5rem;
text-align: center;
margin: 6rem 0; }
#error h1 { font-size: 8rem; }
#description p { text-align: center; }
#description p:first-child { font-size: 1.4rem; }
block nav
nav
a(href='/') Home
a(href='/stuff') Stuff
a(href='/mindflow') Mindflow
a(href='/about') About
a(href='/guestbook') Guestbook
h1 Whoa whoa, watcha tryin'?
block content
section#error
h1 403
| Forbidden
if (error)
section#description
p= error.error
p= error.message

View File

@ -10,6 +10,10 @@ block head
#error h1 { font-size: 8rem; }
#description p { text-align: center; }
#description p:first-child { font-size: 1.4rem; }
block nav
nav
a(href='/') Home
@ -22,4 +26,9 @@ block nav
block content
section#error
h1 50x
| Internal Server Error
| Internal Server Error
if (error)
section#description
p= error.error
p= error.message