Beispiel #1
0
        public Status GetStatus()
        {
            using (StartScope())
            {
                if (CallTask.IsCompletedSuccessfully())
                {
                    return(CallTask.Result);
                }

                throw new InvalidOperationException("Unable to get the status because the call is not complete.");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Resolve the specified exception to an end-user exception that will be thrown from the client.
        /// The resolved exception is normally a RpcException. Returns true when the resolved exception is changed.
        /// </summary>
        internal bool ResolveException(string summary, Exception ex, [NotNull] out Status?status, out Exception resolvedException)
        {
            if (ex is OperationCanceledException)
            {
                status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty);
                if (!Channel.ThrowOperationCanceledOnCancellation)
                {
                    resolvedException = CreateRpcException(status.Value);
                    return(true);
                }
            }
            else if (ex is RpcException rpcException)
            {
                status = rpcException.Status;

                // If trailers have been set, and the RpcException isn't using them, then
                // create new RpcException with trailers. Want to try and avoid this as
                // the exact stack location will be lost.
                //
                // Trailers could be set in another thread so copy to local variable.
                var trailers = Trailers;
                if (trailers != null && rpcException.Trailers != trailers)
                {
                    resolvedException = CreateRpcException(status.Value);
                    return(true);
                }
            }
            else
            {
                var s = GrpcProtocolHelpers.CreateStatusFromException(summary, ex);

                // The server could exceed the deadline and return a CANCELLED status before the
                // client's deadline timer is triggered. When CANCELLED is received check the
                // deadline against the clock and change status to DEADLINE_EXCEEDED if required.
                if (s.StatusCode == StatusCode.Cancelled)
                {
                    lock (this)
                    {
                        if (IsDeadlineExceededUnsynchronized())
                        {
                            s = new Status(StatusCode.DeadlineExceeded, s.Detail, s.DebugException);
                        }
                    }
                }

                status            = s;
                resolvedException = CreateRpcException(s);
                return(true);
            }

            resolvedException = ex;
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// Resolve the specified exception to an end-user exception that will be thrown from the client.
        /// The resolved exception is normally a RpcException. Returns true when the resolved exception is changed.
        /// </summary>
        internal bool ResolveException(string summary, Exception ex, [NotNull] out Status?status, out Exception resolvedException)
        {
            if (ex is OperationCanceledException)
            {
                status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty);
                if (!Channel.ThrowOperationCanceledOnCancellation)
                {
                    resolvedException = CreateRpcException(status.Value);
                    return(true);
                }
            }
            else if (ex is RpcException rpcException)
            {
                status = rpcException.Status;

                // If trailers have been set, and the RpcException isn't using them, then
                // create new RpcException with trailers. Want to try and avoid this as
                // the exact stack location will be lost.
                //
                // Trailers could be set in another thread so copy to local variable.
                var trailers = Trailers;
                if (trailers != null && rpcException.Trailers != trailers)
                {
                    resolvedException = CreateRpcException(status.Value);
                    return(true);
                }
            }
            else
            {
                var exceptionMessage = CommonGrpcProtocolHelpers.ConvertToRpcExceptionMessage(ex);
                var statusCode       = GrpcProtocolHelpers.ResolveRpcExceptionStatusCode(ex);

                status            = new Status(statusCode, summary + " " + exceptionMessage, ex);
                resolvedException = CreateRpcException(status.Value);
                return(true);
            }

            resolvedException = ex;
            return(false);
        }
Beispiel #4
0
        public Exception CreateCanceledStatusException()
        {
            var status = (CallTask.IsCompletedSuccessfully()) ? CallTask.Result : new Status(StatusCode.Cancelled, string.Empty);

            return(CreateRpcException(status));
        }