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 more error reporting information to the Web Client callback responses #296

Merged
merged 7 commits into from Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/clients/transports/call-transport.js
Expand Up @@ -85,24 +85,37 @@ var handleHttpErr = function handleHttpErr(retryOp, apiCb, statusCode) {
* @param apiCb
* @returns {boolean}
*/
var handleHttpResponse = function handleHttpResponse(body, client, apiCb) {
var handleHttpResponse = function handleHttpResponse(body, headers, client, apiCb) {
var jsonResponse;
var jsonParseErr;
var jsonError;

try {
jsonResponse = JSON.parse(body);
} catch (parseErr) {
// TODO(leah): Emit an event here?
jsonParseErr = new Error('unable to parse Slack API Response');
jsonError = new Error('unable to parse Slack API Response');
return false;
}

if (!jsonParseErr && jsonResponse.warning) {
if (!jsonError && jsonResponse.warning) {
client.logger('warn', jsonResponse.warning);
}

if (!jsonResponse.ok) {
jsonError = new Error(jsonResponse.error);
client.logger('error', jsonResponse.error);
}

jsonResponse.scopes = (headers['x-oauth-scopes'] === undefined) ?
[] :
headers['x-oauth-scopes'].trim().split(/\s*,\s*/);
jsonResponse.acceptedScopes = (headers['x-accepted-oauth-scopes'] === undefined) ?
[] :
headers['x-accepted-oauth-scopes'].trim().split(/\s*,\s*/);


try {
apiCb(jsonParseErr, jsonResponse);
apiCb(jsonError, jsonResponse);
} catch (callbackErr) {
// Never retry requests that fail in the callback
client.logger('error', callbackErr);
Expand Down Expand Up @@ -139,7 +152,7 @@ var handleTransportResponse = function handleTransportResponse(
callQueueCb = handleHttpErr(retryArgs.retryOp, retryArgs.task.cb, statusCode);
}
} else {
callQueueCb = handleHttpResponse(body, retryArgs.client, retryArgs.task.cb);
callQueueCb = handleHttpResponse(body, headers, retryArgs.client, retryArgs.task.cb);
}

// This is always an empty callback, even if there's an error, as it's used to signal the
Expand Down
29 changes: 27 additions & 2 deletions test/clients/web/client.js
Expand Up @@ -7,6 +7,8 @@ var sinon = require('sinon');

var WebAPIClient = require('../../../lib/clients/web/client');
var retryPolicies = require('../../../lib/clients/retry-policies');
var defaultHTTPResponseHandler =
require('../../../lib/clients/transports/call-transport').handleHttpResponse;


var mockTransport = function (args, cb) {
Expand Down Expand Up @@ -51,7 +53,7 @@ describe('Web API Client', function () {
expect(client.userAgent).to.equal('test');
});

it('should register facets during construction', function () {
it('should register facets during construction', function () {
var client = new WebAPIClient('test-token', { transport: lodash.noop });
expect(client.auth).to.not.equal(undefined);
});
Expand All @@ -66,7 +68,7 @@ describe('Web API Client', function () {
var client = new WebAPIClient('test-token', { transport: mockTransport });

client._makeAPICall('test', args, null, function (err, res) {
expect(res).to.deep.equal({ test: 10 });
expect(res.test).to.equal(10);
done();
});
});
Expand Down Expand Up @@ -124,3 +126,26 @@ describe('Web API Client', function () {
});

});

describe('Default transport', function () {
it('should report scope information when present', function (done) {
// See https://api.slack.com/docs/oauth-scopes#working_with_scopes
var headers = {
'x-oauth-scopes': 'foo, bar,baz ,qux',
'x-accepted-oauth-scopes': 'a, i,u ,e'
};
var body = '{"test": 10}';
var client = {
logger: function () {}
};

defaultHTTPResponseHandler(body, headers, client, function (err, res) {
expect(res).to.deep.equal({
test: 10,
scopes: ['foo', 'bar', 'baz', 'qux'],
acceptedScopes: ['a', 'i', 'u', 'e']
});
done();
});
});
});