Skip to content

Commit

Permalink
Adding error handling route
Browse files Browse the repository at this point in the history
  • Loading branch information
hgorges committed Aug 25, 2024
1 parent 569e9c5 commit 0143508
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 20 deletions.
2 changes: 1 addition & 1 deletion public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ p {
font-size: small;
}

.not-found {
.error {
display: flex;
justify-content: center;
align-items: center;
Expand Down
22 changes: 22 additions & 0 deletions src/controller/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextFunction, Request, Response } from 'express-serve-static-core';

export function renderErrorPage(
req: Request,
res: Response,
_next: NextFunction,
): void {
res.status(500).render('error', {
path: '/error',
isAdmin: req.session.isAdmin,
});
}

export function errorHandler(
error: any,
_req: Request,
res: Response,
_next: NextFunction,
): void {
console.error(error);
res.redirect(`/error`);
}
6 changes: 5 additions & 1 deletion src/controller/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export async function login(
return;
}

const user = await userModel.getUserForLogin(username, password);
const user = await userModel
.getUserForLogin(username, password)
.catch((error) => {
next(new Error(error));
});
if (!user) {
req.flash('error', 'Invalid username or password!');
renderLogin(req, res, next, {
Expand Down
3 changes: 2 additions & 1 deletion src/controller/notFound.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Request, Response } from 'express-serve-static-core';

export function renderNotFoundPage(_req: Request, res: Response): void {
export function renderNotFoundPage(req: Request, res: Response): void {
res.status(404).render('not-found', {
path: '/not-found',
isAdmin: req.session.isAdmin,
});
}
25 changes: 15 additions & 10 deletions src/controller/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export async function signup(
return;
}

if (await userModel.getUserByUsername(username)) {
if (
await userModel.getUserByUsername(username).catch((error) => {
next(new Error(error));
})
) {
req.flash('error', 'Username is already taken!');
renderSignup(req, res, next, {
statusCode: 422,
Expand All @@ -85,7 +89,11 @@ export async function signup(
});
return;
}
if (await userModel.getUserByEmail(email)) {
if (
await userModel.getUserByEmail(email).catch((error) => {
next(new Error(error));
})
) {
req.flash('error', 'Email is already taken!');
renderSignup(req, res, next, {
statusCode: 422,
Expand Down Expand Up @@ -121,14 +129,11 @@ export async function signup(
return;
}

await userModel.createUser(
username,
firstName,
lastName,
email,
password,
username,
);
await userModel
.createUser(username, firstName, lastName, email, password, username)
.catch((error) => {
next(new Error(error));
});

// Do not await the mailer
mailer.sendMail({
Expand Down
5 changes: 4 additions & 1 deletion src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import {
switchLocation,
} from '../controller/dashboard';
import { renderAdminPage } from '../controller/admin';
import { renderNotFoundPage } from '../controller/notFound';
import validateEmptyBody from '../validators/validateEmptyBody';
import validateSettings from '../validators/validateSettings';
import { renderNotFoundPage } from '../controller/notFound';
import { renderErrorPage } from '../controller/error';

const userRouter = express.Router();

Expand All @@ -38,6 +39,8 @@ userRouter.post('/settings', validateSettings, saveSettings);

userRouter.get('/not-found', renderNotFoundPage);

userRouter.get('/error', renderErrorPage);

// Middleware to redirect to /not-found
userRouter.use((req, res) => {
console.log(`Redirecting from ${req.url} to /not-found`);
Expand Down
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import knexConfig from './db/knexfile';
import configRouter from './routes/configRoutes';
import sessionRouter from './routes/sessionRoutes';
import userRouter from './routes/userRoutes';
import { errorHandler } from './controller/error';

// Loading environment variables
dotenv.config({ path: '../secrets/.env' });
Expand All @@ -22,6 +23,7 @@ db.migrate.latest().finally(() => {
app.use(configRouter);
app.use(sessionRouter);
app.use(userRouter);
app.use(errorHandler);

const privateKey = fs.readFileSync(
path.join(__dirname, '..', 'secrets', 'cryptospace.key'),
Expand Down
11 changes: 11 additions & 0 deletions views/error.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%- include('includes/head.ejs') %>
</head>

<body>
<%- include('includes/navigation.ejs') %>

<div class="error">
<p>Error 500 - Something went wrong!</p>
</div>

<%- include('includes/end.ejs') %>
12 changes: 6 additions & 6 deletions views/not-found.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
</head>

<body>
<%- include('includes/navigation.ejs') %>

<div class="not-found">
<p>Error 404 - Page Not Found</p>
</div>
<%- include('includes/navigation.ejs') %>

<%- include('includes/end.ejs') %>
<div class="error">
<p>Error 404 - Page Not Found</p>
</div>

<%- include('includes/end.ejs') %>

0 comments on commit 0143508

Please sign in to comment.