Skip to content

Commit

Permalink
feat: improve error response messages
Browse files Browse the repository at this point in the history
Signed-off-by: Esteban Laver <10552966+emlaver@users.noreply.github.com>
  • Loading branch information
emlaver committed Nov 18, 2024
1 parent 9c2da9f commit 0810574
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 2.12.0 (unreleased)
- [FIXED] Error messages from retried requests.
- [IMPROVED] Updated error response messages.
- [NOTE] Removed `retry-axios` from peerDependencies.

# 2.11.0 (2024-09-24)
Expand Down
26 changes: 10 additions & 16 deletions includes/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017, 2021 IBM Corp. All rights reserved.
// Copyright © 2017, 2024 IBM Corp. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,30 +37,24 @@ function errorHelper(err) {
}
debug('Applying response error message with status, url, and method');
// Override the status text with an improved message
let errorMsg = `${err.response.status} ${err.response.statusText || ''}: ` +
`${method} ${requestUrl}`;
let errorMsg = `${err.response.status} ${method} ${requestUrl}`;
if (err.response.data) {
debug('Found response data');
// Check if we have a JSON response and try to get the error/reason
if (err.response.headers['content-type'] === 'application/json') {
debug('Response data is JSON');
// Append the error/reason if available
if (err.response.data.error) {
debug('Augmenting error message with error property');
// Override the status text with our more complete message
errorMsg += ` - Error: ${err.response.data.error}`;
if (err.response.data.reason) {
debug('Augmenting error message with reason property');
errorMsg += `, Reason: ${err.response.data.reason}`;
}
// Append the 'errors' message if available
if (err.response.data.errors && err.response.data.errors.length > 0) {
const originalError = err.response.data.errors[0];
originalError.message = `${errorMsg} - Error: ${originalError.message}`;
}
} else {
errorMsg += err.response.data;
// Set a new message for use by the node-sdk-core
// We use the errors array because it gets processed
// ahead of all other service errors.
err.response.data.errors = [{ message: errorMsg }];
}
// Set a new message for use by the node-sdk-core
// We use the errors array because it gets processed
// ahead of all other service errors.
err.response.data.errors = [{ message: errorMsg }];
}
} else if (err.request) {
debug('Error did not include a response');
Expand Down
8 changes: 4 additions & 4 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('#unit Check request response error callback', function() {
(err) => {
err = convertError(err);
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `500 Internal Server Error: get ${url}/bad - Error: foo, Reason: bar`);
assert.strictEqual(err.message, `500 get ${url}/bad - Error: foo: bar`);
assert.ok(couch.isDone());
return true;
});
Expand All @@ -89,7 +89,7 @@ describe('#unit Check request response error callback', function() {
(err) => {
err = convertError(err);
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `503 Service Unavailable: post ${url}/_bulk_get - Error: service_unavailable, Reason: Service unavailable`);
assert.strictEqual(err.message, `503 post ${url}/_bulk_get - Error: service_unavailable: Service unavailable`);
assert.ok(couch.isDone());
return true;
});
Expand All @@ -109,7 +109,7 @@ describe('#unit Check request response error callback', function() {
(err) => {
err = convertError(err);
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `429 Too Many Requests: get ${url}/bad - Error: foo, Reason: bar`);
assert.strictEqual(err.message, `429 get ${url}/bad - Error: foo: bar`);
assert.ok(couch.isDone());
return true;
});
Expand All @@ -128,7 +128,7 @@ describe('#unit Check request response error callback', function() {
(err) => {
err = convertError(err);
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `404 Not Found: get ${url}/bad - Error: foo, Reason: bar`);
assert.strictEqual(err.message, `404 get ${url}/bad - Error: foo: bar`);
assert.ok(couch.isDone());
return true;
});
Expand Down
2 changes: 1 addition & 1 deletion test/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('#unit Check database restore', function() {
getRestorePipeline(),
(err) => {
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `503 : post ${dbUrl}/_bulk_docs - Error: Service Unavailable`);
assert.strictEqual(err.message, `503 post ${dbUrl}/_bulk_docs - Error: Service Unavailable`);
assert.ok(nock.isDone());
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion test/spoolchanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Check spool changes', function() {
// in the underlying SDK call, follower will not retry
return changes(500, 0).catch((err) => {
assert.strictEqual(err.name, 'HTTPFatalError');
assert.strictEqual(err.message, `500 Internal Server Error: post ${url}/${dbName}/_changes - Error: foo, Reason: bar`);
assert.strictEqual(err.message, `500 post ${url}/${dbName}/_changes - Error: foo: bar`);
assert.ok(nock.isDone());
});
}).timeout(longTestTimeout);
Expand Down

0 comments on commit 0810574

Please sign in to comment.