From 9e9d1baeb737a1533662683752e70e93e41140c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20G=C3=BCndogan?= Date: Sat, 21 Mar 2020 15:03:55 +0100 Subject: [PATCH] fix error response --- app/Constants/errorCodes.js | 8 ++++++++ app/Controllers/AuthController.js | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 app/Constants/errorCodes.js diff --git a/app/Constants/errorCodes.js b/app/Constants/errorCodes.js new file mode 100644 index 0000000..e5aeac5 --- /dev/null +++ b/app/Constants/errorCodes.js @@ -0,0 +1,8 @@ +module.exports = { + errorCodes: { + DupKeyError: { + code: 11000, + message: 'Duplicate key error' + } + } +}; diff --git a/app/Controllers/AuthController.js b/app/Controllers/AuthController.js index 085c492..d3078fc 100644 --- a/app/Controllers/AuthController.js +++ b/app/Controllers/AuthController.js @@ -4,6 +4,7 @@ const bcrypt = require('bcryptjs') const jwt = require('jsonwebtoken') const UserSchema = pxl.Model('User') const { validationResult } = require('express-validator') +const { errorCodes } = require('../Constants/errorCodes'); class AuthController { // Sign up controller @@ -32,7 +33,9 @@ class AuthController { 'Successfully signed up and vertification code sent to your email.' }) } catch (error) { - return next(error) + if (error.code && error.code === errorCodes.DupKeyError.code) { + res.status(400).json({ error: { ...errorCodes.DupKeyError } }); + } } }