Skip to content

Commit

Permalink
Merge pull request #57 from barracksiot/error_handling_fix
Browse files Browse the repository at this point in the history
Error handling fix
  • Loading branch information
Greg committed Aug 7, 2017
2 parents 63736eb + 080141d commit d6d5180
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/clients/HookClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ class HookClient {
).then(response => {
resolve(response.body);
}).catch(errResponse => {
reject(errResponse.message);
if (errResponse.statusCode === 404) {
reject('This is not the hook you are looking for.');
}
else {
reject(errResponse.message);
}
});
});
}
Expand Down Expand Up @@ -108,8 +113,15 @@ class HookClient {
}
).then(response => {
resolve(response.body);
}).catch(err => {
reject(err.message);
}).catch(errResponse => {
if (errResponse.statusCode === 404) {
reject('The hook you want to update does not exist.');
}
else if (errResponse.statusCode === 409){
reject('A hook with this name already exists.');
} else {
reject(errResponse.message);
}
});
});
}
Expand Down
77 changes: 77 additions & 0 deletions tests/clients/HookClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ describe('hookClient', () => {
});
});

it('should return an error when request fails with 404', done => {
// Given
const error = { statusCode: 404, message: 'Error !' };
hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error));

// When / Then
hookClient.getHook(token, hookName).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals('This is not the hook you are looking for.');
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'GET',
path: '/api/dispatcher/hooks/:hook'
},
{
headers: { 'x-auth-token': token },
pathVariables: {
hook: hookName
}
}
);
done();
});
});

it('should return specified hook when request succeeds', done => {
// Given
const response = { body: hook };
Expand Down Expand Up @@ -218,6 +245,56 @@ describe('hookClient', () => {
});
});

it('should return an error message when request fails with 404', done => {
// Given
const error = { statusCode: 404, message: 'Error !' };
hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error));

// When / Then
hookClient.updateHook(token, hookName, newHook).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals('The hook you want to update does not exist.');
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'PUT',
path: '/api/dispatcher/hooks/:hook'
}, {
headers: { 'x-auth-token': token },
pathVariables: { hook : hookName},
body: newHook
}
);
done();
});
});

it('should return an error message when request fails with 409', done => {
// Given
const error = { statusCode: 409, message: 'Error !' };
hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error));

// When / Then
hookClient.updateHook(token, hookName, newHook).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals('A hook with this name already exists.');
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'PUT',
path: '/api/dispatcher/hooks/:hook'
}, {
headers: { 'x-auth-token': token },
pathVariables: { hook : hookName},
body: newHook
}
);
done();
});
});

it('should return the updated hook', done => {
// Given
const response = { body: newHook };
Expand Down

0 comments on commit d6d5180

Please sign in to comment.