Skip to content

Commit 7f58729

Browse files
committed
Consolidated exceptions using when clause
1 parent a04cb71 commit 7f58729

2 files changed

Lines changed: 62 additions & 28 deletions

File tree

auth0/src/main/java/com/auth0/android/dpop/DPoPKeyStore.kt

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,16 @@ internal open class DPoPKeyStore {
9999
if (publicKey != null) {
100100
return Pair(privateKey, publicKey)
101101
}
102-
} catch (e: KeyStoreException) {
103-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
104-
} catch (e: NoSuchAlgorithmException) {
105-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
106-
} catch (e: UnrecoverableKeyException) {
107-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
108-
} catch (e: ClassCastException) {
109-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
110-
} catch (e: IOException) {
111-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
112-
} catch (e: CertificateException) {
113-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
102+
} catch (e: Exception) {
103+
when (e) {
104+
is KeyStoreException,
105+
is NoSuchAlgorithmException,
106+
is UnrecoverableKeyException,
107+
is ClassCastException,
108+
is IOException,
109+
is CertificateException -> throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
110+
else -> throw DPoPException(DPoPException.Code.UNKNOWN_ERROR, e)
111+
}
114112
}
115113
Log.d(TAG, "Returning null key pair ")
116114
return null
@@ -119,28 +117,28 @@ internal open class DPoPKeyStore {
119117
fun hasKeyPair(): Boolean {
120118
try {
121119
return keyStore.containsAlias(KEY_ALIAS)
122-
} catch (e: KeyStoreException) {
123-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
124-
} catch (e: NoSuchAlgorithmException) {
125-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
126-
} catch (e: IOException) {
127-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
128-
} catch (e: CertificateException) {
129-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
120+
} catch (e: Exception) {
121+
when (e) {
122+
is KeyStoreException,
123+
is NoSuchAlgorithmException,
124+
is IOException,
125+
is CertificateException -> throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
126+
else -> throw DPoPException(DPoPException.Code.UNKNOWN_ERROR, e)
127+
}
130128
}
131129
}
132130

133131
fun deleteKeyPair() {
134132
try {
135133
keyStore.deleteEntry(KEY_ALIAS)
136-
} catch (e: KeyStoreException) {
137-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
138-
} catch (e: NoSuchAlgorithmException) {
139-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
140-
} catch (e: IOException) {
141-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
142-
} catch (e: CertificateException) {
143-
throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
134+
} catch (e: Exception) {
135+
when (e) {
136+
is KeyStoreException,
137+
is NoSuchAlgorithmException,
138+
is IOException,
139+
is CertificateException -> throw DPoPException(DPoPException.Code.KEY_STORE_ERROR, e)
140+
else -> throw DPoPException(DPoPException.Code.UNKNOWN_ERROR, e)
141+
}
144142
}
145143
}
146144

auth0/src/test/java/com/auth0/android/dpop/DPoPKeyStoreTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ public class DPoPKeyStoreTest {
229229
assertThat(exception.cause, `is`(cause))
230230
}
231231

232+
@Test
233+
public fun `getKeyPair should throw UNKNOWN_ERROR on unhandled exception`() {
234+
val cause = RuntimeException("Unexpected error")
235+
whenever(mockKeyStore.getKey(any(), anyOrNull())).thenThrow(cause)
236+
237+
val exception = assertThrows(DPoPException::class.java) {
238+
dpopKeyStore.getKeyPair()
239+
}
240+
assertEquals(exception.message, DPoPException.UNKNOWN_ERROR.message)
241+
assertThat(exception.cause, `is`(cause))
242+
}
243+
232244
@Test
233245
public fun `hasKeyPair should return true when alias exists`() {
234246
whenever(mockKeyStore.containsAlias(any())).thenReturn(true)
@@ -255,6 +267,18 @@ public class DPoPKeyStoreTest {
255267
assertThat(exception.cause, `is`(cause))
256268
}
257269

270+
@Test
271+
public fun `hasKeyPair should throw UNKNOWN_ERROR on unhandled exception`() {
272+
val cause = RuntimeException("Unexpected error")
273+
whenever(mockKeyStore.containsAlias(any())).thenThrow(cause)
274+
275+
val exception = assertThrows(DPoPException::class.java) {
276+
dpopKeyStore.hasKeyPair()
277+
}
278+
assertEquals(exception.message, DPoPException.UNKNOWN_ERROR.message)
279+
assertThat(exception.cause, `is`(cause))
280+
}
281+
258282
@Test
259283
public fun `deleteKeyPair should call deleteEntry`() {
260284
dpopKeyStore.deleteKeyPair()
@@ -274,6 +298,18 @@ public class DPoPKeyStoreTest {
274298
}
275299

276300

301+
@Test
302+
public fun `deleteKeyPair should throw UNKNOWN_ERROR on unhandled exception`() {
303+
val cause = RuntimeException("Unexpected error")
304+
whenever(mockKeyStore.deleteEntry(any())).thenThrow(cause)
305+
306+
val exception = assertThrows(DPoPException::class.java) {
307+
dpopKeyStore.deleteKeyPair()
308+
}
309+
assertEquals(exception.message, DPoPException.UNKNOWN_ERROR.message)
310+
assertThat(exception.cause, `is`(cause))
311+
}
312+
277313
@Test
278314
public fun `generateKeyPair should retry without StrongBox when ProviderException occurs with StrongBox enabled`() {
279315
val providerException = ProviderException("StrongBox attestation failed")

0 commit comments

Comments
 (0)