diff --git a/src/main/java/com/google/firebase/auth/FirebaseAuth.kt b/src/main/java/com/google/firebase/auth/FirebaseAuth.kt index 0e6f373..f692d30 100644 --- a/src/main/java/com/google/firebase/auth/FirebaseAuth.kt +++ b/src/main/java/com/google/firebase/auth/FirebaseAuth.kt @@ -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 { @@ -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() } @@ -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() } @@ -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() } @@ -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( + 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() {