From aec673fc53c2b41618b4a6998357eb2324563027 Mon Sep 17 00:00:00 2001 From: im-snowin Date: Sun, 18 Dec 2022 01:15:06 +0530 Subject: [PATCH] created error handlers for user and products --- src/handlers/product.ts | 26 +++++++++++++++++--------- src/handlers/user.ts | 34 +++++++++++++++++----------------- src/server.ts | 4 ++++ src/utils/errorHandler.ts | 16 ++++++++++++++++ 4 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 src/utils/errorHandler.ts diff --git a/src/handlers/product.ts b/src/handlers/product.ts index bfdb7da..a74ee02 100644 --- a/src/handlers/product.ts +++ b/src/handlers/product.ts @@ -1,4 +1,4 @@ -import { Response } from 'express'; +import { NextFunction, Response } from 'express'; import prisma from '../prisma'; import { CustomRequest } from '../types'; @@ -26,15 +26,23 @@ export const getOneProduct = async (req: CustomRequest, res: Response) => { }; // Create product -export const createProduct = async (req: CustomRequest, res: Response) => { - const product = await prisma.product.create({ - data: { - name: req.body.name, - ownerId: req.user.id, - }, - }); +export const createProduct = async ( + req: CustomRequest, + res: Response, + next: NextFunction +) => { + try { + const product = await prisma.product.create({ + data: { + name: req.body.name, + ownerId: req.user.id, + }, + }); - res.json({ data: product }); + res.json({ data: product }); + } catch (e) { + next(e); + } }; // update product diff --git a/src/handlers/user.ts b/src/handlers/user.ts index 8c515b3..b9630c2 100644 --- a/src/handlers/user.ts +++ b/src/handlers/user.ts @@ -1,26 +1,26 @@ -import { Request, Response } from 'express'; +import { NextFunction, Request, Response } from 'express'; import prisma from '../prisma'; import { comparePassword, createJWT, hashPassword } from '../utils/auth'; -export const createNewUser = async (req: Request, res: Response) => { - if (!req.body.password && !req.body.user) { - res.status(404); - res.json({ - message: 'Nope', +export const createNewUser = async ( + req: Request, + res: Response, + next: NextFunction +) => { + try { + const user = await prisma.user.create({ + data: { + username: req.body.username, + password: await hashPassword(req.body.password), + }, }); - return; + const token = createJWT(user); + res.json({ token }); + } catch (e) { + e.type = 'input'; + next(e); } - - const user = await prisma.user.create({ - data: { - username: req.body.username, - password: await hashPassword(req.body.password), - }, - }); - - const token = createJWT(user); - res.json({ token }); }; export const signIn = async (req: Request, res: Response) => { diff --git a/src/server.ts b/src/server.ts index afd05cb..c1d77f9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,6 +6,7 @@ dotenv.config(); import router from './router'; import { protect } from './utils/auth'; import { createNewUser, signIn } from './handlers/user'; +import { errorHandler } from './utils/errorHandler'; const app = express(); @@ -20,4 +21,7 @@ app.use('/api', protect, router); app.post('/user', createNewUser); app.post('/signin', signIn); +/** Error handler **/ +app.use(errorHandler); + export default app; diff --git a/src/utils/errorHandler.ts b/src/utils/errorHandler.ts new file mode 100644 index 0000000..ce8e64d --- /dev/null +++ b/src/utils/errorHandler.ts @@ -0,0 +1,16 @@ +import { NextFunction, Request, Response } from 'express'; + +export const errorHandler = async ( + err, + req: Request, + res: Response, + next: NextFunction +) => { + if (err.type === 'auth') { + res.status(401).json({ message: 'unauthorized' }); + } else if (err.type === 'input') { + res.status(400).json({ message: 'invalid input' }); + } else { + res.status(500).json({ message: 'oops, thats on us' }); + } +};