Skip to content

Commit

Permalink
feat(errors): add error.body with original JSON error body (#4421)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed May 17, 2023
1 parent ce3b067 commit 49f357b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/next-release/feature-errors-02c46955.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "errors",
"description": "add error.body hidden field with original error body for JSON protocols"
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/*
test/browser/**/*
test/configuration.js
workspace
19 changes: 19 additions & 0 deletions lib/protocol/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function extractError(resp) {
if (httpResponse.body.length > 0) {
try {
var e = JSON.parse(httpResponse.body.toString());

var code = e.__type || e.code || e.Code;
if (code) {
error.code = code.split('#').pop();
Expand All @@ -49,6 +50,24 @@ function extractError(resp) {
} else {
error.message = (e.message || e.Message || null);
}

// The minimized models do not have error shapes, so
// without expanding the model size, it's not possible
// to validate the response shape (members) or
// check if any are sensitive to logging.

// Assign the fields as non-enumerable, allowing specific access only.
for (var key in e || {}) {
if (key === 'code' || key === 'message') {
continue;
}
error['[' + key + ']'] = 'See error.' + key + ' for details.';
Object.defineProperty(error, key, {
value: e[key],
enumerable: false,
writable: true
});
}
} catch (e) {
error.statusCode = httpResponse.statusCode;
error.message = httpResponse.statusMessage;
Expand Down
20 changes: 19 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,25 @@ var util = {
err.name = String(options && options.name || err.name || err.code || 'Error');
err.time = new Date();

if (originalError) err.originalError = originalError;
if (originalError) {
err.originalError = originalError;
}


for (var key in options || {}) {
if (key[0] === '[' && key[key.length - 1] === ']') {
key = key.slice(1, -1);
if (key === 'code' || key === 'message') {
continue;
}
err['[' + key + ']'] = 'See error.' + key + ' for details.';
Object.defineProperty(err, key, {
value: err[key] || (options && options[key]) || (originalError && originalError[key]),
enumerable: false,
writable: true
});
}
}

return err;
},
Expand Down

0 comments on commit 49f357b

Please sign in to comment.