Skip to content

Commit 93fa04d

Browse files
committed
Use error.message for error code
1 parent abf1a6a commit 93fa04d

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/main/java/com/google/firebase/auth/FirebaseAuth.kt

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ class FirebaseUserImpl private constructor(
7979
override fun onResponse(call: Call, response: Response) {
8080
if (!response.isSuccessful) {
8181
FirebaseAuth.getInstance(app).signOut()
82-
source.setException(FirebaseAuthInvalidUserException(
83-
response.message(),
84-
FirebaseAuth.getInstance(app).formatErrorMessage("deleteAccount", request, response)
85-
))
82+
source.setException(
83+
FirebaseAuth.getInstance(app).createAuthInvalidUserException(
84+
"deleteAccount",
85+
request,
86+
response,
87+
)
88+
)
8689
} else {
8790
source.setResult(null)
8891
}
@@ -179,10 +182,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
179182
@Throws(IOException::class)
180183
override fun onResponse(call: Call, response: Response) {
181184
if (!response.isSuccessful) {
182-
source.setException(FirebaseAuthInvalidUserException(
183-
response.message(),
184-
formatErrorMessage("accounts:signUp", request, response)
185-
))
185+
source.setException(
186+
createAuthInvalidUserException("accounts:signUp", request, response)
187+
)
186188
} else {
187189
val body = response.body()!!.use { it.string() }
188190
user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject, true)
@@ -212,10 +214,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
212214
@Throws(IOException::class)
213215
override fun onResponse(call: Call, response: Response) {
214216
if (!response.isSuccessful) {
215-
source.setException(FirebaseAuthInvalidUserException(
216-
response.message(),
217-
formatErrorMessage("verifyCustomToken", request, response)
218-
))
217+
source.setException(
218+
createAuthInvalidUserException("verifyCustomToken", request, response)
219+
)
219220
} else {
220221
val body = response.body()!!.use { it.string() }
221222
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
@@ -245,10 +246,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
245246
@Throws(IOException::class)
246247
override fun onResponse(call: Call, response: Response) {
247248
if (!response.isSuccessful) {
248-
source.setException(FirebaseAuthInvalidUserException(
249-
response.message(),
250-
formatErrorMessage("verifyPassword", request, response)
251-
))
249+
source.setException(
250+
createAuthInvalidUserException("verifyPassword", request, response)
251+
)
252252
} else {
253253
val body = response.body()!!.use { it.string() }
254254
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
@@ -259,10 +259,27 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
259259
return source.task
260260
}
261261

262-
internal fun formatErrorMessage(title: String, request: Request, response: Response): String {
262+
internal fun createAuthInvalidUserException(
263+
action: String,
264+
request: Request,
265+
response: Response,
266+
): FirebaseAuthInvalidUserException {
267+
val body = response.body()!!.use { it.string() }
268+
val jsonObject = jsonParser.parseToJsonElement(body).jsonObject
269+
270+
return FirebaseAuthInvalidUserException(
271+
jsonObject["error"]?.jsonObject
272+
?.get("message")?.jsonPrimitive
273+
?.contentOrNull
274+
?: "UNKNOWN_ERROR",
275+
formatErrorMessage(action, request, response, body)
276+
)
277+
}
278+
279+
internal fun formatErrorMessage(title: String, request: Request, response: Response, responseBody: String): String {
263280
return "$title API returned an error, " +
264281
"with url [${request.method()}] ${request.url()} ${request.body()} -- " +
265-
"response [${response.code()}] ${response.message()} ${response.body().use { it?.string() }}"
282+
"response [${response.code()}] ${response.message()} $responseBody"
266283
}
267284

268285
fun signOut() {

0 commit comments

Comments
 (0)