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

Add withErrorCancellable #110

Open
xperiandri opened this issue Dec 28, 2022 · 0 comments
Open

Add withErrorCancellable #110

xperiandri opened this issue Dec 28, 2022 · 0 comments

Comments

@xperiandri
Copy link
Contributor

/// Error handler for decoding fetch responses into an user defined error type. Will ignore successful responses.
let withErrorCancellable
    (errorHandler: HttpResponse -> HttpContent -> CancellationToken -> Task<exn>)
    (source: HttpHandler<HttpContent>)
    : HttpHandler<HttpContent> =
    fun next ->
        { new IHttpNext<HttpContent> with
            member _.OnSuccessAsync(ctx, content) =
                task {
                    let response = ctx.Response

                    match response.IsSuccessStatusCode with
                    | true -> return! next.OnSuccessAsync(ctx, content)
                    | false ->
                        ctx.Request.Metrics.Counter Metric.FetchErrorInc ctx.Request.Labels 1L

                        let! err = errorHandler response content ctx.Request.CancellationToken
                        return! next.OnErrorAsync(ctx, HttpException(ctx, err))
                }

            member _.OnErrorAsync(ctx, exn) = next.OnErrorAsync(ctx, exn)
            member _.OnCancelAsync(ctx) = next.OnCancelAsync(ctx) }
        |> source
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

1 participant