This interceptor provides an easy way to handle the most common REST HTTP status code as exceptions.
try {
repository.makeRestRequest()
} catch (throwable: Throwable) {
when(throwable) {
BadRequest -> { } // Handle the bad request status code
InternalServerError -> { } // Handle the internal server error status code
Unauthorized -> { } // Handle the unauthorized status code
}
}
val errorInterceptor = RestErrorInterceptor()
val client = OkHttpClient.Builder()
.addInterceptor(errorInterceptor)
.build()
1- Add the Jitpack Repository in your root build.gradle file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2- Add the dependency in your project-level build.gradle file:
dependencies {
implementation 'com.github.jeancsanchez:okhttp-rest-error-interceptor:{latest version}'
}