Skip to content

Commit

Permalink
Merge pull request #45 from kaijuliu/handle-browse-node-lookup-errors
Browse files Browse the repository at this point in the history
Adds error handling to browse node lookups, similar to those for items.
  • Loading branch information
t3chnoboy committed Mar 18, 2016
2 parents 7a9796c + 8ac2b79 commit 9b21ef3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ var runQuery = function (credentials, method) {
respObj.Items[0]
);
}
} else if (respObj.BrowseNodes && respObj.BrowseNodes.length > 0 && respObj.BrowseNodes[0].BrowseNode) {
cb(
null,
respObj.BrowseNodes[0].BrowseNode,
respObj.BrowseNodes[0]
);
} else if (respObj.BrowseNodes && respObj.BrowseNodes.length > 0) {
// Request Error
if (respObj.BrowseNodes[0].Request && respObj.BrowseNodes[0].Request.length > 0 && respObj.BrowseNodes[0].Request[0].Errors) {
cb(respObj.BrowseNodes[0].Request[0].Errors);
} else if (respObj.BrowseNodes[0].BrowseNode) {
cb(
null,
respObj.BrowseNodes[0].BrowseNode,
respObj.BrowseNodes[0]
);
}
}
}
});
Expand Down Expand Up @@ -83,8 +88,13 @@ var runQuery = function (credentials, method) {
} else if (respObj.Items[0].Item) {
resolve(respObj.Items[0].Item);
}
} else if (respObj.BrowseNodes && respObj.BrowseNodes.length > 0 && respObj.BrowseNodes[0].BrowseNode) {
} else if (respObj.BrowseNodes && respObj.BrowseNodes.length > 0) {
// Request Error
if (respObj.BrowseNodes[0].Request && respObj.BrowseNodes[0].Request.length > 0 && respObj.BrowseNodes[0].Request[0].Errors) {
reject(respObj.BrowseNodes[0].Request[0].Errors);
} else if (respObj.BrowseNodes[0].BrowseNode) {
resolve(respObj.BrowseNodes[0].BrowseNode);
}
}
}
});
Expand Down
21 changes: 21 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,24 @@ describe 'client.browseNodeLookup(query, cb)', ->
err.should.be.an.Object
err.should.have.property 'Error'
done()

describe 'when the request returns an error', ->
client = amazonProductApi.createClient credentials

describe 'when no callback is passed', ->
it 'should return the errors inside the request node', ->
client.browseNodeLookup
browseNodeId: '102340',
responseGroup: 'NewReleases'
.catch (err) =>
err.should.be.an.Array
err[0].should.be.an.Object
err[0].should.have.property 'Error'

describe 'when callback is passed', ->
it 'should return the errors inside the request node', ->
client.browseNodeLookup {browseNodeId: '102340', responseGroup: 'NewReleases'}, (err, results) ->
err.should.be.an.Array
err[0].should.be.an.Object
err[0].should.have.property 'Error'
done()

0 comments on commit 9b21ef3

Please sign in to comment.