Skip to content

Commit

Permalink
fix: change getCode method and id from finishCampaign
Browse files Browse the repository at this point in the history
  • Loading branch information
DLBrianPina committed Sep 17, 2021
1 parent 9fe3659 commit c4b2efe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/controllers/CampaignController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 25 additions & 14 deletions src/utils/errorHandler.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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}`;
}
}
}

module.exports = {
errorHandler,
BadRequestError,
ConflictError,
ForbiddenError,
GeneralError,
NotFoundError,
UnauthorizedError
};

0 comments on commit c4b2efe

Please sign in to comment.