Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception not re-thrown #24

Closed
CrHasher opened this issue Sep 4, 2020 · 1 comment
Closed

Exception not re-thrown #24

CrHasher opened this issue Sep 4, 2020 · 1 comment

Comments

@CrHasher
Copy link

CrHasher commented Sep 4, 2020

In the following code the inner method InvokeAsync is throwing an OnlineException but I can never catch it what is re-thrown is a simple Exception:

//-- in a singleton
CircuitBreakerPolicy = Policy
    .Handle<Exception>()
    .CircuitBreakerAsync(
        exceptionsAllowedBeforeBreaking: ExceptionsAllowedForCircuiteBreaker,
        durationOfBreak: TimeSpan.FromSeconds(BreakTime),
        onBreak: (ex, breakDelay) =>
        {
            Debug.Log($".Breaker logging: Breaking the circuit for {breakDelay.TotalMilliseconds} ms!");
            Debug.Log($"..due to: + {ex.Message}");
        },
        onReset: () => Debug.Log(".Breaker logging: Call ok! Closed the circuit again!"),
        onHalfOpen: () => Debug.Log(".Breaker logging: Half-open: Next call is a trial!")
    );
//--

public virtual async Task<IOnlineResponse> Process(CancellationToken ct)
{
    Debug.Log($"Processing = {Type}");

    State = new RequestState();
    U serviceResponse = null;

    AsyncTimeoutPolicy timeoutPolicy = Policy.TimeoutAsync(TimeOut);

    AsyncRetryPolicy waitAndRetryPolicy = Policy
            .Handle<Exception>(e => !(e is BrokenCircuitException))
            .WaitAndRetryAsync(
            retryCount: DefaultRetryCount,
            attempt => TimeSpan.FromMilliseconds(AttemptDelay),
            (exception, calculatedWaitDuration) =>
            {
                Debug.Log($".Request exception (will retry): " + exception.Message);
            });

    AsyncPolicyWrap policyWrap = Policy.WrapAsync(timeoutPolicy, waitAndRetryPolicy, CircuitBreaker.Instance.CircuitBreakerPolicy);

    try
    {
        serviceResponse = await policyWrap.ExecuteAsync(async (cancellationToken) =>
        {
            await CheckConnectivity(cancellationToken);
            return await InvokeAsync(cancellationToken);
        }, ct, continueOnCapturedContext: true);

        State.Status = RequestStatus.Success;
        State.Message = "";
    }
    catch (OnlineException ex)
    {
        State.Status = OnlineExceptionStatusToRequestStatus(ex.StatusCode);
        State.Message = ex.ToString();
    }
    catch (Exception ex)
    {
        State.Status = RequestStatus.Failed;
        State.Message = ex.ToString();
    }

    Debug.Log($"Response = {Type} - {State.Status}");

    return new V { State = State, Response = serviceResponse };
}
@CrHasher
Copy link
Author

CrHasher commented Sep 4, 2020

Never mind its a simple fix I added the custom exception to both CB and Retry policies (hopefully I'm doing it right):

CircuitBreakerPolicy = Policy
    .Handle<OnlineException>()
    .Or<Exception>()

AsyncRetryPolicy waitAndRetryPolicy = Policy
    .Handle<OnlineException>()
    .Or<Exception>(e => !(e is BrokenCircuitException))

@martincostello martincostello closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants