From b01c7c5e5eee49c2fbab3164234727ea5d60df35 Mon Sep 17 00:00:00 2001 From: Brian Pina Date: Mon, 13 Sep 2021 20:33:59 -0300 Subject: [PATCH] fix: change getCode method and id from finishCampaign --- index.js | 2 +- src/controllers/CampaignController.js | 1 + src/utils/errorHandler.js | 39 +++++++++++++++++---------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index f8f2effa..ad4915e8 100644 --- a/index.js +++ b/index.js @@ -22,12 +22,12 @@ const { errorHandler } = require('./src/utils/errorHandler'); app.use(cors()); app.use(bodyParser.json()); +app.use(errorHandler); databaseConnect(); dailySchedule(); setRoutes(app); -app.use(errorHandler); app.use(Sentry.Handlers.errorHandler()); server.listen(8000); diff --git a/src/controllers/CampaignController.js b/src/controllers/CampaignController.js index 44aee146..1db5c2a6 100644 --- a/src/controllers/CampaignController.js +++ b/src/controllers/CampaignController.js @@ -80,6 +80,7 @@ class CampaignController { } async finishCampaign(req, res) { + const { id } = req.params; if(!id) throw new BadRequestError('No id provided'); const result = await this.CampaignService.finishCampaign(id); return res.status(200).json(result); diff --git a/src/utils/errorHandler.js b/src/utils/errorHandler.js index f3f4ed8f..b9fa916f 100644 --- a/src/utils/errorHandler.js +++ b/src/utils/errorHandler.js @@ -1,6 +1,6 @@ -import saveError from "./ErrorHistory"; +const saveError = require('./ErrorHistory'); -export const errorHandler = (err, req, res, next) => { +const errorHandler = (err, _req, res, _next) => { if(err instanceof GeneralError) { return res.status(err.getCode()).json({ status: 'error', @@ -15,51 +15,62 @@ export const errorHandler = (err, req, res, next) => { }) } -export class GeneralError extends Error { +class GeneralError extends Error { constructor(msg) { super(msg); } getCode() { - if(this instanceof BadRequestError) return 400; - else if(this instanceof UnauthorizedError) return 404; - else if(this instanceof ForbiddenError) return 404; - else if(this instanceof NotFoundError) return 404; - else if(this instanceof ConflictError) return 409; + return this.code; } } -export class BadRequestError extends GeneralError { +class BadRequestError extends GeneralError { constructor(msg) { super(msg); + this.code = 400; this.name = `BadRequestError:${msg}`; } } -export class UnauthorizedError extends GeneralError { +class UnauthorizedError extends GeneralError { constructor(msg) { super(msg); + this.code = 401; this.name = `UnauthorizedError:${msg}`; } } -export class ForbiddenError extends GeneralError { +class ForbiddenError extends GeneralError { constructor(msg) { super(msg); + this.code = 403; this.name = `ForbiddenError:${msg}`; } } -export class NotFoundError extends GeneralError { +class NotFoundError extends GeneralError { constructor(msg) { super(msg); + this.code = 404; this.name = `NotFoundError:${msg}`; } } -export class ConflictError extends GeneralError { +class ConflictError extends GeneralError { constructor(msg) { super(msg); + this.code = 409; this.name = `ConflictError:${msg}`; } -} \ No newline at end of file +} + +module.exports = { + errorHandler, + BadRequestError, + ConflictError, + ForbiddenError, + GeneralError, + NotFoundError, + UnauthorizedError +}; \ No newline at end of file