Error code from Throwable - Android

Vineesh TP picture Vineesh TP · Jun 28, 2017 · Viewed 11.2k times · Source

How can I get the error code from the Throwable

public void onFailure(Throwable exception) {

   }

I saw that we can get the error messages, LocalizedMessage etc

Answer

nhp picture nhp · Jun 28, 2017

Only HttpException gives you http error code. Make sure you check instance of before using it.

Here is the code:

if (throwable instanceof HttpException) {
        HttpException exception = (HttpException) throwable;
        switch (exception.code()) {
            case 400:
                // Handle code 400
                break;
            case 500:
                // Handle code 500
                break;
            default:
                break;
        }
    }