google-gax
Advanced tools
Comparing version 2.20.0 to 2.21.0
@@ -35,2 +35,3 @@ { | ||
"options": { | ||
"cc_enable_arenas": true, | ||
"go_package": "google.golang.org/genproto/googleapis/rpc/errdetails;errdetails", | ||
@@ -104,2 +105,19 @@ "java_multiple_files": true, | ||
}, | ||
"ErrorInfo": { | ||
"fields": { | ||
"reason": { | ||
"type": "string", | ||
"id": 1 | ||
}, | ||
"domain": { | ||
"type": "string", | ||
"id": 2 | ||
}, | ||
"metadata": { | ||
"keyType": "string", | ||
"type": "string", | ||
"id": 3 | ||
} | ||
} | ||
}, | ||
"PreconditionFailure": { | ||
@@ -106,0 +124,0 @@ "fields": { |
@@ -87,2 +87,6 @@ "use strict"; | ||
if (err) { | ||
if (err.metadata) { | ||
const decoder = new googleError_1.GoogleErrorDecoder(); | ||
err.statusDetails = decoder.decodeRpcStatusDetails(err.metadata.get('grpc-status-details-bin')); | ||
} | ||
rejectCallback(err); | ||
@@ -89,0 +93,0 @@ } |
@@ -26,3 +26,3 @@ /** | ||
import { Descriptor } from './descriptor'; | ||
import { FallbackServiceError } from './fallbackError'; | ||
import { FallbackServiceError } from './googleError'; | ||
export { FallbackServiceError }; | ||
@@ -29,0 +29,0 @@ export { PathTemplate } from './pathTemplate'; |
@@ -49,3 +49,3 @@ "use strict"; | ||
const createApiCall_1 = require("./createApiCall"); | ||
const fallbackError_1 = require("./fallbackError"); | ||
const googleError_1 = require("./googleError"); | ||
const transcoding_1 = require("./transcoding"); | ||
@@ -208,3 +208,3 @@ var pathTemplate_1 = require("./pathTemplate"); | ||
// decoder for google.rpc.Status messages | ||
const statusDecoder = new fallbackError_1.FallbackErrorDecoder(); | ||
const statusDecoder = new googleError_1.GoogleErrorDecoder(); | ||
if (!this.authClient) { | ||
@@ -211,0 +211,0 @@ if (this.auth && 'getClient' in this.auth) { |
@@ -16,6 +16,33 @@ /** | ||
*/ | ||
/// <reference types="node" /> | ||
import { Status } from './status'; | ||
import * as protobuf from 'protobufjs'; | ||
import { Metadata } from './grpc'; | ||
export declare class GoogleError extends Error { | ||
code?: Status; | ||
note?: string; | ||
metadata?: Metadata; | ||
statusDetails?: string | protobuf.Message<{}>[]; | ||
} | ||
export declare type FallbackServiceError = FallbackStatusObject & Error; | ||
interface FallbackStatusObject { | ||
code: Status; | ||
message: string; | ||
details: Array<{}>; | ||
} | ||
interface ProtobufAny { | ||
type_url: string; | ||
value: Uint8Array; | ||
} | ||
export declare class GoogleErrorDecoder { | ||
root: protobuf.Root; | ||
anyType: protobuf.Type; | ||
statusType: protobuf.Type; | ||
constructor(); | ||
decodeProtobufAny(anyValue: ProtobufAny): protobuf.Message<{}>; | ||
decodeRpcStatus(buffer: Buffer | ArrayBuffer): FallbackStatusObject; | ||
callErrorFromStatus(status: FallbackStatusObject): FallbackServiceError; | ||
decodeErrorFromBuffer(buffer: Buffer | ArrayBuffer): Error; | ||
decodeRpcStatusDetails(bufferArr: Buffer[] | ArrayBuffer[]): protobuf.Message<{}>[]; | ||
} | ||
export {}; |
@@ -18,6 +18,65 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GoogleError = void 0; | ||
exports.GoogleErrorDecoder = exports.GoogleError = void 0; | ||
const status_1 = require("./status"); | ||
const protobuf = require("protobufjs"); | ||
class GoogleError extends Error { | ||
} | ||
exports.GoogleError = GoogleError; | ||
class GoogleErrorDecoder { | ||
constructor() { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const errorProtoJson = require('../../protos/status.json'); | ||
this.root = protobuf.Root.fromJSON(errorProtoJson); | ||
this.anyType = this.root.lookupType('google.protobuf.Any'); | ||
this.statusType = this.root.lookupType('google.rpc.Status'); | ||
} | ||
decodeProtobufAny(anyValue) { | ||
const match = anyValue.type_url.match(/^type.googleapis.com\/(.*)/); | ||
if (!match) { | ||
throw new Error(`Unknown type encoded in google.protobuf.any: ${anyValue.type_url}`); | ||
} | ||
const typeName = match[1]; | ||
const type = this.root.lookupType(typeName); | ||
if (!type) { | ||
throw new Error(`Cannot lookup type ${typeName}`); | ||
} | ||
return type.decode(anyValue.value); | ||
} | ||
// Decodes gRPC-fallback error which is an instance of google.rpc.Status. | ||
decodeRpcStatus(buffer) { | ||
const uint8array = new Uint8Array(buffer); | ||
const status = this.statusType.decode(uint8array); | ||
// google.rpc.Status contains an array of google.protobuf.Any | ||
// which need a special treatment | ||
const result = { | ||
code: status.code, | ||
message: status.message, | ||
details: status.details.map(detail => this.decodeProtobufAny(detail)), | ||
}; | ||
return result; | ||
} | ||
// Construct an Error from a StatusObject. | ||
// Adapted from https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/call.ts#L79 | ||
callErrorFromStatus(status) { | ||
status.message = `${status.code} ${status_1.Status[status.code]}: ${status.message}`; | ||
return Object.assign(new Error(status.message), status); | ||
} | ||
// Decodes gRPC-fallback error which is an instance of google.rpc.Status, | ||
// and puts it into the object similar to gRPC ServiceError object. | ||
decodeErrorFromBuffer(buffer) { | ||
return this.callErrorFromStatus(this.decodeRpcStatus(buffer)); | ||
} | ||
// Decodes gRPC-fallback error which is an instance of google.rpc.Status. | ||
decodeRpcStatusDetails(bufferArr) { | ||
const status = []; | ||
bufferArr.forEach(buffer => { | ||
const uint8array = new Uint8Array(buffer); | ||
const error_status = this.statusType.decode(uint8array); | ||
const status_details_array = error_status.details.map(detail => this.decodeProtobufAny(detail)); | ||
status.push(status_details_array[0]); | ||
}); | ||
return status; | ||
} | ||
} | ||
exports.GoogleErrorDecoder = GoogleErrorDecoder; | ||
//# sourceMappingURL=googleError.js.map |
@@ -7,2 +7,14 @@ # Changelog | ||
## [2.21.0](https://www.github.com/googleapis/gax-nodejs/compare/v2.20.0...v2.21.0) (2021-08-02) | ||
### Features | ||
* decode error details property from google.rpc.Status ([#1057](https://www.github.com/googleapis/gax-nodejs/issues/1057)) ([2dde63e](https://www.github.com/googleapis/gax-nodejs/commit/2dde63e87c11144e79e523df48756c60c119d404)) | ||
### Bug Fixes | ||
* clean up the lint ([#1067](https://www.github.com/googleapis/gax-nodejs/issues/1067)) ([d08e9d5](https://www.github.com/googleapis/gax-nodejs/commit/d08e9d5d67df89a4cecc73f8c00a76adc5e393a2)) | ||
## [2.20.0](https://www.github.com/googleapis/gax-nodejs/compare/v2.19.0...v2.20.0) (2021-07-30) | ||
@@ -9,0 +21,0 @@ |
{ | ||
"name": "google-gax", | ||
"version": "2.20.0", | ||
"version": "2.21.0", | ||
"description": "Google API Extensions", | ||
@@ -5,0 +5,0 @@ "main": "build/src/index.js", |
@@ -35,2 +35,3 @@ { | ||
"options": { | ||
"cc_enable_arenas": true, | ||
"go_package": "google.golang.org/genproto/googleapis/rpc/errdetails;errdetails", | ||
@@ -104,2 +105,19 @@ "java_multiple_files": true, | ||
}, | ||
"ErrorInfo": { | ||
"fields": { | ||
"reason": { | ||
"type": "string", | ||
"id": 1 | ||
}, | ||
"domain": { | ||
"type": "string", | ||
"id": 2 | ||
}, | ||
"metadata": { | ||
"keyType": "string", | ||
"type": "string", | ||
"id": 3 | ||
} | ||
} | ||
}, | ||
"PreconditionFailure": { | ||
@@ -106,0 +124,0 @@ "fields": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7519448
110200
371