Skip to content
Snippets Groups Projects
Unverified Commit dcf8831b authored by Kolya Opahle's avatar Kolya Opahle Committed by GitHub
Browse files

Added exception handler for UnknownHostException's (#412)


* Added exception handler for UnknownHostException's

Signed-off-by: default avatarKolya Opahle <k.opahle@sap.com>

* Added a custom CwaWebException conform UnknownHostException

Signed-off-by: default avatarKolya Opahle <k.opahle@sap.com>
parent f89c5fea
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ open class CwaSuccessResponseWithCodeMismatchNotSupportedError(val statusCode: I ...@@ -27,6 +27,7 @@ open class CwaSuccessResponseWithCodeMismatchNotSupportedError(val statusCode: I
open class CwaInformationalNotSupportedError(val statusCode: Int) : CwaWebException(statusCode) open class CwaInformationalNotSupportedError(val statusCode: Int) : CwaWebException(statusCode)
open class CwaRedirectNotSupportedError(val statusCode: Int) : CwaWebException(statusCode) open class CwaRedirectNotSupportedError(val statusCode: Int) : CwaWebException(statusCode)
class CwaUnknownHostException : CwaWebException(901)
class BadRequestException : CwaClientError(400) class BadRequestException : CwaClientError(400)
class UnauthorizedException : CwaClientError(401) class UnauthorizedException : CwaClientError(401)
class ForbiddenException : CwaClientError(403) class ForbiddenException : CwaClientError(403)
......
...@@ -8,6 +8,7 @@ import de.rki.coronawarnapp.exception.http.CwaInformationalNotSupportedError ...@@ -8,6 +8,7 @@ import de.rki.coronawarnapp.exception.http.CwaInformationalNotSupportedError
import de.rki.coronawarnapp.exception.http.CwaRedirectNotSupportedError import de.rki.coronawarnapp.exception.http.CwaRedirectNotSupportedError
import de.rki.coronawarnapp.exception.http.CwaServerError import de.rki.coronawarnapp.exception.http.CwaServerError
import de.rki.coronawarnapp.exception.http.CwaSuccessResponseWithCodeMismatchNotSupportedError import de.rki.coronawarnapp.exception.http.CwaSuccessResponseWithCodeMismatchNotSupportedError
import de.rki.coronawarnapp.exception.http.CwaUnknownHostException
import de.rki.coronawarnapp.exception.http.CwaWebException import de.rki.coronawarnapp.exception.http.CwaWebException
import de.rki.coronawarnapp.exception.http.ForbiddenException import de.rki.coronawarnapp.exception.http.ForbiddenException
import de.rki.coronawarnapp.exception.http.GatewayTimeoutException import de.rki.coronawarnapp.exception.http.GatewayTimeoutException
...@@ -25,40 +26,47 @@ import de.rki.coronawarnapp.exception.http.UnauthorizedException ...@@ -25,40 +26,47 @@ import de.rki.coronawarnapp.exception.http.UnauthorizedException
import de.rki.coronawarnapp.exception.http.UnsupportedMediaTypeException import de.rki.coronawarnapp.exception.http.UnsupportedMediaTypeException
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import java.net.UnknownHostException
class HttpErrorParser : Interceptor { class HttpErrorParser : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request()) try {
return when (val code = response.code) { val response = chain.proceed(chain.request())
200 -> response return when (val code = response.code) {
201 -> response 200 -> response
202 -> response 201 -> response
204 -> response 202 -> response
400 -> throw BadRequestException() 204 -> response
401 -> throw UnauthorizedException() 400 -> throw BadRequestException()
403 -> throw ForbiddenException() 401 -> throw UnauthorizedException()
404 -> throw NotFoundException() 403 -> throw ForbiddenException()
409 -> throw ConflictException() 404 -> throw NotFoundException()
410 -> throw GoneException() 409 -> throw ConflictException()
415 -> throw UnsupportedMediaTypeException() 410 -> throw GoneException()
429 -> throw TooManyRequestsException() 415 -> throw UnsupportedMediaTypeException()
500 -> throw InternalServerErrorException() 429 -> throw TooManyRequestsException()
501 -> throw NotImplementedException() 500 -> throw InternalServerErrorException()
502 -> throw BadGatewayException() 501 -> throw NotImplementedException()
503 -> throw ServiceUnavailableException() 502 -> throw BadGatewayException()
504 -> throw GatewayTimeoutException() 503 -> throw ServiceUnavailableException()
505 -> throw HTTPVersionNotSupported() 504 -> throw GatewayTimeoutException()
511 -> throw NetworkAuthenticationRequiredException() 505 -> throw HTTPVersionNotSupported()
598 -> throw NetworkReadTimeoutException() 511 -> throw NetworkAuthenticationRequiredException()
599 -> throw NetworkConnectTimeoutException() 598 -> throw NetworkReadTimeoutException()
else -> { 599 -> throw NetworkConnectTimeoutException()
if (code in 100..199) throw CwaInformationalNotSupportedError(code) else -> {
if (code in 200..299) throw CwaSuccessResponseWithCodeMismatchNotSupportedError(code) if (code in 100..199) throw CwaInformationalNotSupportedError(code)
if (code in 300..399) throw CwaRedirectNotSupportedError(code) if (code in 200..299) throw CwaSuccessResponseWithCodeMismatchNotSupportedError(
if (code in 400..499) throw CwaClientError(code) code
if (code in 500..599) throw CwaServerError(code) )
throw CwaWebException(code) if (code in 300..399) throw CwaRedirectNotSupportedError(code)
if (code in 400..499) throw CwaClientError(code)
if (code in 500..599) throw CwaServerError(code)
throw CwaWebException(code)
}
} }
} catch (err: UnknownHostException) {
throw CwaUnknownHostException()
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment