Using RoboSpice is there a way to get the HTTP Error Code out of an exception?

前端 未结 5 919
失恋的感觉
失恋的感觉 2021-01-31 11:29

I am writing an application that uses RoboSpice. In the request listener onRequestFailure( SpiceException arg0 ) is there a way to know for sure that the error was a result of a

5条回答
  •  无人共我
    2021-01-31 12:27

    It's even easier than all of the other answers.

    For me @Scrotos answer was problematic, because the whole point for the 401 to be caught is to make the request to auth endpoint and then make the primary request again. So that you only return to UI with desired data or some "real" error. So it shouldn't be done inside the callback, but rather inside loadDataFromNetwork() itself.

    I've done it this way:

    @Override
    public SubscriptionsContainer loadDataFromNetwork() {
    
        //...
    
        ResponseEntity response = null;
        try {
            response = getRestTemplate().exchange(
            //your request data                    
            );
        } catch (HttpClientErrorException e) {
            HttpStatus statusCode = e.getStatusCode();
            //check the exception, if it's 401, make call to auth and repeat loadDataFromNetwork()
        }
    

提交回复
热议问题