RestClient: Exception with status code

Hi, I’ve been using the package for a week now, it’s pretty good actually but there’s one thing that bothers me, Exception does not have a status code and/or the description from the server. I’ve been looking into your code and found that you return the error from UnityWebRequest which does not have neither the status code or a description from the server, in case my server return a 400 (BadRequest) with a description why it has failed. Both of those thing can be gathered pretty ease since you are creating a RequestException and have a RequestHelper in your disposal, which has both of those things. To make it a little more clear why it’s a good “feature” I’ll leave a snippet below.

RestClient.Get($"{URL}/test")
            .Then(response => {
                //Do something
                return RestClient.Post($"{URL}/token", model);
            }).Then(response => {
                //Here I can can use the status code and/or the "descrition" from server
                switch (response.statusCode) {
                    case 200:
                        //Success
                        break;
                    case 401:
                        //Unauthorized
                        break;
                    default:
                        //All the others
                        break;
                }
            }).Finally(() => {
                //Do something
            }).Catch(Debug.LogException);

If something goes wrong here (return RestClient.Post($"{URL}/token", model);) the second Then() won’t be called, and that’s fine, but catching the exception does not help me 'cause it does not say what really happened there

*EDIT

After posting this I realized that even this change won’t be enough for some cases. Using my own scenario, when opening the app, the user should log in, he/she might type the wrong email/password, if that is the case, the server will return a 401 (Unauthorized) (or you may want a 400 (BadRequest)), none of those are really an error, at least not a error to be “catchched” (in the Catch()), i would like to have them in the Then(), my case 401: (in the switch) will never be called, since server returned errors are treated as generic errors.

The EDIT part are just some thoughts, I’m not trying to suggest anything, is just, I don’t know, my case is “special”, (is it?)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Hello @jdnichollsc, I’ve updated my comment there, I just don’t know how to update the “on previous version 1.2.2” and since I’m already here I would like to suggest one more thing, but that time is pretty simple, as you might have noticed, I don’t like JSONUtility and the .BodyString solved this problem, I would like to ask you to add a section to the README with others JSON Serializers, and how it would be used/implemented (I can send you my wrapper), that way will be easier to newcomers.

I can recommend two libs that work nicely with Unity:

But that is really up to you. 😃

Hi @jdnichollsc I’ve tested it with my server and it works fine, now I can have more control over each request and do what I need.

One thing that you may have not realized is that the demo does not work anymore, 'cause you renamed some variables, quickly you’ll fix it, I’m saying just to let you know.

Thanks man! 😃

@caneva20 please validate the changes before generating a new release https://github.com/proyecto26/RestClient/commit/e287f942568f0457376f14293fe543e7d0382229

Thanks in advance 👍

@jdnichollsc I should thank you for the attention.

Thanks!

@caneva20 sounds great, let me publish a new version of the plugin this weekend Thanks for your help!

Humm, I think that’s is pretty great actually, and it will be fine for everyone.

Hi @jdnichollsc , I’ve just forked the project and I’m changing things here and there to make it work for me.

I haven’t thought about adding more things to RequestException, but I agree that this is a great solution, and probably the best the way, what I’ve done now was just a quick fix, (I’m appending the code and desc the to error description, hehe).

One other change that I’ve made to this class was here https://github.com/proyecto26/RestClient/blob/master/src/Proyecto26.RestClient/HttpActions/HttpBase.cs#L17

Instead of if (request.isDone && string.IsNullOrEmpty(request.error)) I’m using if (request.isDone && !request.isNetworkError)

That way no changes need to be made to RequestException and only if there’s a NetworkError an error will be returned.

BUT this may not desired by everyone, I don’t know.

What you think?