Skip to content

Commit

Permalink
Feat network error once (#141)
Browse files Browse the repository at this point in the history
* Add shortcut function networkErrorOnce and timoutOnce

* Set replyOnce function
  • Loading branch information
mikaoelitiana authored and ctimmerm committed Jun 20, 2018
1 parent 8abf4a5 commit 4a0e874
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,16 @@ VERBS.concat('any').forEach(function(method) {
return _this;
}

function replyOnce(code, response, headers) {
var handler = [matcher, body, requestHeaders, code, response, headers, true];
addHandler(method, _this.handlers, handler);
return _this;
}

return {
reply: reply,

replyOnce: function replyOnce(code, response, headers) {
var handler = [matcher, body, requestHeaders, code, response, headers, true];
addHandler(method, _this.handlers, handler);
return _this;
},
replyOnce: replyOnce,

passThrough: function passThrough() {
var handler = [matcher, body];
Expand All @@ -99,13 +101,30 @@ VERBS.concat('any').forEach(function(method) {
});
},

networkErrorOnce: function() {
replyOnce(function(config) {
var error = new Error('Network Error');
error.config = config;
return Promise.reject(error);
});
},

timeout: function() {
reply(function(config) {
var error = new Error('timeout of ' + config.timeout + 'ms exceeded');
error.config = config;
error.code = 'ECONNABORTED';
return Promise.reject(error);
});
},

timeoutOnce: function() {
replyOnce(function(config) {
var error = new Error('timeout of ' + config.timeout + 'ms exceeded');
error.config = config;
error.code = 'ECONNABORTED';
return Promise.reject(error);
});
}
};
};
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type ResponseSpecFunc = (statusOrCallback: number | CallbackResponseSpecFunc, da
interface RequestHandler {
reply: ResponseSpecFunc;
replyOnce: ResponseSpecFunc;
timeoutOnce: ResponseSpecFunc;
networkErrorOnce: ResponseSpecFunc;

passThrough(): void;
networkError(): void;
Expand Down

0 comments on commit 4a0e874

Please # to comment.