Skip to content

DownloadStreamAsync and server error #2057

Closed

Description

@nssoft-rlagrange
          Ahh, looking at the code I think your suggestion before might work here. I think we can use the OnAfterRequest callback for the request to be able to get the response message so we can do something useful with that (basically save it off to inspect later?) when the download action completes?

If that is accurate we can close this out.

Originally posted by @kendallb in #1823 (comment)

Hi, I'm having a similar issue : the server respond an error (406 in my case), but I don't have any indication that the download failed, and I save the body as the expected file I'm downloading (but got an error message in file).
What where you thinking to circumvent the issue ?
At the very least, I want an exception if the server doesn't return a 20X.
Regards,

Activity

  1. alexeyzimarev commented on Apr 7, 2023

    @alexeyzimarev
    Member

    Have you tried setting ThrowOnAnyError to true? I looked at the Download code and it will throw in your case.

  2. nssoft-rlagrange commented on Apr 7, 2023

    @nssoft-rlagrange
    Author

    I don't have this property on 108.
    I found a workaround by settings the AfterRequest delegate :

    Exception sharedException = null;
    request.OnAfterRequest = (response) => // capture exception
    {
    	try
    	{
    		response.EnsureSuccessStatusCode();
    	}
    	catch(Exception e) { sharedException = e; }
    	return new ValueTask(Task.FromResult(response.Content));
    	
    };
    using (var str = await client.DownloadStreamAsync(request, token))
    {
    	if (sharedException != null) throw sharedException; // fire captured exception
    	await str.CopyToAsync(fs);
    }
    
  3. alexeyzimarev commented on Apr 7, 2023

    @alexeyzimarev
    Member

    ThrowOnAnyError is the client option that exists since v107. Before that it was the client property.

    https://restsharp.dev/error-handling.html

  4. nssoft-rlagrange commented on Apr 7, 2023

    @nssoft-rlagrange
    Author

    Ok i found the property. It doesn't work, at least in my 108 version.
    No exception thrown, at the very least the stream is empty, so I could check the length.
    My workaround work fine.

  5. alexeyzimarev commented on Apr 7, 2023

    @alexeyzimarev
    Member

    It was fixed by this PR #1962. I am not sure what version was release after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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