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