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 1 commit
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
53 changes: 33 additions & 20 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,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