Skip to content

Commit b67d71b

Browse files
committed
feat: in googleError:decoreHTTPError handle unknown/custom errors.
1 parent 4098e20 commit b67d71b

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

gax/src/googleError.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {JSONValue} from 'proto3-json-serializer';
2323

2424
const protoTypePrefix = 'type.googleapis.com/';
2525
const resourceInfoType = 'type.googleapis.com/google.rpc.ResourceInfo';
26-
const defaultResourceTypeNameForUnknownTypes = "Uknown type";
26+
const defaultResourceTypeNameForUnknownTypes = 'Unknown type';
2727

2828
const numOfPartsInProtoTypeName = 2;
2929

@@ -215,33 +215,42 @@ const getErrorDetails = (protobuf: any, json: JSONValue): ErrorDetails => {
215215
return error_details;
216216
};
217217

218-
const makeResourceInfoError = (resourceType: string, description: string): JSONValue => {
218+
const makeResourceInfoError = (
219+
resourceType: string,
220+
description: string,
221+
): JSONValue => {
219222
return {
220223
'@type': resourceInfoType,
221224
resourceType,
222-
description
223-
}
224-
}
225+
description,
226+
};
227+
};
225228

226-
const convertUnknownDetailsToResourceInfoError = (unknownDetails: JSONValue[]) => {
229+
const convertUnknownDetailsToResourceInfoError = (
230+
unknownDetails: JSONValue[],
231+
) => {
227232
const unknownDetailsAsResourceInfoError: JSONValue[] = [];
228233
for (const unknownDetail of unknownDetails) {
229234
try {
230235
let resourceType: string = defaultResourceTypeNameForUnknownTypes;
231-
if (typeof unknownDetail === 'object' && unknownDetail !== null && '@type' in unknownDetail) {
236+
if (
237+
typeof unknownDetail === 'object' &&
238+
unknownDetail !== null &&
239+
'@type' in unknownDetail
240+
) {
232241
const unknownType: any = unknownDetail['@type'];
233242
resourceType = unknownType;
234243
}
235244
const description = JSON.stringify(unknownDetail);
236245
unknownDetailsAsResourceInfoError.push(
237-
makeResourceInfoError(resourceType, description)
246+
makeResourceInfoError(resourceType, description),
238247
);
239248
} catch (e) {
240249
// Failed convert to string, ignore it.
241250
}
242251
}
243252
return unknownDetailsAsResourceInfoError;
244-
}
253+
};
245254

246255
export class GoogleErrorDecoder {
247256
root: protobuf.Root;
@@ -354,8 +363,9 @@ export class GoogleErrorDecoder {
354363
details = errorDetails.knownDetails;
355364
}
356365
if (errorDetails.unknownDetails.length) {
357-
const unknowDetailsAsResourceInfo = convertUnknownDetailsToResourceInfoError(errorDetails.unknownDetails);
358-
details = [...details, ...unknowDetailsAsResourceInfo];
366+
const unknowDetailsAsResourceInfo =
367+
convertUnknownDetailsToResourceInfoError(errorDetails.unknownDetails);
368+
details = [...details, ...unknowDetailsAsResourceInfo];
359369
}
360370
if (details.length) {
361371
json.details = details;

gax/test/unit/googleError.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,9 @@ describe('http error decoding return resource info for unknown proto-error', ()
471471
error.statusDetails?.length,
472472
json['error']['details'].length,
473473
);
474-
assert.deepEqual(error.statusDetails[0],
475-
{
476-
resourceType: 'type.googleapis.com/Custom',
477-
description: JSON.stringify(custom),
478-
}
479-
)
474+
assert.deepEqual(error.statusDetails[0], {
475+
resourceType: 'type.googleapis.com/Custom',
476+
description: JSON.stringify(custom),
477+
});
480478
});
481479
});

0 commit comments

Comments
 (0)