don't retry if validation exception
See original GitHub issueI am using Polly for .Net Core. I am trying to skip retrying when validation exception but still getting some unusual exception in other format and global exception handling is also not showing exception in correct format.
Domain.Validation.ValidationException: Validation Error(s)
at Infrastructure.NoSql.CosmosDb.Company.CommandHandlers.CreateCommandHandler.Handle(CreateCommand request, CancellationToken cancellationToken)
at Polly.AsyncPolicy.<>c__DisplayClass40_0.<<ImplementationAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Polly.Retry.AsyncRetryEngine.ImplementationAsync[TResult](Func`3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates`1 shouldRetryResultPredicates, Func`5 onRetryAsync, Int32 permittedRetryCount, IEnumerable`1 sleepDurationsEnumerable, Func`4 sleepDurationProvider, Boolean continueOnCapturedContext)
at Polly.AsyncPolicy.ExecuteAsync(Func`3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
at MediatR.Mediator.PublishCore(IEnumerable`1 allHandlers, INotification notification, CancellationToken cancellationToken)
at WebApi.Controllers.CompaniesController.AddAsync(CompanyDto item, CancellationToken cancellationToken) in C:\Projects\ReceiptVoucher\logify.Finance.ReceiptVoucherService\WebApi\Controllers\CompaniesController.cs:line 159
at lambda_method11(Closure , Object )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at WebApi.Extensions.ApiLogging.Invoke(HttpContext httpContext, IMediator mediator) in C:\Projects\ReceiptVoucher\logify.Finance.ReceiptVoucherService\WebApi\Extensions\ApiLogging.cs:line 34
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.InvokeCore(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VyTmFtZSI6IlNDMTAwNSIsIlVzZXJHcm91cCI6IjY5ZjJjZDQyLWI2YTQtNDE4Zi1hNGMyLWVjZTNkNzE5ZWQ2ZiIsIlVzZXJWaXNpYmlsaXR5IjoiOWU4Y2Y5NjAtZjk4ZC00OWZhLTk2YWYtNjBjMWY4OGI1OGY4IiwiU2hvd0xvY2FsTGFuZ3VhZ2UiOiIiLCJGdWxsTmFtZSI6IlByYXZpbiBZYWRhdiIsImV4cCI6MTYzODYyNzk3MywiaXNzIjoiaHR0cHM6Ly9hcGkuZGV2LnVzZXIubG9naS1meS5jb20vIiwiYXVkIjoiTG9naWZ5In0.PNh0kuBE2U6kbqg6DAC3fVlHdPnbaq1fXkCy0V-XFdU
Connection: keep-alive
Content-Length: 853
Content-Type: application/json
Host: localhost:44347
User-Agent: PostmanRuntime/7.28.4
Session: 11dd55a7-8297-49b1-ac93-b95fe31f0467
Postman-Token: cea5774a-931c-470f-bb5e-61837063da75
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How to retry after exception? - python
I have a loop starting with for i in range(0, 100) . Normally it runs correctly, but sometimes it fails due to network...
Read more >Validation - Throw Exception or Return False?
This tutorial discusses whether you should throw an exception or return false (or a similar error code) when a method receives invalid ...
Read more >c# - Why is it bad to use exceptions for handling this type ...
On principle, do not throw data validation exceptions. Throwing only after performing all validations is not an exceptional case. Nor does it " ......
Read more >Kafka Consumer Error Handling, Retry, and Recovery
The Retry and Recover go hand in hand, if the number of retries is exhausted, the recovery will test if the event exception...
Read more >Handling operation errors - Apollo GraphQL Docs
If a syntax error or validation error occurs, your server doesn't execute the operation at all because it's invalid. If resolver errors occur,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @martincostello, Thanks for your advice and sorry for wasting your time. You are right, issue was in ApiLogging Middleware, I was confused due to stack trace showing Polly.MoveNext().
Thanks & sorry once again !!
Do you have any observable proof that
ValidationException
is causing retries?You have not configured the policy to do so, and its presence in the stack trace does not imply it is performing retries, just that calls are being executed through the policy, which is by design.