Support user-returned error responses using statusCode.
Fixes #40.
diff --git a/lib/client.js b/lib/client.js
index 6f467bd..e1060c8 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -110,8 +110,13 @@
if (typeof error.error === 'string') {
return error.error
- } else if (error.response && error.response.result && typeof error.response.result.error === 'string') {
- return error.response.result.error
+ } else if (error.response && error.response.result) {
+ const result = error.response.result
+ if (typeof result.error === 'string') {
+ return result.error
+ } else if (typeof result.statusCode === 'number') {
+ return `application error, status code: ${result.statusCode}`
+ }
}
return 'Missing error message.'
diff --git a/test/unit/client.test.js b/test/unit/client.test.js
index 3a79265..16bed22 100644
--- a/test/unit/client.test.js
+++ b/test/unit/client.test.js
@@ -109,8 +109,10 @@
t.throws(() => client.handle_errors({statusCode: 409}), /Conflict/)
t.throws(() => client.handle_errors({statusCode: 500, error: {}}), /API call failed/)
t.throws(() => client.handle_errors({statusCode: 500, error: {response: {result: {error: 'custom'}}}}), /custom/)
+ t.throws(() => client.handle_errors({statusCode: 500, error: {response: {result: {statusCode: 404}}}}), /404/)
t.throws(() => client.handle_errors({statusCode: 502, error: {}}), /Action invocation failed/)
- t.throws(() => client.handle_errors({statusCode: 500, error: {response: {result: {error: 'custom'}}}}), /custom/)
+ t.throws(() => client.handle_errors({statusCode: 502, error: {response: {result: {error: 'custom'}}}}), /custom/)
+ t.throws(() => client.handle_errors({statusCode: 502, error: {response: {result: {statusCode: 404}}}}), /404/)
})
test('should throw errors for non-HTTP response failures', t => {