Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProblemType.prototype.reject() method #1

Merged
merged 2 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ Create a new instance of this problem type and return it. The
`options` argument is the same as the `options` for the
`Problem` constructor.

### Method: `<void> Problem.Type.prototype.reject([options])`

Create a new instance of this problem type and create a rejected
Promise with the Error. The `options` argument is the same as the
`options` for the `Problem` constructor.

### Constants:

* `<Problem.Type> Problem.BLANK` - The `about:blank` Problem Type
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ ProblemType.prototype = {
},
throw : function(options) {
throw this.raise(options);
},
reject : function(options) {
return Promise.reject(this.raise(options));
}
};
Object.defineProperty(ProblemType.prototype, 'status', statusDef);
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-problem",
"version": "0.2.0",
"version": "0.3.0",
"description": "HTTP Problem",
"main": "index.js",
"scripts": {
Expand All @@ -17,5 +17,7 @@
"mocha": "^2.3.2",
"request": "^2.62.0"
},
"dependencies": {}
"dependencies": {
"http-status": "^0.2.3"
}
}
34 changes: 30 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,39 @@ describe('Problem', function() {
'accounts': ['http://example.net/account/12345',
'http://example.net/account/67890']
};
let problem = Problem.wrap(obj);
assert.throws(function() {
problem.throw();
});
var thrown = false;
try {
Problem.GONE.throw();
} catch(error) {
thrown = true;
assert.equal(error.status, 410);
}
assert(thrown);
done();
});

it('should reject', function(done) {
let obj = {
'type': 'http://example.com/probs/out-of-credit',
'title': 'You do not have enough credit.',
'detail': 'Your current balance is 30, but that costs 50.',
'instance': 'http://example.net/account/12345/msgs/abc',
'balance': 30,
'accounts': ['http://example.net/account/12345',
'http://example.net/account/67890']
};
Problem.GONE.reject().then(
function onSuccess() {
assert.fail();
done();
},
function onError(error) {
assert(error);
assert.equal(error.status, 410);
done();
}
);
});
});

describe('Problem.middleware', function() {
Expand Down