Skip to content

Add withErrorCancellable #110

@xperiandri

Description

@xperiandri
/// 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions