Skip to content

Commit dab1585

Browse files
authored
Refactor client error handling (#479)
1 parent 1275462 commit dab1585

22 files changed

+805
-288
lines changed

src/Grpc.Net.Client/GrpcChannel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public sealed class GrpcChannel : ChannelBase, IDisposable
4545
internal int? SendMaxMessageSize { get; }
4646
internal int? ReceiveMaxMessageSize { get; }
4747
internal ILoggerFactory LoggerFactory { get; }
48+
internal bool ThrowOperationCanceledExceptionOnCancellation { get; }
4849
internal bool? IsSecure { get; }
4950
internal List<CallCredentials>? CallCredentials { get; }
5051
internal Dictionary<string, ICompressionProvider> CompressionProviders { get; }
@@ -71,6 +72,7 @@ internal GrpcChannel(Uri address, GrpcChannelOptions channelOptions) : base(addr
7172
CompressionProviders = ResolveCompressionProviders(channelOptions.CompressionProviders);
7273
MessageAcceptEncoding = GrpcProtocolHelpers.GetMessageAcceptEncoding(CompressionProviders);
7374
LoggerFactory = channelOptions.LoggerFactory ?? NullLoggerFactory.Instance;
75+
ThrowOperationCanceledExceptionOnCancellation = channelOptions.ThrowOperationCanceledExceptionOnCancellation;
7476

7577
if (channelOptions.Credentials != null)
7678
{

src/Grpc.Net.Client/GrpcChannelOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#endregion
1818

19+
using System;
1920
using System.Collections.Generic;
2021
using System.Net.Http;
2122
using Grpc.Core;
@@ -75,6 +76,14 @@ public sealed class GrpcChannelOptions
7576
/// </remarks>
7677
public bool DisposeHttpClient { get; set; }
7778

79+
/// <summary>
80+
/// Gets or sets a value indicating whether clients will throw <see cref="OperationCanceledException"/> for a call when its
81+
/// <see cref="CallOptions.CancellationToken"/> is triggered or its <see cref="CallOptions.Deadline"/> is exceeded.
82+
/// The default value is <c>false</c>.
83+
/// Note: experimental API that can change or be removed without any prior notice.
84+
/// </summary>
85+
public bool ThrowOperationCanceledExceptionOnCancellation { get; set; }
86+
7887
/// <summary>
7988
/// Initializes a new instance of the <see cref="GrpcChannelOptions"/> class.
8089
/// </summary>

src/Grpc.Net.Client/Internal/GrpcCall.Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static class Log
3333
LoggerMessage.Define(LogLevel.Trace, new EventId(2, "ResponseHeadersReceived"), "Response headers received.");
3434

3535
private static readonly Action<ILogger, StatusCode, string, Exception?> _grpcStatusError =
36-
LoggerMessage.Define<StatusCode, string>(LogLevel.Error, new EventId(3, "GrpcStatusError"), "Server returned gRPC error status. Status code: '{StatusCode}', Message: '{StatusMessage}'.");
36+
LoggerMessage.Define<StatusCode, string>(LogLevel.Error, new EventId(3, "GrpcStatusError"), "Call failed with gRPC error status. Status code: '{StatusCode}', Message: '{StatusMessage}'.");
3737

3838
private static readonly Action<ILogger, Exception?> _finishedCall =
3939
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "FinishedCall"), "Finished gRPC call.");

0 commit comments

Comments
 (0)