Add custom http error exceptions

This commit is contained in:
Mikołaj Pich 2020-10-15 11:22:44 +02:00
parent 4d7e031999
commit a9c771814a
22 changed files with 46 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package io.github.wulkanowy.sdk.scrapper
import io.github.wulkanowy.sdk.scrapper.attendance.Absent
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
import io.github.wulkanowy.sdk.scrapper.messages.Folder
import io.github.wulkanowy.sdk.scrapper.messages.Message

View file

@ -1,5 +1,3 @@
package io.github.wulkanowy.sdk.scrapper.exception
import io.github.wulkanowy.sdk.scrapper.ScrapperException
class InvalidEmailException(message: String) : ScrapperException(message)

View file

@ -1,5 +1,3 @@
package io.github.wulkanowy.sdk.scrapper.exception
import io.github.wulkanowy.sdk.scrapper.ScrapperException
open class InvalidPathException internal constructor(message: String) : ScrapperException(message)

View file

@ -1,5 +1,3 @@
package io.github.wulkanowy.sdk.scrapper.exception
import io.github.wulkanowy.sdk.scrapper.ScrapperException
open class PasswordResetErrorException(message: String) : ScrapperException(message)

View file

@ -1,4 +1,4 @@
package io.github.wulkanowy.sdk.scrapper
package io.github.wulkanowy.sdk.scrapper.exception
import java.io.IOException

View file

@ -1,5 +1,3 @@
package io.github.wulkanowy.sdk.scrapper.exception
import io.github.wulkanowy.sdk.scrapper.ScrapperException
class ServiceUnavailableException internal constructor(message: String) : ScrapperException(message)

View file

@ -1,5 +1,3 @@
package io.github.wulkanowy.sdk.scrapper.exception
import io.github.wulkanowy.sdk.scrapper.ScrapperException
open class VulcanException internal constructor(message: String) : ScrapperException(message)

View file

@ -1,7 +1,7 @@
package io.github.wulkanowy.sdk.scrapper.interceptor
import io.github.wulkanowy.sdk.scrapper.ApiResponse
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException
import io.github.wulkanowy.sdk.scrapper.exception.InvalidPathException
import io.github.wulkanowy.sdk.scrapper.exception.VulcanException

View file

@ -1,6 +1,6 @@
package io.github.wulkanowy.sdk.scrapper.interceptor
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ServiceUnavailableException
import io.github.wulkanowy.sdk.scrapper.exception.TemporarilyDisabledException
import io.github.wulkanowy.sdk.scrapper.exception.VulcanException

View file

@ -0,0 +1,23 @@
package io.github.wulkanowy.sdk.scrapper.interceptor
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.VulcanException
import io.github.wulkanowy.sdk.scrapper.login.NotLoggedInException
import okhttp3.Interceptor
import okhttp3.Response
class HttpErrorInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
if (response.isSuccessful || response.isRedirect) return response
return when (response.code()) {
429 -> throw NotLoggedInException(response.body()?.string() ?: response.message())
in 400..499 -> throw ScrapperException(response.code().toString() + ": " + response.message())
in 500..599 -> throw VulcanException(response.code().toString() + ": " + response.message())
else -> response
}
}
}

View file

@ -1,5 +1,5 @@
package io.github.wulkanowy.sdk.scrapper.login
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
class AccountPermissionException internal constructor(message: String) : ScrapperException(message)

View file

@ -1,5 +1,5 @@
package io.github.wulkanowy.sdk.scrapper.login
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
class BadCredentialsException internal constructor(message: String) : ScrapperException(message)

View file

@ -8,7 +8,7 @@ import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.ADFSLightCufs
import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.ADFSLightScoped
import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.AUTO
import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.STANDARD
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.VulcanException
import io.github.wulkanowy.sdk.scrapper.register.SendCertificateResponse
import io.github.wulkanowy.sdk.scrapper.service.LoginService

View file

@ -1,5 +1,5 @@
package io.github.wulkanowy.sdk.scrapper.login
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
class NotLoggedInException internal constructor(message: String) : ScrapperException(message)

View file

@ -1,5 +1,5 @@
package io.github.wulkanowy.sdk.scrapper.login
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
class PasswordChangeRequiredException internal constructor(message: String, val redirectUrl: String) : ScrapperException(message)

View file

@ -8,7 +8,7 @@ import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.ADFSLightCufs
import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.ADFSLightScoped
import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.AUTO
import io.github.wulkanowy.sdk.scrapper.Scrapper.LoginType.STANDARD
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.InvalidCaptchaException
import io.github.wulkanowy.sdk.scrapper.exception.InvalidEmailException
import io.github.wulkanowy.sdk.scrapper.exception.NoAccountFoundException

View file

@ -1,6 +1,6 @@
package io.github.wulkanowy.sdk.scrapper.repository
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.getScriptParam
import io.github.wulkanowy.sdk.scrapper.interceptor.handleErrors
import io.github.wulkanowy.sdk.scrapper.messages.Attachment

View file

@ -1,7 +1,7 @@
package io.github.wulkanowy.sdk.scrapper.repository
import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.TemporarilyDisabledException
import io.github.wulkanowy.sdk.scrapper.getNormalizedSymbol
import io.github.wulkanowy.sdk.scrapper.getScriptParam

View file

@ -4,11 +4,12 @@ import com.squareup.moshi.Moshi
import io.github.wulkanowy.sdk.scrapper.adapter.CustomDateAdapter
import io.github.wulkanowy.sdk.scrapper.OkHttpClientBuilderFactory
import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.TLSSocketFactory
import io.github.wulkanowy.sdk.scrapper.adapter.GradeDateDeserializer
import io.github.wulkanowy.sdk.scrapper.interceptor.AutoLoginInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.HttpErrorInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.StudentCookieInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.UserAgentInterceptor
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
@ -66,7 +67,8 @@ class ServiceManager(
HttpLoggingInterceptor().setLevel(logLevel) to true,
ErrorInterceptor() to false,
AutoLoginInterceptor(loginType, cookies, emptyCookieJarIntercept) { loginHelper.login(email, password) } to false,
UserAgentInterceptor(androidVersion, buildTag) to false
UserAgentInterceptor(androidVersion, buildTag) to false,
HttpErrorInterceptor() to false
)
private val trustManager: X509TrustManager by lazy {

View file

@ -5,6 +5,7 @@ import io.github.wulkanowy.sdk.scrapper.adapter.CustomDateAdapter
import io.github.wulkanowy.sdk.scrapper.adapter.GradeDateDeserializer
import io.github.wulkanowy.sdk.scrapper.interceptor.AutoLoginInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptor
import io.github.wulkanowy.sdk.scrapper.interceptor.HttpErrorInterceptor
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper
import io.github.wulkanowy.sdk.scrapper.repository.StudentRepository
import io.github.wulkanowy.sdk.scrapper.service.LoginService
@ -72,6 +73,7 @@ abstract class BaseLocalTest : BaseTest() {
if (autoLoginInterceptorOn) addInterceptor(autoLoginInterceptor)
}
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.addInterceptor(HttpErrorInterceptor())
.build()
private fun getAutoLoginInterceptor(loginType: Scrapper.LoginType, autoLogin: Boolean): AutoLoginInterceptor {

View file

@ -2,7 +2,7 @@ package io.github.wulkanowy.sdk.scrapper.repository
import io.github.wulkanowy.sdk.scrapper.BaseLocalTest
import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.VulcanException
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptorTest
import io.github.wulkanowy.sdk.scrapper.login.LoginHelper

View file

@ -3,7 +3,8 @@ package io.github.wulkanowy.sdk.scrapper.service
import io.github.wulkanowy.sdk.scrapper.BaseLocalTest
import io.github.wulkanowy.sdk.scrapper.OkHttpClientBuilderFactory
import io.github.wulkanowy.sdk.scrapper.Scrapper
import io.github.wulkanowy.sdk.scrapper.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.ScrapperException
import io.github.wulkanowy.sdk.scrapper.exception.VulcanException
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorInterceptorTest
import io.github.wulkanowy.sdk.scrapper.login.LoginTest
import io.github.wulkanowy.sdk.scrapper.notes.NotesTest
@ -13,7 +14,6 @@ import okhttp3.mockwebserver.MockResponse
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import retrofit2.HttpException
import java.net.URL
class ServiceManagerTest : BaseLocalTest() {
@ -105,7 +105,7 @@ class ServiceManagerTest : BaseLocalTest() {
val exception = res.exceptionOrNull()!!
assertEquals("HTTP 503 Server Error", exception.message)
assertEquals(HttpException::class.java, exception::class.java)
assertEquals("503: Server Error", exception.message)
assertEquals(VulcanException::class.java, exception::class.java)
}
}