Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error type with RpcError & internal code sync #1139

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const request = new EchoRequest();
request.setMessage('Hello World!');

const call = echoService.echo(request, {'custom-header-1': 'value1'},
(err: grpcWeb.Error, response: EchoResponse) => {
(err: grpcWeb.RpcError, response: EchoResponse) => {
console.log(response.getMessage());
});
call.on('status', (status: grpcWeb.Status) => {
Expand Down
63 changes: 5 additions & 58 deletions javascript/net/grpc/web/abstractclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ goog.module.declareLegacyNamespace();


const ClientReadableStream = goog.require('grpc.web.ClientReadableStream');
const GrpcWebError = goog.require('grpc.web.Error');
const MethodDescriptor = goog.require('grpc.web.MethodDescriptor');
const MethodType = goog.require('grpc.web.MethodType');
const RpcError = goog.require('grpc.web.RpcError');


/**
Expand All @@ -46,10 +45,9 @@ const AbstractClientBase = class {
* @param {string} method The method to invoke
* @param {REQUEST} requestMessage The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!MethodDescriptor<REQUEST, RESPONSE>|
* !AbstractClientBase.MethodInfo<REQUEST, RESPONSE>}
* @param {!MethodDescriptor<REQUEST, RESPONSE>}
* methodDescriptor Information of this RPC method
* @param {function(?GrpcWebError, ?)}
* @param {function(?RpcError, ?)}
* callback A callback function which takes (error, RESPONSE or null)
* @return {!ClientReadableStream<RESPONSE>}
*/
Expand All @@ -62,8 +60,7 @@ const AbstractClientBase = class {
* @param {string} method The method to invoke
* @param {REQUEST} requestMessage The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!MethodDescriptor<REQUEST, RESPONSE>|
* !AbstractClientBase.MethodInfo<REQUEST,RESPONSE>}
* @param {!MethodDescriptor<REQUEST, RESPONSE>}
* methodDescriptor Information of this RPC method
* @return {!IThenable<RESPONSE>}
* A promise that resolves to the response message
Expand All @@ -76,36 +73,12 @@ const AbstractClientBase = class {
* @param {string} method The method to invoke
* @param {REQUEST} requestMessage The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!MethodDescriptor<REQUEST, RESPONSE>|
* !AbstractClientBase.MethodInfo<REQUEST,RESPONSE>}
* @param {!MethodDescriptor<REQUEST, RESPONSE>}
* methodDescriptor Information of this RPC method
* @return {!ClientReadableStream<RESPONSE>} The Client Readable Stream
*/
serverStreaming(method, requestMessage, metadata, methodDescriptor) {}

/**
* As MethodType is being deprecated, for now we need to convert MethodType to
* MethodDescriptor.
* @static
* @template REQUEST, RESPONSE
* @param {string} method
* @param {REQUEST} requestMessage
* @param {!MethodType} methodType
* @param {!AbstractClientBase.MethodInfo<REQUEST,RESPONSE>|!MethodDescriptor<REQUEST,RESPONSE>}
* methodInfo
* @return {!MethodDescriptor<REQUEST,RESPONSE>}
*/
static ensureMethodDescriptor(
method, requestMessage, methodType, methodInfo) {
if (methodInfo instanceof MethodDescriptor) {
return methodInfo;
}
const requestType = methodInfo.requestType || requestMessage.constructor;
return new MethodDescriptor(
method, methodType, requestType, methodInfo.responseType,
methodInfo.requestSerializeFn, methodInfo.responseDeserializeFn);
}

/**
* Get the hostname of the current request.
* @static
Expand All @@ -121,31 +94,5 @@ const AbstractClientBase = class {
};


/** @template REQUEST, RESPONSE */
AbstractClientBase.MethodInfo = class {
/**
* @param {function(new: RESPONSE, ...)} responseType
* @param {function(REQUEST): ?} requestSerializeFn
* @param {function(?): RESPONSE} responseDeserializeFn
* @param {string=} name
* @param {function(new: REQUEST, ...)=} requestType
*/
constructor(
responseType, requestSerializeFn, responseDeserializeFn, name,
requestType) {
/** @const */
this.name = name;
/** @const */
this.requestType = requestType;
/** @const */
this.responseType = responseType;
/** @const */
this.requestSerializeFn = requestSerializeFn;
/** @const */
this.responseDeserializeFn = responseDeserializeFn;
}
};



exports = AbstractClientBase;
25 changes: 13 additions & 12 deletions javascript/net/grpc/web/clientoptions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
goog.module('grpc.web.ClientOptions');
goog.module.declareLegacyNamespace();

const XmlHttpFactory = goog.requireType('goog.net.XmlHttpFactory');
const {StreamInterceptor, UnaryInterceptor} = goog.require('grpc.web.Interceptor');


Expand Down Expand Up @@ -47,18 +46,20 @@ class ClientOptions {
this.format;

/**
* The XmlHttpFactory for server-streaming calls.
* Example: use 'goog.net.FetchXmlHttpFactory' to reduce memory consumption
* during high throughput server-streaming calls.
* <pre>
* ...
*
* const xmlHttpFactory =
* new FetchXmlHttpFactory({streamBinaryChunks: true});
* </pre>
* @type {!XmlHttpFactory|undefined}
* The Worker global scope. Once this option is specified, gRPC-Web will
* also use 'fetch' API as the underlying transport instead of native
* XmlHttpRequest.
* @type {!WorkerGlobalScope|undefined}
*/
this.streamingXmlHttpFactory;
this.workerScope;

/**
* This is an experimental feature to reduce memory consumption
* during high throughput server-streaming calls by using
* 'streamBinaryChunks' mode FetchXmlHttpFactory.
* @type {boolean|undefined}
*/
this.useFetchDownloadStreams;
}
}

Expand Down
15 changes: 15 additions & 0 deletions javascript/net/grpc/web/clientreadablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ const ClientReadableStream = function() {};
/**
* Register a callback to handle different stream events.
*
* Available event types for gRPC-Web:
* 'data': The 'data' event is emitted when a new response message chunk is
* received and successfully handled by gRPC-Web client.
* 'status': the google RPC status of the response stream.
* 'end': The 'end' event is emitted when all the data have been successfully
* consumed from the stream.
* 'error': typically, this may occur when an underlying internal failure
* happens, or a stream implementation attempts to push an invalid chunk of
* data.
* 'metadata': the response metadata. Response headers should be read via
* 'metadata' callbacks.
*
* For server-streaming calls. the 'data' and 'status' callbacks (if exist)
* will always precede 'metadata', 'error', or 'end' callbacks.
*
* @param {string} eventType The event type
* @param {function(?)} callback The callback to handle the event with
* an optional input object
Expand Down
Loading