Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use error.message for Auth error codes #9

Merged
merged 4 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ class FirebaseUserImpl private constructor(
if (!response.isSuccessful) {
FirebaseAuth.getInstance(app).signOut()
source.setException(
FirebaseAuthInvalidUserException(
response.message(),
FirebaseAuth.getInstance(app).formatErrorMessage("deleteAccount", request, response)
FirebaseAuth.getInstance(app).createAuthInvalidUserException(
"deleteAccount",
request,
response,
)
)
} else {
Expand Down Expand Up @@ -200,10 +201,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
source.setException(
FirebaseAuthInvalidUserException(
response.message(),
formatErrorMessage("accounts:signUp", request, response)
)
createAuthInvalidUserException("accounts:signUp", request, response)
)
} else {
val body = response.body()!!.use { it.string() }
Expand Down Expand Up @@ -235,10 +233,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
source.setException(
FirebaseAuthInvalidUserException(
response.message(),
formatErrorMessage("verifyCustomToken", request, response)
)
createAuthInvalidUserException("verifyCustomToken", request, response)
)
} else {
val body = response.body()!!.use { it.string() }
Expand Down Expand Up @@ -270,10 +265,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
source.setException(
FirebaseAuthInvalidUserException(
response.message(),
formatErrorMessage("verifyPassword", request, response)
)
createAuthInvalidUserException("verifyPassword", request, response)
)
} else {
val body = response.body()!!.use { it.string() }
Expand All @@ -285,10 +277,23 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
return source.task
}

internal fun formatErrorMessage(title: String, request: Request, response: Response): String {
return "$title API returned an error, " +
"with url [${request.method()}] ${request.url()} ${request.body()} -- " +
"response [${response.code()}] ${response.message()} ${response.body().use { it?.string() }}"
internal fun createAuthInvalidUserException(
nbransby marked this conversation as resolved.
Show resolved Hide resolved
action: String,
request: Request,
response: Response,
): FirebaseAuthInvalidUserException {
val body = response.body()!!.use { it.string() }
val jsonObject = jsonParser.parseToJsonElement(body).jsonObject

return FirebaseAuthInvalidUserException(
jsonObject["error"]?.jsonObject
?.get("message")?.jsonPrimitive
?.contentOrNull
?: "UNKNOWN_ERROR",
"$action API returned an error, " +
"with url [${request.method()}] ${request.url()} ${request.body()} -- " +
"response [${response.code()}] ${response.message()} $body"
)
}

fun signOut() {
Expand Down
Loading