Skip to content

Commit

Permalink
feat: add fallback logic in error handling (#1309)
Browse files Browse the repository at this point in the history
* feat: add fallback logic in error handling

* chore: fix typo

* feat: restore error details removal

Co-authored-by: Alexander Fenster <fenster@google.com>
  • Loading branch information
sckimynwa and alexander-fenster committed Aug 17, 2022
1 parent 9b73ddd commit 6b12234
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/googleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ export class GoogleError extends Error {
return 'error' in obj;
});
}

// fallback logic.
// related issue: https://github.com/googleapis/gax-nodejs/issues/1303
// google error mapping: https://cloud.google.com/apis/design/errors
// if input json doesn't have 'error' fields, wrap the whole object with 'error' field
if (!json['error']) {
json['error'] = {};

Object.keys(json)
.filter(key => key !== 'error')
.forEach(key => {
json['error'][key] = json[key];
delete json[key];
});
}

const decoder = new GoogleErrorDecoder();
const proto3Error = decoder.decodeHTTPError(json['error']);
const error = Object.assign(
Expand All @@ -83,7 +99,7 @@ export class GoogleError extends Error {
delete error.code;
}
// Keep consistency with gRPC statusDetails fields. gRPC details has been occupied before.
// Rename "detials" to "statusDetails".
// Rename "details" to "statusDetails".
if (error.details) {
try {
const statusDetailsObj: GRPCStatusDetailsObject =
Expand Down

0 comments on commit 6b12234

Please sign in to comment.