bluetianx / BirdMessenger

DotNET client implementation of the Tus protocol for resumable file uploads.
https://tus.io/
MIT License
62 stars 12 forks source link

How to detect server response status code #52

Closed raV720 closed 1 year ago

raV720 commented 1 year ago

I would like to detect when server responses with 409 or 404 or other status code and act accordingly. Is it possible to do it with your library? As I can see your library throws HttpRequestException but it does not allow to get status code.

bluetianx commented 1 year ago

Is it possible to do it with your library?

For sure

demo code :

TusPatchRequestOption tusPatchRequestOption = new TusPatchRequestOption
            {
                // others codes

                // If it failed Upload, client will invoke OnFailedAsync method 
                OnFailedAsync = x =>
                {
                    Console.WriteLine($"error: {x.Exception.Message}");
                    if (x.OriginHttpRequestMessage is not null)
                    {
                        //log httpRequest
                    }

                    if (x.OriginResponseMessage is not null)
                    {
                        //log response
                    }
                    return Task.CompletedTask;
                }
            };

            var tusPatchResp = await tusClient.TusPatchAsync(tusPatchRequestOption, CancellationToken.None);
raV720 commented 1 year ago

I don't know how i missed it. Thank you

raV720 commented 1 year ago

The response is not available when there is an error (not success status code) on the TusHeadAsync, TusOptionAsync and TusDeleteAsync

bluetianx commented 1 year ago

I suggest that you get that exception with try catch code, and then you can know why there is an error.

raV720 commented 1 year ago

The exception has very limited information about what happened and this information is included in Message string. Parsing message is not a good way to detect for example status code.

bluetianx commented 1 year ago

You can detect status code with https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestexception.statuscode?view=net-7.0#system-net-http-httprequestexception-statuscode

raV720 commented 1 year ago

Netstandard 2.0 does not have such property. And still it is very limited if available. IMO the response should be accessible in some way.

bluetianx commented 1 year ago

Can you show me an exception, I may resolve your problem

raV720 commented 1 year ago

"InnerException": { "HResult": -2146233088, "Message": "Response status code does not indicate success: 409 (Conflict).", "Source": "System.Net.Http", "Type": "System.Net.Http.HttpRequestException" },

at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at BirdMessenger.HttpClientExtension.d__4.MoveNext()

bluetianx commented 1 year ago

Which method did throw an exception?

raV720 commented 1 year ago

https://github.com/bluetianx/BirdMessenger/blob/0c92afab65fe33ab41a743050d687968637218ad/src/BirdMessenger/HttpClientExtension.cs#L139

bluetianx commented 1 year ago
截屏2023-08-28 22 49 05

It is so weird, I think HeadMethod never gets an 409 exception, You can refer to https://tus.io/protocols/resumable-upload#concatenation

raV720 commented 1 year ago

The 409 status is only an example of response which is not possible to handle. Other statuses are also not possible to verify.

According to the 409 Conflict it is known issue with Kestrel selfhost and tusdotnet implementation: https://github.com/tusdotnet/tusdotnet/issues/204

bluetianx commented 1 year ago

OK, I understand. If you do not know how to handle an exceptional status, I can handle it for you. If you want to log which exceptional status with codes, I suggest you run your code on net core runtime.

By the way, I want to know why do want to detect the server response status code, Because I think logging exceptional Messages is enough

raV720 commented 1 year ago

The status code is required to act accordingly when it is 409 or 404 or 401 or other status (maybe 5**). And whether to retry sending file or stop retrying if error is permanent.

bluetianx commented 1 year ago

Okay, I see. I will think about that.

bluetianx commented 1 year ago

I suggest retrying invoking method as long as there is an exception and you set Max retry times

raV720 commented 1 year ago

Some exceptions (status codes) are permanent and it is pointless to repeat the request. Anyway thank you for help and time.

bluetianx commented 1 year ago

Some exceptions (status codes) are permanent and it is pointless to repeat the request.

Yep, I totally agree. You can avoid that by setting Max retry times at present.

Anyway thank you for help and time.

You are welcome, I will handle your problem by throwing a custom exception which contains any information you want

bluetianx commented 11 months ago

@raV720

I will handle your problem by throwing a custom exception which contains any information you want

I have been released v3.1.2