@@ -79,10 +79,13 @@ class FirebaseUserImpl private constructor(
79
79
override fun onResponse (call : Call , response : Response ) {
80
80
if (! response.isSuccessful) {
81
81
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
+ )
86
89
} else {
87
90
source.setResult(null )
88
91
}
@@ -179,10 +182,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
179
182
@Throws(IOException ::class )
180
183
override fun onResponse (call : Call , response : Response ) {
181
184
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
+ )
186
188
} else {
187
189
val body = response.body()!! .use { it.string() }
188
190
user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject, true )
@@ -212,10 +214,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
212
214
@Throws(IOException ::class )
213
215
override fun onResponse (call : Call , response : Response ) {
214
216
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
+ )
219
220
} else {
220
221
val body = response.body()!! .use { it.string() }
221
222
val user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject)
@@ -245,10 +246,9 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
245
246
@Throws(IOException ::class )
246
247
override fun onResponse (call : Call , response : Response ) {
247
248
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
+ )
252
252
} else {
253
253
val body = response.body()!! .use { it.string() }
254
254
val user = FirebaseUserImpl (app, jsonParser.parseToJsonElement(body).jsonObject)
@@ -259,10 +259,23 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
259
259
return source.task
260
260
}
261
261
262
- internal fun formatErrorMessage (title : String , request : Request , response : Response ): String {
263
- return " $title API returned an error, " +
264
- " with url [${request.method()} ] ${request.url()} ${request.body()} -- " +
265
- " response [${response.code()} ] ${response.message()} ${response.body().use { it?.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
+ " $action API returned an error, " +
276
+ " with url [${request.method()} ] ${request.url()} ${request.body()} -- " +
277
+ " response [${response.code()} ] ${response.message()} $body "
278
+ )
266
279
}
267
280
268
281
fun signOut () {
0 commit comments