Skip to content

Commit 644add1

Browse files
committed
Added error codes to generalize errors
Signed-off-by: Gaurav Goel <[email protected]>
1 parent 09d6119 commit 644add1

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
154154
if (web3AuthResponse.error?.isNotBlank() == true) {
155155
loginCompletableFuture.completeExceptionally(
156156
UnKnownException(
157-
web3AuthResponse.error ?: "Something went wrong"
157+
web3AuthResponse.error ?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_ERROR)
158158
)
159159
)
160160
}
@@ -259,7 +259,9 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
259259
Handler(Looper.getMainLooper()).postDelayed(10) {
260260
sessionCompletableFuture.completeExceptionally(
261261
UnKnownException(
262-
web3AuthResponse.error ?: "Something went wrong"
262+
web3AuthResponse.error ?: Web3AuthError.getError(
263+
ErrorCode.SOMETHING_WENT_ERROR
264+
)
263265
)
264266
)
265267
}
@@ -334,7 +336,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
334336

335337
fun getPrivkey(): String {
336338
val privKey: String = if (web3AuthResponse == null) {
337-
throw Error("No user found, please login again")
339+
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
338340
} else {
339341
if (web3AuthOption.useCoreKitKey == true) {
340342
web3AuthResponse.coreKitEd25519PrivKey.toString()
@@ -347,7 +349,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
347349

348350
fun getEd25519PrivKey(): String {
349351
val ed25519Key: String = if (web3AuthResponse == null) {
350-
throw Error("No user found, please login again")
352+
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
351353
} else {
352354
if (web3AuthOption.useCoreKitKey == true) {
353355
web3AuthResponse.coreKitEd25519PrivKey.toString()
@@ -360,7 +362,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
360362

361363
fun getUserInfo(): UserInfo? {
362364
if (web3AuthResponse == null || web3AuthResponse.userInfo == null) {
363-
throw Error("No userInfo found, please login again")
365+
throw Error(Web3AuthError.getError(ErrorCode.NOUSERFOUND))
364366
} else {
365367
return web3AuthResponse.userInfo
366368
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.web3auth.core.types
2+
3+
object Web3AuthError {
4+
5+
fun getError(errorCode: ErrorCode): String {
6+
return when (errorCode) {
7+
ErrorCode.NOUSERFOUND -> {
8+
"No user found, please login again!"
9+
}
10+
ErrorCode.ENCODING_ERROR -> {
11+
"Encoding Error"
12+
}
13+
ErrorCode.DECODING_ERROR -> {
14+
"Decoding Error"
15+
}
16+
ErrorCode.SOMETHING_WENT_ERROR -> {
17+
"Something went wrong!"
18+
}
19+
ErrorCode.RUNTIME_ERROR -> {
20+
"Runtime Error"
21+
}
22+
ErrorCode.APP_CANCELLED -> {
23+
"App Cancelled"
24+
}
25+
}
26+
}
27+
}
28+
29+
enum class ErrorCode {
30+
NOUSERFOUND,
31+
ENCODING_ERROR,
32+
DECODING_ERROR,
33+
RUNTIME_ERROR,
34+
APP_CANCELLED,
35+
SOMETHING_WENT_ERROR,
36+
}

0 commit comments

Comments
 (0)