Skip to content

recursive loop when trying to parse the response body exception middleware #864

@riezebosch

Description

@riezebosch

await FlurlClient.HandleExceptionAsync(_call, new FlurlParsingException(_call, "JSON", ex), CancellationToken.None).ConfigureAwait(false);

I try to parse the response body to throw a meaningful exception from middleware, but when using the GetJsonAsync<T> fails it will incur another OnError being called ultimately causing a stackoverflow crashing your application.

So, are there other means to parse the response body for specific http responses? Should you avoid using the GetJsonAsync in the exception middleware? I now have a workaround by setting and checking the ExceptionHandled flag to break out of recursion but that feels a bit like a hack.

smallest repro:

using var http =  new HttpTest();
http.RespondWith("some error", 400);

var act = () => "http://x".OnError(async call =>{await call.Response.GetJsonAsync<string[]>();}).GetJsonAsync();

await act.Should().ThrowAsync<FlurlHttpException>();

result:

Stack Overflow Exception

Last runner error: Stack overflow exception occurred in test. Test run is aborted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions