Skip to content

Commit 71e0ca6

Browse files
committed
Fixes #169 - corrected error handling
1 parent bb24339 commit 71e0ca6

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lib/client.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ export default class Client {
6060
return new Bluebird((resolve, reject) => {
6161
const resolver = (err, data) => {
6262
if (err) {
63-
reject(new Error(JSON.stringify(err)));
63+
reject(err);
6464
} else {
6565
resolve(data);
6666
}
6767
};
68-
this.request(args, (_, r) => {
69-
callbackHandler(resolver, r);
68+
this.request(args, (err, r) => {
69+
callbackHandler(resolver, err, r);
7070
});
7171
});
7272
} else {
73-
this.request(args, (_, r) => this.callback(f, r));
73+
this.request(args, (err, r) => this.callback(f, err, r));
7474
}
7575
}
7676
ping(f) {
@@ -138,19 +138,29 @@ export default class Client {
138138
callback
139139
).auth(this.usernamePart, this.passwordPart);
140140
}
141-
callback(f, data) {
141+
callback(f, err, res) {
142142
if (!f) {
143143
return;
144144
}
145145
if (f.length >= 2) {
146-
const hasErrors = data.error || (data.body && data.body.type === 'error.list');
147-
if (hasErrors) {
148-
f(data, null);
146+
if (res && res.body && res.body.type === 'error.list') {
147+
let message = null;
148+
if (Array.isArray(res.body.errors) && res.body.errors[0] && 'message' in res.body.errors) {
149+
// Try to use the first errors message
150+
message = res.body.errors[0].message;
151+
}
152+
err = new Error(message || 'Response error');
153+
err.statusCode = res.statusCode;
154+
err.body = res.body;
155+
err.headers = res.headers;
156+
}
157+
if (err) {
158+
f(err, null);
149159
} else {
150-
f(null, data);
160+
f(null, res);
151161
}
152162
} else {
153-
f(data);
163+
f(res || null);
154164
}
155165
}
156166
}

0 commit comments

Comments
 (0)