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 handler) : Interceptor { public override AsyncUnaryCall AsyncUnaryCall( TRequest request, ClientInterceptorContext context, AsyncUnaryCallContinuation continuation) { var call = continuation(request, context); return new AsyncUnaryCall( HandleUnaryResponse(call), call.ResponseHeadersAsync, call.GetStatus, call.GetTrailers, call.Dispose); } public override AsyncClientStreamingCall AsyncClientStreamingCall( ClientInterceptorContext context, AsyncClientStreamingCallContinuation continuation) { var call = continuation(context); return new AsyncClientStreamingCall( call.RequestStream, HandleStreamResponse(call), call.ResponseHeadersAsync, call.GetStatus, call.GetTrailers, call.Dispose); } private async Task HandleUnaryResponse(AsyncUnaryCall call) { try { return await call; } catch (Exception ex) { handler(ex); throw; } } private async Task HandleStreamResponse(AsyncClientStreamingCall call) { try { return await call; } catch (Exception ex) { handler(ex); throw; } } }