Skip to content

Commit

Permalink
refactor exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkNenadov committed Aug 7, 2023
1 parent 89a60e5 commit e49194a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ interface KpasswordlessService {
fun deleteCredentials(privateKey: String, credentialId: String): Boolean
}
```

Runtime Exceptions:
* KpasswordlessServiceException
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import org.json.JSONObject
import org.pythonbyte.haveibeenkwned.domain.KPasswordlessIdentity
import org.pythonbyte.haveibeenkwned.domain.KPasswordlessSignIn
import org.pythonbyte.haveibeenkwned.domain.KpasswordlessCredential
import org.pythonbyte.haveibeenkwned.service.exceptions.KpasswordlessServiceException
import org.pythonbyte.krux.http.buildRequest
import org.pythonbyte.krux.json.JsonObject
import org.pythonbyte.krux.properties.PropertyReader
import org.pythonbyte.krux.http.sendRequest
import java.lang.Exception

class KpasswordlessServiceImpl : KpasswordlessService {
private val propertiesFile = "/kpasswordless.properties"
Expand All @@ -34,7 +34,7 @@ class KpasswordlessServiceImpl : KpasswordlessService {
val responseObject = JsonObject( JSONObject( response.body().string().trim() ))
return responseObject.getString("token")
}
throw Exception( "Register Token failed with code ${response.code()}] [${response.message()}]" )
throw KpasswordlessServiceException(response, "Register token")
}

override fun addAliases( privateKey: String, identity: KPasswordlessIdentity, aliases: List<String>): Boolean {
Expand All @@ -45,7 +45,7 @@ class KpasswordlessServiceImpl : KpasswordlessService {
if ( response.code() == 200 ) {
return true
}
throw Exception( "Add Aliases failed with code [${response.code()}] [${response.message()}]" )
throw KpasswordlessServiceException(response, "Add alias")
}

override fun signin( privateKey: String, token: String ): KPasswordlessSignIn {
Expand All @@ -58,9 +58,7 @@ class KpasswordlessServiceImpl : KpasswordlessService {
return KPasswordlessSignIn.create( responseObject )
}

val responseJson = response.body().string().trim()

throw Exception( "Sign In failed with code [${response.code()}] [${response.message()}] json [$responseJson]" )
throw KpasswordlessServiceException(response, "Sign in verify")
}

override fun listCredentials( privateKey: String, identity: KPasswordlessIdentity ): List<KpasswordlessCredential> {
Expand All @@ -74,9 +72,7 @@ class KpasswordlessServiceImpl : KpasswordlessService {
return KpasswordlessCredential.createList( responseObject.getArray("values") )
}

val responseJson = response.body().string().trim()

throw Exception( "List Credentials failed with code [${response.code()}] [${response.message()}] json [$responseJson]" )
throw KpasswordlessServiceException(response, "List credentials")
}

override fun deleteCredentials(privateKey: String, credentialId: String): Boolean {
Expand All @@ -88,7 +84,7 @@ class KpasswordlessServiceImpl : KpasswordlessService {
return true
}

throw Exception( "Delete Aliases failed with code [${response.code()}] [${response.message()}] json [${response.body().string().trim()}]" )
throw KpasswordlessServiceException(response, "Delete credentials")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.pythonbyte.haveibeenkwned.service.exceptions

import com.squareup.okhttp.Response;

class KpasswordlessServiceException(response: Response, requestAlias: String) : RuntimeException() {
val code: Int = response.code()
val responseMessage: String = response.message()
val responseBody: String = response.body().string().trim()
val requestAlias: String = requestAlias;

override val message: String?
get() = "KpasswordlessServiceException: $requestAlias request failed with code [$code] [$responseMessage] json [$responseBody]"
}

0 comments on commit e49194a

Please sign in to comment.