-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DescopeLogger and DescopeException (#47)
- Loading branch information
1 parent
e454f69
commit c5e3091
Showing
13 changed files
with
383 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
descopesdk/src/main/java/com/descope/internal/http/ClientErrors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.descope.internal.http | ||
|
||
import com.descope.internal.others.toMap | ||
import com.descope.internal.others.with | ||
import com.descope.types.DescopeException | ||
import org.json.JSONObject | ||
|
||
internal fun parseServerError(response: String): DescopeException? = try { | ||
val map = JSONObject(response).toMap() | ||
val code = map["errorCode"] as? String ?: throw Exception("errorCode is required") | ||
val desc = map["errorDescription"] as? String ?: "Descope server error" | ||
val message = map["errorMessage"] as? String | ||
DescopeException(code = code, desc = desc, message = message) | ||
} catch (_: Exception) { | ||
null | ||
} | ||
|
||
internal fun exceptionFromResponseCode(code: Int): DescopeException? { | ||
val desc = when (code) { | ||
in 200..299 -> null | ||
400 -> "The request was invalid" | ||
401 -> "The request was unauthorized" | ||
403 -> "The request was forbidden" | ||
404 -> "The resource was not found" | ||
500, 503 -> "The server failed with status code $code" | ||
in 500..<600 -> "The server was unreachable" | ||
else -> "The server returned status code $code" | ||
} | ||
return desc?.run { DescopeException.httpError.with(desc = this) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
descopesdk/src/main/java/com/descope/internal/others/Errors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.descope.internal.others | ||
|
||
import com.descope.types.DescopeException | ||
|
||
internal fun DescopeException.with(desc: String? = null, message: String? = null, cause: Throwable? = null) = DescopeException( | ||
code = code, | ||
desc = desc ?: this.desc, | ||
message = message ?: this.message, | ||
cause = cause ?: this.cause, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.