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

DownloadStreamAsync and server error #2057

Closed
nssoft-rlagrange opened this issue Apr 6, 2023 · 5 comments
Closed

DownloadStreamAsync and server error #2057

nssoft-rlagrange opened this issue Apr 6, 2023 · 5 comments

Comments

@nssoft-rlagrange
Copy link

          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,

@alexeyzimarev
Copy link
Member

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

@nssoft-rlagrange
Copy link
Author

nssoft-rlagrange commented Apr 7, 2023

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);
}

@alexeyzimarev
Copy link
Member

alexeyzimarev commented Apr 7, 2023

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

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

@nssoft-rlagrange
Copy link
Author

nssoft-rlagrange commented Apr 7, 2023

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.

@alexeyzimarev
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants