I'm working on a small express project to create and check keys. I have 2 custom errors that I throw when needed, InvalidKeyError
and NotFoundError
. I am using an error handling middleware:
import { Request, Response, NextFunction } from 'express';
import { CustomError } from '../errors/custom-error';
export const errorHandler = (
err: Error,
req: Request,
res: Response,
next: NextFunction
) => {
if (err instanceof CustomError) {
console.log(err);
return res.status(err.statusCode).send({ errors: err.serialiseErrors() });
}
res.status(400).send({
errors: [{ message: 'Something went wrong' }]
});
};
When NotFoundError
is called I get the following JSON (which is what I want):
{
"errors": [
{
"message": "Page does not exist"
}
]
}
But when I throw InvalidKeyError
my app crashes, but it does show me the error message in the console. I want it to throw like my NotFoundError
.
My InvalidKeyError
and NotFoundError
both extend my abstract class CustomError
. I throw a NotFoundError
in my index.ts
file when a route that does not exist is being requested. I call my InvalidKeyError
from a router that my index.ts
uses.
This is index.ts:
import express from 'express';
import { json } from 'body-parser';
import mongoose from 'mongoose';
import { createAPIKeyRouter } from './routes/create-api-key';
import { checkAPIKeyRouter } from './routes/check-api-key';
import { errorHandler } from './middlewares/error-handler';
import { NotFoundError } from './errors/not-found-error';
const app = express();
app.use(json());
app.use(createAPIKeyRouter);
app.use(checkAPIKeyRouter);
app.all('*', (req, res) => {
throw new NotFoundError();
});
app.use(errorHandler);
const start = async () => {
try {
await mongoose.connect("mongodb://localhost:27017/coupons");
console.log("Connected to MongoDb");
} catch (err) {
console.error(err);
}
app.listen(4000, () => {
console.log("Listening on port 4000");
});
};
start();
and the code for the router that my app uses:
import express from 'express';
import { InvalidKeyError } from '../errors/invalid-api-key-error';
import { APIKey } from '../models/api-key-model';
import { Hash } from '../services/hash';
const router = express.Router();
router.get('/api/checkkey', async (req, res) => {
const suppliedKey = req.get('key');
const suppliedSecret = req.get('secret');
if (!suppliedKey || !suppliedSecret)
throw new InvalidKeyError('Key or secret not passed');
const storedAPIKey = await APIKey.findOne({ key: suppliedKey });
if (!storedAPIKey)
throw new InvalidKeyError('Key does not exist');
if (!Hash.compareKeys(suppliedKey, suppliedSecret, storedAPIKey.salt))
throw new InvalidKeyError('Key and secret do not correspond')
res.send('Hi there');
});
export { router as checkAPIKeyRouter };