Skip to content

Commit

Permalink
Merge pull request stripe#33 from gflandre/fix-errors
Browse files Browse the repository at this point in the history
Requests return Javascript Errors
  • Loading branch information
abh committed Jan 29, 2013
2 parents 03edc8e + b6a22a5 commit c3fb202
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Access to the [Stripe](https://stripe.com/) [API](https://stripe.com/docs/api).
{ email: '[email protected]' },
function(err, customer) {
if (err) {
console.log("Couldn't create the customer record");
console.log(err.message);
return;
}
console.log("customer id", customer.id);
Expand All @@ -28,7 +28,7 @@ Access to the [Stripe](https://stripe.com/) [API](https://stripe.com/docs/api).
## API

All methods takes a callback as their last parameter. The callback is
called with an error code (if any) and then the response.
called with a Javascript `Error` (if any) and then the response.

* `stripe.charges` - create, retrieve, refund and list charges
* `.create(charge)` - [create a charge](https://stripe.com/docs/api#create_charge)
Expand Down
12 changes: 8 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ function setup_response_handler(req, callback) {
});
res.on('end',
function() {
var err = 200 == res.statusCode ? null : res.statusCode;
var err = null;
try {
response = JSON.parse(response);
if(200 != res.statusCode) {
err = new Error(response.error.message);
err.name = response.error.type;
response = null;
}
}
catch(e) {
err = 1;
response = { error : { message : "Invalid JSON from stripe.com" } };
err = new Error("Invalid JSON from stripe.com");
response = null;
}
err && (err = { statusCode: err, response: response });
callback(err, response);
});
});
Expand Down
26 changes: 26 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var vows = require('vows'),
assert = require('assert'),
sys = require('sys');

var api_key = process.env.STRIPE_API;

if (!api_key) {
sys.puts('To run vows, you must have a STRIPE_API environment variable with a test api key');
process.exit(2)
}

var stripe = require('./../lib/main.js')(api_key);

vows.describe("Error handling").addBatch({
'Retrieve non-existing plan' : {
topic: function() {
stripe.plans.retrieve("unknown_plan_" + Date.now(), this.callback);
},
'returns an error' : function(err, response) {
assert.isNull(response);
assert.instanceOf(err, Error);
assert.equal(err.name, 'invalid_request_error');
assert.isNotNull(err.message);
},
},
}).export(module, {error: false});

0 comments on commit c3fb202

Please sign in to comment.