-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGrpcExceptionInterceptor.cs
More file actions
39 lines (36 loc) · 1.11 KB
/
GrpcExceptionInterceptor.cs
File metadata and controls
39 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace PathApi.Server
{
using System;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
using PathApi.Server.GrpcApi;
using Serilog;
/// <summary>
/// gRPC interceptor to log unexpected exceptions thrown by API implementations.
/// </summary>
internal sealed class GrpcExceptionInterceptor : Interceptor
{
private readonly Type type;
public GrpcExceptionInterceptor(IGrpcApi service)
{
this.type = service.GetType();
}
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
{
try
{
return await continuation.Invoke(request, context);
}
catch (RpcException)
{
throw;
}
catch (Exception ex)
{
Log.Logger.Here().Error(ex, $"Unexpected error in gRPC handler for {type.ToString()}.");
throw;
}
}
}
}