Skip to content

Commit

Permalink
404 Page not found added (#225)
Browse files Browse the repository at this point in the history
* replace repose of aliases with error page

* error twig modified with proper messages

* add the error pcss

* the lint removed

* centered the error message

* center style added independent of screen

* the top position changed

* aliases not found added

* aliases throw removed

* Update src/backend/controllers/pages.ts

Co-authored-by: Peter Savchenko <[email protected]>

Co-authored-by: Peter Savchenko <[email protected]>
  • Loading branch information
robonetphy and neSpecc authored Sep 1, 2022
1 parent 09835e3 commit 05f8f0d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
4 changes: 0 additions & 4 deletions src/backend/controllers/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class Aliases {
public static async get(aliasName: string): Promise<Alias> {
const alias = await Alias.get(aliasName);

if (!alias.id) {
throw new Error('Entity with given alias does not exist');
}

return alias;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/models/pagesFlatArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface PagesFlatArrayData {
}

/**
* @class PagesFlatArray model - flat array of pages, which are ordered like in sidebar
* @class PagesFlatArray model - flat array of pages, which are ordered like in sidebar
*/
class PagesFlatArray {
/**
Expand Down
31 changes: 20 additions & 11 deletions src/backend/routes/aliases.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import express, { Request, Response } from 'express';
import Aliases from '../controllers/aliases.js';
import Pages from '../controllers/pages.js';
import Alias from '../models/alias.js';
import verifyToken from './middlewares/token.js';
import PagesFlatArray from '../models/pagesFlatArray.js';
import Aliases from '../controllers/aliases';
import Pages from '../controllers/pages';
import Alias from '../models/alias';
import verifyToken from './middlewares/token';
import PagesFlatArray from '../models/pagesFlatArray';
import HttpException from '../exceptions/httpException';


const router = express.Router();

Expand All @@ -24,7 +26,7 @@ router.get('*', verifyToken, async (req: Request, res: Response) => {
const alias = await Aliases.get(url);

if (alias.id === undefined) {
throw new Error('Alias not found');
throw new HttpException(404, 'Alias not found');
}

switch (alias.type) {
Expand All @@ -46,11 +48,18 @@ router.get('*', verifyToken, async (req: Request, res: Response) => {
}
}
} catch (err) {
res.status(400).json({
success: false,
error: err,
});
if (err instanceof HttpException && err.status === 404) {
res.status(404).render('error', {
message: 'Page not found',
status: 404,
});
} else {
res.status(500).json({
success: false,
error: err,
});
}
}
});

export default router;
export default router;
9 changes: 6 additions & 3 deletions src/backend/views/error.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{% extends 'layout.twig' %}

{% block body %}
<h1>{{message}}</h1>
<h2>{{error.status}}</h2>
<pre>{{error.stack}}</pre>
<div class="error-page">
<h1>
┬┴┬┴┤ {{status}} ├┬┴┬┴
</h1>
<h1>{{message}}</h1>
</div>
{% endblock %}
29 changes: 29 additions & 0 deletions src/frontend/styles/components/error.pcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.error-page {
font-size: 15px;
text-align: center;
position: absolute;
top: 45%;
left: 50%;

@media (--mobile) {
position: relative;
top: 30vh;
left: 0;
}

@media (--tablet) {
position: relative;
top: 30vh;
left: 0;
}

h1 {
@media (--mobile) {
font-size: 20px;
}
}

p {
margin: 40px 0 20px;
}
}
1 change: 1 addition & 0 deletions src/frontend/styles/main.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@import './components/page.pcss';
@import './components/landing.pcss';
@import './components/auth.pcss';
@import './components/error.pcss';
@import './components/button.pcss';
@import './components/sidebar.pcss';
@import './components/navigator.pcss';
Expand Down

0 comments on commit 05f8f0d

Please sign in to comment.