From e1b91010428704cf616f6b7f0cef22ac0fe9f303 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 28 Nov 2021 04:29:44 +0400 Subject: [PATCH] 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. --- homepage/index.js | 19 ++++++++++++------- homepage/views/403.pug | 34 ++++++++++++++++++++++++++++++++++ homepage/views/500.pug | 11 ++++++++++- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 homepage/views/403.pug diff --git a/homepage/index.js b/homepage/index.js index f7b19d8..128cda2 100644 --- a/homepage/index.js +++ b/homepage/index.js @@ -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()) diff --git a/homepage/views/403.pug b/homepage/views/403.pug new file mode 100644 index 0000000..bef428d --- /dev/null +++ b/homepage/views/403.pug @@ -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 \ No newline at end of file diff --git a/homepage/views/500.pug b/homepage/views/500.pug index 8f7782d..c1c2b93 100644 --- a/homepage/views/500.pug +++ b/homepage/views/500.pug @@ -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 \ No newline at end of file + | Internal Server Error + + if (error) + section#description + p= error.error + p= error.message \ No newline at end of file