Skip to content

Commit

Permalink
fix: Added errno for errors with specific codes (#488)
Browse files Browse the repository at this point in the history
* fix: Added errno for errors with specific codes

* fix: Added test for errno

* fix: Added test for errno and addressed review comments

* fix: Addressed review comments
  • Loading branch information
rackstar17 authored and kumar303 committed Sep 25, 2016
1 parent 80c59df commit 0d4141b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,18 @@ export function onlyInstancesOf(
*
*/
export function onlyErrorsWithCode(
codeWanted: string | Array<string>, errorHandler: Function): Function {
codeWanted: (string|number) | Array<string|number>,
errorHandler: Function): Function {
return (error) => {
let throwError = true;

if (Array.isArray(codeWanted) && codeWanted.indexOf(error.code) !== -1) {
throwError = false;
} else if (error.code === codeWanted) {
if (Array.isArray(codeWanted)) {
if (codeWanted.indexOf(error.code) !== -1 ||
codeWanted.indexOf(error.errno) !== -1) {
throwError = false;
}
}
else if (error.code === codeWanted || error.errno === codeWanted) {
throwError = false;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test.errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,28 @@ describe('errors', () => {
}
}

class ErrorWithErrno extends Error {
errno: number;
constructor() {
super('pretend this is a system error');
this.errno = 53;
}
}

it('catches errors having a code', () => {
return Promise.reject(new ErrorWithCode())
.catch(onlyErrorsWithCode('SOME_CODE', (error) => {
assert.equal(error.code, 'SOME_CODE');
}));
});

it('catches errors having a error no', () => {
return Promise.reject(new ErrorWithErrno())
.catch(onlyErrorsWithCode(53, (error) => {
assert.equal(error.errno, 53);
}));
});

it('throws errors that do not match the code', () => {
return Promise.reject(new SyntaxError('simulated error'))
.catch(onlyErrorsWithCode('SOME_CODE', () => {
Expand All @@ -67,6 +82,13 @@ describe('errors', () => {
}));
});

it('catches errors having one of many errno', () => {
return Promise.reject(new ErrorWithErrno())
.catch(onlyErrorsWithCode([34, 53], (error) => {
assert.equal(error.errno, 53);
}));
});

it('throws errors that are not in an array of codes', () => {
return Promise.reject(new ErrorWithCode())
.catch(onlyErrorsWithCode(['OTHER_CODE', 'ANOTHER_CODE'], () => {
Expand Down

0 comments on commit 0d4141b

Please sign in to comment.