Пример #1
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             RequestHandlerDelegate <TResponse> next)
        {
            var fluentValidationResult = await _compositeValidator.ValidateAsync(request, cancellationToken);

            if (!fluentValidationResult.IsValid)
            {
                var result = new TResponse();
                var error  = ResultExtensions.Get400BadRequestError();
                foreach (var reason in fluentValidationResult.Errors)
                {
                    error.Reasons.Add(new Error(reason.ErrorMessage));
                }

                result.Reasons.Add(error);
                return(result);
            }

            // Encapsulate all handlers with this try/catch block as not to do this in every handler itself.
            try
            {
                return(await next());
            }
            catch (Exception e)
            {
                return(Result.Fail(new ExceptionalError(e)).LogError() as TResponse);
            }
        }