@@ -99,9 +99,10 @@ class FirebaseUserImpl private constructor(
9999 if (! response.isSuccessful) {
100100 FirebaseAuth .getInstance(app).signOut()
101101 source.setException(
102- FirebaseAuthInvalidUserException (
103- response.message(),
104- FirebaseAuth .getInstance(app).formatErrorMessage(" deleteAccount" , request, response)
102+ FirebaseAuth .getInstance(app).createAuthInvalidUserException(
103+ " deleteAccount" ,
104+ request,
105+ response,
105106 )
106107 )
107108 } else {
@@ -200,10 +201,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
200201 override fun onResponse (call : Call , response : Response ) {
201202 if (! response.isSuccessful) {
202203 source.setException(
203- FirebaseAuthInvalidUserException (
204- response.message(),
205- formatErrorMessage(" accounts:signUp" , request, response)
206- )
204+ createAuthInvalidUserException(" accounts:signUp" , request, response)
207205 )
208206 } else {
209207 val body = response.body()!! .use { it.string() }
@@ -235,10 +233,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
235233 override fun onResponse (call : Call , response : Response ) {
236234 if (! response.isSuccessful) {
237235 source.setException(
238- FirebaseAuthInvalidUserException (
239- response.message(),
240- formatErrorMessage(" verifyCustomToken" , request, response)
241- )
236+ createAuthInvalidUserException(" verifyCustomToken" , request, response)
242237 )
243238 } else {
244239 val body = response.body()!! .use { it.string() }
@@ -270,10 +265,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
270265 override fun onResponse (call : Call , response : Response ) {
271266 if (! response.isSuccessful) {
272267 source.setException(
273- FirebaseAuthInvalidUserException (
274- response.message(),
275- formatErrorMessage(" verifyPassword" , request, response)
276- )
268+ createAuthInvalidUserException(" verifyPassword" , request, response)
277269 )
278270 } else {
279271 val body = response.body()!! .use { it.string() }
@@ -285,10 +277,23 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
285277 return source.task
286278 }
287279
288- internal fun formatErrorMessage (title : String , request : Request , response : Response ): String {
289- return " $title API returned an error, " +
290- " with url [${request.method()} ] ${request.url()} ${request.body()} -- " +
291- " response [${response.code()} ] ${response.message()} ${response.body().use { it?.string() }} "
280+ internal fun createAuthInvalidUserException (
281+ action : String ,
282+ request : Request ,
283+ response : Response ,
284+ ): FirebaseAuthInvalidUserException {
285+ val body = response.body()!! .use { it.string() }
286+ val jsonObject = jsonParser.parseToJsonElement(body).jsonObject
287+
288+ return FirebaseAuthInvalidUserException (
289+ jsonObject[" error" ]?.jsonObject
290+ ?.get(" message" )?.jsonPrimitive
291+ ?.contentOrNull
292+ ? : " UNKNOWN_ERROR" ,
293+ " $action API returned an error, " +
294+ " with url [${request.method()} ] ${request.url()} ${request.body()} -- " +
295+ " response [${response.code()} ] ${response.message()} $body "
296+ )
292297 }
293298
294299 fun signOut () {
0 commit comments