2

是否可以用我自己的中间件类替换“官方”ExceptionMiddleware HotChocolate 类?

我计划通过包含 AgregationException .Net 异常来“完成”捕获,并通过循环 AgregationException.InnerExceptions 属性创建 IError[] 数组(参见下面的原始 ExceptionMiddleware)。

我想用我自己的实现来替换它。

可能吗 ?我怎样才能做到这一点 ?

谢谢。

亲切的问候

   internal sealed class ExceptionMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly IErrorHandler _errorHandler;

        public ExceptionMiddleware(RequestDelegate next, IErrorHandler errorHandler)
        {
            _next = next ?? throw new ArgumentNullException(nameof(next));
            _errorHandler = errorHandler ?? throw new ArgumentNullException(nameof(errorHandler));
        }

        public async ValueTask InvokeAsync(IRequestContext context)
        {
            try
            {
                await _next(context).ConfigureAwait(false);
            }
            catch (GraphQLException ex)
            {
                context.Exception = ex;
                context.Result = QueryResultBuilder.CreateError(_errorHandler.Handle(ex.Errors));
            }
            catch (Exception ex)
            {
                context.Exception = ex;
                IError error = _errorHandler.CreateUnexpectedError(ex).Build();
                context.Result = QueryResultBuilder.CreateError(_errorHandler.Handle(error));
            }
        }
    }
4

0 回答 0