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

How to detect server response status code #52

Closed
raV720 opened this issue Aug 11, 2023 · 20 comments
Closed

How to detect server response status code #52

raV720 opened this issue Aug 11, 2023 · 20 comments
Labels
question Further information is requested

Comments

@raV720
Copy link

raV720 commented Aug 11, 2023

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
Copy link
Owner

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

@bluetianx bluetianx added the question Further information is requested label Aug 11, 2023
@raV720
Copy link
Author

raV720 commented Aug 16, 2023

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

@raV720 raV720 closed this as completed Aug 16, 2023
@raV720
Copy link
Author

raV720 commented Aug 28, 2023

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

@raV720 raV720 reopened this Aug 28, 2023
@bluetianx
Copy link
Owner

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

@raV720
Copy link
Author

raV720 commented Aug 28, 2023

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.

@raV720
Copy link
Author

raV720 commented Aug 28, 2023

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
Copy link
Owner

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

@raV720
Copy link
Author

raV720 commented Aug 28, 2023

"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
Copy link
Owner

Which method did throw an exception?

@raV720
Copy link
Author

raV720 commented Aug 28, 2023

response.EnsureSuccessStatusCode();

@bluetianx
Copy link
Owner

截屏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
Copy link
Author

raV720 commented Aug 29, 2023

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:
tusdotnet/tusdotnet#204

@bluetianx
Copy link
Owner

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
Copy link
Author

raV720 commented Aug 29, 2023

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
Copy link
Owner

Okay, I see. I will think about that.

@bluetianx
Copy link
Owner

bluetianx commented Aug 29, 2023

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

@raV720
Copy link
Author

raV720 commented Aug 29, 2023

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

@bluetianx
Copy link
Owner

bluetianx commented Aug 29, 2023

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
Copy link
Owner

@raV720

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

I have been released v3.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants