upwork / phystrix

Phystrix is a latency and fault tolerance library for PHP, inspired by Netflix’s Hystrix
Apache License 2.0
1 stars 0 forks source link

How to avoid of re-throwing custom exceptions from triggering the circuit breaker #37

Closed omar-faruk-sust closed 5 years ago

omar-faruk-sust commented 5 years ago

Hello @lcf ,

I am facing an issue of using Phystrix in a project in a particular situation.

Let say, i have some custom (validation, some others) on my execution path. Without circuit breaker it's working good. I have implemented circuit breaker and those exception catch through circuit breaker and re-throwing them.

However what i have tried is, i was checking my custom exception(for example: ExceptionA) on fallback and translate this exception into another exception named ExeptionB and want to throw it (which i want to make as a behaviour in case of ExceptionA)

what i notice is that, it's actually re-throwing the ExceptionA again instead of ExceptionB which i don't want.

so, is there any suggestion in this particular situation how to protect my custom exceptions and/or translate my exception to another and throw my new exception?

lcf commented 5 years ago

Hi @omar-faruk-sust , if it's an exception that means "invalid user input" you can use BadRequestException https://github.com/upwork/phystrix/blob/master/library/Odesk/Phystrix/Exception/BadRequestException.php#L22

If you extend your exception from this class it will re-thrown as is. It will not be counted in metrics as a server error and it will not trigger the circuit breaker logic.

omar-faruk-sust commented 5 years ago

hi @lcf ,

If the exceptions are not BadRequestException, how i can skip them of re throwing them? I saw that BadRequestException is re-throwing as it is. I need to skip re-throwing my custom exceptions. Particularly my exception is falling into failed retrieving fallback (EVENT_FALLBACK_FAILURE) error criteria according to your implementation. Any suggestion how i can translate this error into my custom exception?

lcf commented 5 years ago

@omar-faruk-sust you can just extend your custom exceptions from BadRequestException like this:

class MyCustomException extends BadRequestException