Bowery/orchestrate.js — pull request #19 by housejester (2014-03-03 05:56:57+00:00)
Title: Fix so failure callbacks will be called.

An issue came up in community chat (https://www.hipchat.com/g0WE90LD0) where failures were not being triggered. I may not have things quite right here, but it has something to do with how failures are propagated in Q. So, this PR makes it so that:

- Failure callbacks are triggered when expected (ie non 2xx range)
- Works with .then(callback, failure) form as well as .then().failure() form.
- Tries to parse the error json for errors (Orchestrate error responses do include a json info object).

The 'test cases' I was targeting:

```
// should call the failure callback
db.get("testing", "bad1").then(
function(response){
console.log("bad1: GET Success 1", response);
},
function(reason){
console.log("bad1: GET FAIL 1", reason.statusCode, reason.body);
}
);

// should work ok
db.put("testing", "good1", {"name":"good"}).then(
function(response){
console.log("good1: PUT", response.statusCode, response.body);
db.get("testing", "good1").then(
function(res2){
console.log("good1: GET", res2.body);
}
);
}
);

// should also call the failure callback
db.put("testing", "bad2", '{"name":"bad2"}').then(
function(response){
console.log("bad2: PUT SUCCESS", response.statusCode, response.body);
},
function(reason){
console.log("bad2: PUT FAIL 1", reason.statusCode, reason.body);
}
);

// Should still call the failure form
db.get("testing", "bad3").then(
function(response){
console.log("bad3: GET Success 1", response);
}
).fail(
function(reason){
console.log("bad3: GET FAIL 2", reason.statusCode, reason.body);
}
);
```

Pre-change output:
```
bad1: GET Success 1 undefined
good1: PUT 201 undefined
good1: GET { name: 'good' }
bad3: GET Success 1 undefined
```
Note the lack of output for bad2?

Output with the change:
```
bad1: GET FAIL 1 404 { message: 'The requested items could not be found.',
details: { items: [ [Object] ] },
code: 'items_not_found' }
good1: PUT 201 undefined
good1: GET { name: 'good' }
bad2: PUT FAIL 1 400 { message: 'The API request is malformed.',
details: { info: 'Content must be a valid JSON object.' },
code: 'api_bad_request' }
bad3: GET FAIL 2 404 { message: 'The requested items could not be found.',
details: { items: [ [Object] ] },
code: 'items_not_found' }
```