Skip to content

Commit

Permalink
Use error.message for error code
Browse files Browse the repository at this point in the history
  • Loading branch information
fmasa committed Apr 10, 2024
1 parent abf1a6a commit 93fa04d
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ class FirebaseUserImpl private constructor(
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
FirebaseAuth.getInstance(app).signOut()
source.setException(FirebaseAuthInvalidUserException(
response.message(),
FirebaseAuth.getInstance(app).formatErrorMessage("deleteAccount", request, response)
))
source.setException(
FirebaseAuth.getInstance(app).createAuthInvalidUserException(
"deleteAccount",
request,
response,
)
)
} else {
source.setResult(null)
}
Expand Down Expand Up @@ -179,10 +182,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
source.setException(FirebaseAuthInvalidUserException(
response.message(),
formatErrorMessage("accounts:signUp", request, response)
))
source.setException(
createAuthInvalidUserException("accounts:signUp", request, response)
)
} else {
val body = response.body()!!.use { it.string() }
user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject, true)
Expand Down Expand Up @@ -212,10 +214,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
source.setException(FirebaseAuthInvalidUserException(
response.message(),
formatErrorMessage("verifyCustomToken", request, response)
))
source.setException(
createAuthInvalidUserException("verifyCustomToken", request, response)
)
} else {
val body = response.body()!!.use { it.string() }
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
Expand Down Expand Up @@ -245,10 +246,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
source.setException(FirebaseAuthInvalidUserException(
response.message(),
formatErrorMessage("verifyPassword", request, response)
))
source.setException(
createAuthInvalidUserException("verifyPassword", request, response)
)
} else {
val body = response.body()!!.use { it.string() }
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
Expand All @@ -259,10 +259,27 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
return source.task
}

internal fun formatErrorMessage(title: String, request: Request, response: Response): 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",
formatErrorMessage(action, request, response, body)
)
}

internal fun formatErrorMessage(title: String, request: Request, response: Response, responseBody: String): 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() }}"
"response [${response.code()}] ${response.message()} $responseBody"
}

fun signOut() {
Expand Down

0 comments on commit 93fa04d

Please sign in to comment.