Permalink
Browse files

Better error handling

  • Loading branch information...
1 parent 650f6ce commit 52721f5e843da5e41b2a2945c1501e804a6eadf6 @jerryjj jerryjj committed Apr 25, 2012
Showing with 24 additions and 3 deletions.
  1. +21 −1 lib/main.js
  2. +3 −2 test/queues.js
View
@@ -71,7 +71,27 @@ IronMQ._transport = {
_parseResponse: function(cb) {
return function parse(err, response, body) {
if (err) return cb(err);
- cb(null, JSON.parse(body));
+ if (response.statusCode == 200 || response.statusCode == 201) {
+ cb(null, JSON.parse(body));
+ } else {
+ var msg = null;
+ switch (response.statusCode) {
+ case 400:
+ msg = "Bad Request: Invalid JSON (can't be parsed or has wrong types).";
+ case 401:
+ msg = "Unauthorized: The OAuth token is either not provided or invalid.";
+ case 404:
+ msg = "Not Found: The resource, project, or endpoint being requested doesn’t exist.";
+ case 405:
+ msg = "Invalid HTTP method: A GET, POST, DELETE, or PUT was sent to an endpoint that doesn’t support that particular verb.";
+ case 406:
+ msg = "Not Acceptable: Required fields are missing.";
+ case 503:
+ msg = "Server currently unavailable!";
+ //TODO: Handle "exponential backoff"
+ }
+ return cb(msg, JSON.parse(body));
+ }
};
}
};
View
@@ -44,13 +44,14 @@ if (test_data.use_proxy) {
"size": 0
}
);
+
var req = nock('https://mq-aws-us-east-1.iron.io:443')
.matchHeader('authorization','OAuth ' + test_data.token)
.matchHeader('content-type','application/json')
.matchHeader('user-agent','IronMQ Node Client for Heroku')
.post('/1/projects/' + TEST_PROJECT + '/queues/' + TEST_QUEUE + '/clear')
.reply(
- 200,
+ 201,
{
"msg": "cleared"
}
@@ -76,7 +77,7 @@ if (test_data.use_proxy) {
{"messages": [{"body": "test message body"}]}
)
.reply(
- 200,
+ 201,
{
"ids": ["1234567890"],
"msg": "Messages put on queue."

0 comments on commit 52721f5

Please sign in to comment.