Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
introduce HomeConnectError.StartProgramIssue for differentiating betw…
Browse files Browse the repository at this point in the history
…een different error cases when starting a program
  • Loading branch information
dominikgold committed Dec 9, 2020
1 parent 852513a commit 032afab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ internal class DefaultHomeConnectInteractor(
try {
val response = homeConnectApi.startProgram(forApplianceId, HomeConnectApiRequest(program))
if (!response.isSuccessful) {
val errorDescription = response.errorBody()?.tryParsingApiError()?.description
throw HomeConnectError.Unspecified(message = errorDescription ?: "", cause = HttpException(response))
val error = response.errorBody()?.tryParsingApiError()
throw HomeConnectError.StartProgramIssue(
errorKey = error?.key ?: "",
message = error?.description ?: "",
cause = HttpException(response),
)
}
} catch (e: Throwable) {
Log.e("HomeConnectApi", "starting a program failed", e)
errorHandler.handle(e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.ajnsnewmedia.kitchenstories.homeconnect.sdk
import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ sealed class HomeConnectError(message: String? = null, cause: Throwable? = null)

object StaleAuthorization : HomeConnectError("The user session needs to be restored")

class StartProgramIssue(val errorKey: String, message: String?, cause: Throwable?) : HomeConnectError(message, cause)

class Unspecified(message: String?, cause: Throwable?) : HomeConnectError(message, cause)

abstract class UserAbortedAuthorization(errorDescription: String?): HomeConnectError("User aborted the login: '${errorDescription}'")
abstract class UserAbortedAuthorization(errorDescription: String?) : HomeConnectError("User aborted the login: '${errorDescription}'")

/**
* the user pressed cancel after logging in when reviewing the app permissions
*/
class UserAbortedAuthorizationWhileGrantingPermissision(errorDescription: String?): UserAbortedAuthorization(errorDescription)
class UserAbortedAuthorizationWhileGrantingPermission(errorDescription: String?) : UserAbortedAuthorization(errorDescription)

/**
* The user pressed cancel on the username and password entry page
*/
class UserAbortedAuthorizationOnLogin(errorDescription: String?): UserAbortedAuthorization(errorDescription)
class UserAbortedAuthorizationOnLogin(errorDescription: String?) : UserAbortedAuthorization(errorDescription)

companion object{
companion object {
fun getExceptionFromError(errorString: String?, errorDescription: String?, cause: Throwable?): HomeConnectError {
//documentation from https://developer.home-connect.com/docs/authorization/authorizationerrors
return if (errorString == "access_denied"){
if (errorDescription == "login aborted by the user"){
return if (errorString == "access_denied") {
if (errorDescription == "login aborted by the user") {
UserAbortedAuthorizationOnLogin(errorDescription)
} else if (errorDescription == "grant operation aborted by the user") {
UserAbortedAuthorizationWhileGrantingPermissision(errorDescription)
}else {
UserAbortedAuthorizationWhileGrantingPermission(errorDescription)
} else {
Unspecified(errorDescription, cause = cause)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class DefaultHomeConnectInteractorTest {
whenever(homeConnectApi.startProgram(any(), any())).thenReturn(Response.error(400, "".toResponseBody()))

verifyThrowingSuspended(action = { interactor.startProgram(forApplianceId = "appliance_id", testStartProgramRequest) }) { error ->
assertTrue(error is HomeConnectError.Unspecified)
assertTrue(error is HomeConnectError.StartProgramIssue)
}
}

Expand Down

0 comments on commit 032afab

Please sign in to comment.