Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 928032e

Browse files
committed
Return better errors to help downstream clients
* Return 409 Conflict if the role already exists * Return 422 Unprocessable if there is a validation error.
1 parent b0e9f59 commit 928032e

File tree

7 files changed

+9
-9
lines changed

7 files changed

+9
-9
lines changed

memberships/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class MembershipExists extends Error {
99
constructor() {
1010
super();
1111
this.message = 'Membership already has this role';
12-
this.status = 400;
12+
this.status = 409;
1313
}
1414
}
1515
module.exports = {

test/integration/club.createMembership.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('integration:club:createMembership', () => {
4545
userType: 'mentor',
4646
})
4747
.set('Accept', 'application/json')
48-
.expect(400);
48+
.expect(409);
4949
});
5050
it('should return 404 on non-existing club', async () => {
5151
await request(app)

test/integration/lead.list.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ describe('integration:leads:list', () => {
2828
expect(res.body.total).to.equal(0);
2929
});
3030

31-
it('should return 400 on wrong params', async () => {
31+
it('should return 422 on wrong params', async () => {
3232
await request(app)
3333
.get('/leads?userId=123')
3434
.set('Accept', 'application/json')
35-
.expect(400);
35+
.expect(422);
3636
});
3737
});
3838

test/integration/membership.update.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ describe('integration:membership:update', () => {
2121
expect(res.body.userTypes).to.eql(['parent-guardian', 'mentor']);
2222
expect(res.body.userPermissions).to.eql([{ title: 'Ticketing Admin', name: 'ticketing-admin' }]);
2323
});
24-
it('should return 400 when the role already exists', async () => {
24+
it('should return 409 when the role already exists', async () => {
2525
await request(app)
2626
.post('/memberships/463e716d-e2c6-42f6-9809-dc6236f0e480/roles')
2727
.send({
2828
userType: 'mentor',
2929
})
3030
.set('Accept', 'application/json')
31-
.expect(400);
31+
.expect(409);
3232
});
3333
it('should not have duplicate permissions', async () => {
3434
const res = await request(app)

test/unit/memberships/controller/create.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('memberships/controller:create', () => {
4242
await memberController.create('u1', 'd1', 'champion');
4343
} catch (e) {
4444
expect(constr).to.not.have.been.called;
45-
expect(e.status).to.equal(400);
45+
expect(e.status).to.equal(409);
4646
}
4747
});
4848
});

test/unit/memberships/controller/createRole.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('memberships/controller:update', () => {
4242
expect(queryBuilder.findOne).to.have.been.calledOnce.and
4343
.calledWith({ id: 'u1' });
4444
expect(queryBuilder.update).to.not.have.been.called;
45-
expect(e.status).to.equal(400);
45+
expect(e.status).to.equal(409);
4646
}
4747
});
4848
});

util/ValidationHelper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ValidationHelper {
66
const errors = validationResult(req);
77
if (!errors.isEmpty()) {
88
logger.error(errors.mapped());
9-
return res.status(400).json({ errors: errors.mapped() });
9+
return res.status(422).json({ errors: errors.mapped() });
1010
}
1111
return next();
1212
}

0 commit comments

Comments
 (0)