Skip to content

Commit 30a9682

Browse files
committed
first steps towards simplified exception handling (server-side)
1 parent bb1961b commit 30a9682

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/protobuf-net.Grpc/Internal/Reshape.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System;
33
using System.Collections.Generic;
44
using System.ComponentModel;
5+
using System.IO;
56
using System.Runtime.CompilerServices;
7+
using System.Security;
68
using System.Threading;
79
using System.Threading.Tasks;
810
namespace ProtoBuf.Grpc.Internal
@@ -136,6 +138,68 @@ public static async Task WriteTo<T>(this IAsyncEnumerable<T> reader, IServerStre
136138
}
137139
}
138140

141+
/// <summary>
142+
/// Consumes the provided task raising exceptions as <see cref="RpcException"/>
143+
/// </summary>
144+
[Obsolete(WarningMessage, false)]
145+
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
146+
public static Task WithSimpleExceptionHandling(Task task)
147+
{
148+
return task.RanToCompletion() ? Task.CompletedTask : Awaited(task);
149+
150+
static async Task Awaited(Task task)
151+
{
152+
try
153+
{
154+
await task.ConfigureAwait(false);
155+
}
156+
catch (Exception ex) when (!(ex is RpcException))
157+
{
158+
Rethrow(ex);
159+
}
160+
}
161+
}
162+
163+
/// <summary>
164+
/// Consumes the provided task raising exceptions as <see cref="RpcException"/>
165+
/// </summary>
166+
[Obsolete(WarningMessage, false)]
167+
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
168+
public static Task<T> WithSimpleExceptionHandling<T>(Task<T> task)
169+
{
170+
return task.RanToCompletion() ? task : Awaited(task);
171+
172+
static async Task<T> Awaited(Task<T> task)
173+
{
174+
try
175+
{
176+
return await task.ConfigureAwait(false);
177+
}
178+
catch (Exception ex) when (!(ex is RpcException))
179+
{
180+
Rethrow(ex);
181+
return default!; // never reached
182+
}
183+
}
184+
}
185+
186+
private static void Rethrow(Exception ex)
187+
{
188+
var code = ex switch
189+
{
190+
OperationCanceledException => StatusCode.Cancelled,
191+
ArgumentException => StatusCode.InvalidArgument,
192+
NotImplementedException => StatusCode.Unimplemented,
193+
SecurityException => StatusCode.PermissionDenied,
194+
EndOfStreamException => StatusCode.OutOfRange,
195+
FileNotFoundException => StatusCode.NotFound,
196+
DirectoryNotFoundException => StatusCode.NotFound,
197+
TimeoutException => StatusCode.DeadlineExceeded,
198+
_ => StatusCode.Unknown,
199+
};
200+
throw new RpcException(new Status(code, ex.Message), ex.Message);
201+
}
202+
139203
/// <summary>
140204
/// Performs a gRPC blocking unary call
141205
/// </summary>

tests/protobuf-net.Grpc.Test.IntegrationUpLevel/protobuf-net.Grpc.Test.IntegrationUpLevel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
14-
<PackageReference Include="protobuf-net" Version="3.0.1" />
14+
<PackageReference Include="protobuf-net" Version="3.0.2" />
1515
<PackageReference Include="System.Collections.Immutable" Version="1.7.1" />
1616
<PackageReference Include="xunit" Version="2.4.1" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">

0 commit comments

Comments
 (0)