Rename project, namespaces and class names Signed-off-by: Pavel Gross <p.gross@yadro.com>
68 lines
2 KiB
C#
68 lines
2 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
using Grpc.Core;
|
|
using Grpc.Core.Interceptors;
|
|
|
|
namespace FrostFS.SDK.Client;
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods",
|
|
Justification = "parameters are provided by GRPC infrastructure")]
|
|
public class ErrorInterceptor(Action<Exception> handler) : Interceptor
|
|
{
|
|
public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
|
|
TRequest request,
|
|
ClientInterceptorContext<TRequest, TResponse> context,
|
|
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
|
|
{
|
|
var call = continuation(request, context);
|
|
|
|
return new AsyncUnaryCall<TResponse>(
|
|
HandleUnaryResponse(call),
|
|
call.ResponseHeadersAsync,
|
|
call.GetStatus,
|
|
call.GetTrailers,
|
|
call.Dispose);
|
|
}
|
|
|
|
public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(
|
|
ClientInterceptorContext<TRequest, TResponse> context,
|
|
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
|
|
{
|
|
var call = continuation(context);
|
|
|
|
return new AsyncClientStreamingCall<TRequest, TResponse>(
|
|
call.RequestStream,
|
|
HandleStreamResponse(call),
|
|
call.ResponseHeadersAsync,
|
|
call.GetStatus,
|
|
call.GetTrailers,
|
|
call.Dispose);
|
|
}
|
|
|
|
private async Task<TResponse> HandleUnaryResponse<TResponse>(AsyncUnaryCall<TResponse> call)
|
|
{
|
|
try
|
|
{
|
|
return await call;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
handler(ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private async Task<TResponse> HandleStreamResponse<TRequest, TResponse>(AsyncClientStreamingCall<TRequest, TResponse> call)
|
|
{
|
|
try
|
|
{
|
|
return await call;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
handler(ex);
|
|
throw;
|
|
}
|
|
}
|
|
}
|