private static TBroker AddServicesExceptionHandling <TBroker>(this TBroker broker, ILogger logger, string applicationName) where TBroker : IApiBroker { var handlingApiRequestConfiguration = Handling.Prepare <string, Exception>() .On <TimeoutException>((ex, i) => { logger.LogError(0, ex, "HTTP REST Outbound Request -> TimeoutException"); return(Handling.Handled(new GenericException("Não foi possível comunicar com o Saúda. Demasiado tempo a aguardar resposta", ex))); }) .On <HttpRequestException>((ex, i) => { logger.LogError(0, ex, "HTTP REST Outbound Request -> System.Net.Http.HttpRequestException"); return(Handling.Handled(new GenericException("Não foi possível comunicar com o Saúda.", ex))); }); var handlingApiResponseConfiguration = Handling.Prepare <string, Exception>() .On <UnprocessableEntityApiResponseException>((ex) => { logger.LogError(0, ex, "HTTP REST Outbound Response -> Converted into UnprocessableEntityApiResponseException"); var errors = ex.Error?.Error?.ModelState != null ? ex.Error.Error.ModelState.Select( error => new ValidationExceptionError(error.Key, error.Value)) : Enumerable.Empty <ValidationExceptionError>(); return(Handling.Handled(new Common.Exceptions.ValidationException(ex, errors))); }) .On <BadRequestApiResponseException>((ex) => { logger.LogError(0, ex, "HTTP REST Outbound Response -> Converted into BadRequestApiResponseException"); if (ex.Error?.Error?.ModelState == null) { return(Handling.Handled(new BusinessException(ex.Message, ex))); } if (ex.Error.Error.ModelState.Count == 1 && ex.Error.Error.ModelState.First().Key == string.Empty) { //Número de cartão recebido não é válido [ErrorCode: 2270] return(Handling.Handled( new BusinessException(string.Join(",", ex.Error.Error.ModelState.First().Value[0]), ex))); } return(Handling.Handled(new BusinessException.WithKeyCollection(ex.Message, ex, ex.Error.Error.ModelState .Select(o => new ExceptionWithKeyCollectionItem(!string.IsNullOrEmpty(o.Key) ? o.Key : ex.Message, string.Join(",", o.Value)))) )); }) .On <ConflictApiResponseException>((ex, i) => { logger.LogWarning(0, ex, "HTTP REST Outbound Response -> Converted into ConflictApiResponseException"); if (ex.Error?.Error?.ModelState == null) { return(Handling.Handled(new BusinessException(ex.Message, ex))); } if (ex.Error.Error.ModelState.Count == 0 && !string.IsNullOrEmpty(ex.Error.Error.Message)) { //O movimento não pode ser anulado pois foi feito em contexto familiar diferente return(Handling.Handled(new BusinessException(ex.Error.Error.Message, ex))); } return(Handling.Handled(new BusinessException.WithKeyCollection(ex.Message, ex, ex.Error.Error.ModelState .Select(o => new ExceptionWithKeyCollectionItem(!string.IsNullOrEmpty(o.Key) ? o.Key : ex.Message, string.Join(",", o.Value)))) )); }) .On <InternalServerErrorApiResponseException>((ex, i) => { logger.LogError(0, ex, "HTTP REST Outbound Response -> Converted into GenericException"); return(Handling.Handled(new GenericException( "Ocorreu um erro na comunicação com o servidor. Por favor tente novamente.", ex))); }); return(broker.OnRequestException((e, r, ct) => { var result = handlingApiRequestConfiguration.Catch(e, applicationName, false); if (result.Handled) { throw result.Result; } logger.LogError(0, e, "HTTP REST Outbound Response -> Converted into GenericException"); throw new GenericException( $"Erro a comunicar com o servidor {applicationName}. Contacte o administrador do sistema.", e); }) .OnApiResponseException((e, ct) => { var result = handlingApiResponseConfiguration.Catch(e, applicationName, false); if (result.Handled) { throw result.Result; } logger.LogError(0, e, "HTTP REST Outbound Response -> Converted into GenericException"); throw new GenericException( $"O servidor {applicationName} devolveu erro. Contacte o administrador do sistema.", e); })); }
private static TBroker AddRequestExceptionHandling <TBroker>(this TBroker broker, ILogger logger) where TBroker : IApiBroker { var handlingConfiguration = Handling.Prepare <string, Exception>() .On <UnprocessableEntityApiResponseException>((ex) => { logger.LogWarning(0, ex, "HTTP REST Outbound Response -> Converted into UnprocessableEntityApiResponseException"); var errors = ex.Error?.Error?.ModelState != null ? ex.Error.Error.ModelState.Select( error => new ValidationExceptionError(error.Key, error.Value)) : Enumerable.Empty <ValidationExceptionError>(); return(Handling.Handled(new Common.Exceptions.ValidationException(ex, errors))); }) .On <BadRequestApiResponseException>((ex) => { logger.LogWarning(0, ex, "HTTP REST Outbound Response -> Converted into BadRequestApiResponseException"); if (ex.Error?.Error?.ModelState == null) { return(Handling.Handled(new BusinessException(ex.Message, ex))); } if (ex.Error.Error.ModelState.Count == 1 && ex.Error.Error.ModelState.First().Key == string.Empty) { //Número de cartão recebido não é válido [ErrorCode: 2270] return(Handling.Handled( new BusinessException(string.Join(",", ex.Error.Error.ModelState.First().Value[0]), ex))); } return(Handling.Handled(new BusinessException.WithKeyCollection(ex.Message, ex, ex.Error.Error.ModelState .Select(o => new ExceptionWithKeyCollectionItem(!string.IsNullOrEmpty(o.Key) ? o.Key : ex.Message, string.Join(",", o.Value)))) )); }).On <ConflictApiResponseException>((ex, i) => { logger.LogWarning(0, ex, "HTTP REST Outbound Response -> Converted into ConflictApiResponseException"); if (ex.Error?.Error?.ModelState == null) { return(Handling.Handled(new BusinessException(ex.Message, ex))); } if (ex.Error.Error.ModelState.Count == 0 && !string.IsNullOrEmpty(ex.Error.Error.Message)) { //O movimento não pode ser anulado pois foi feito em contexto familiar diferente return(Handling.Handled(new BusinessException(ex.Error.Error.Message, ex))); } return(Handling.Handled(new BusinessException.WithKeyCollection(ex.Message, ex, ex.Error.Error.ModelState .Select(o => new ExceptionWithKeyCollectionItem(!string.IsNullOrEmpty(o.Key) ? o.Key : ex.Message, string.Join(",", o.Value)))) )); }) .On <InternalServerErrorApiResponseException>((ex, i) => { logger.LogWarning(0, ex, "HTTP REST Outbound Response -> Converted into GenericException"); return(Handling.Handled(new GenericException( "Ocorreu um erro na comunicação com o servidor. Por favor tente novamente.", ex))); }); return(broker.OnRequestException((e, r, ct) => { logger.LogWarning(0, e, "HTTP REST Outbound Request -> Failed"); return Task.CompletedTask; }) .OnApiResponseException((e, ct) => { var result = handlingConfiguration.Catch(e, throwIfNotHandled: false); if (result.Handled) { throw result.Result; } logger.LogWarning(0, e, "HTTP REST Outbound Response -> Converted into ApiResponseException"); return Task.CompletedTask; })); }