gRPC - Global exception handling with interceptor in gRPC service

要做 gRPC 的全域攔截,可以使用 gRPC interceptor 來做。


建立一個 Interceptor 類別繼承自 gRPC 的 Interceptor,覆寫掉會用到的方法,像是 UnaryServerHandler,然後在內部調用基底的方法並用 try/catch 去攔截錯誤做對應的處理即可。


像是下面這邊筆者實作了一個 Interceptor,希望能在錯誤發生時顯示錯誤,並回傳錯誤訊息。

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
using System;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.Logging;


namespace GrpcServiceInterceptor
{
public class ExceptionInterceptor : Interceptor
{
public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request,
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
try
{
return await base.UnaryServerHandler(request, context, continuation);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());


var obj = (TResponse) Activator.CreateInstance(typeof(TResponse));
var prop = typeof(TResponse).GetProperty("Message");

prop.SetValue(obj, e.Message, null);


return obj;
}
}
}
}


Interceptor 寫好後要掛載進去。

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpc(options =>
{
options.Interceptors.Add<ExceptionInterceptor>();
});
}

}


運行起來實際去調用 gRPC,當錯誤發生時會可看到 gRPC 仍舊正常回應,只是回應的內容是 Interceptor 攔截錯誤後回傳出去的。


攔截到的錯誤資訊也會正常的顯示。


透過 gRPC interceptor 我們可將錯誤集中處理,可以紀錄發生的錯誤,也可以轉成自訂的錯誤回應格式。