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 20, 2021
1 parent 9fe3659 commit b01c7c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 b01c7c5

Please sign in to comment.