31
31
import com .google .common .base .Strings ;
32
32
import com .google .common .collect .ImmutableList ;
33
33
import com .google .common .collect .ImmutableMap ;
34
+ import com .google .firebase .ErrorCode ;
34
35
import com .google .firebase .FirebaseApp ;
35
36
import com .google .firebase .ImplFirebaseTrampolines ;
36
37
import com .google .firebase .internal .ApiClientUtils ;
38
+ import com .google .firebase .IncomingHttpResponse ;
37
39
import com .google .firebase .internal .CallableOperation ;
38
40
import java .nio .charset .StandardCharsets ;
39
41
import java .util .ArrayList ;
@@ -318,20 +320,24 @@ protected String execute() throws FirebaseProjectManagementException {
318
320
payloadBuilder .put ("display_name" , displayName );
319
321
}
320
322
OperationResponse operationResponseInstance = new OperationResponse ();
321
- httpHelper .makePostRequest (
323
+ IncomingHttpResponse response = httpHelper .makePostRequest (
322
324
url , payloadBuilder .build (), operationResponseInstance , projectId , "Project ID" );
323
325
if (Strings .isNullOrEmpty (operationResponseInstance .name )) {
324
- throw HttpHelper . createFirebaseProjectManagementException (
326
+ String message = buildMessage (
325
327
namespace ,
326
328
"Bundle ID" ,
327
- "Unable to create App: server returned null operation name." ,
328
- /* cause= */ null );
329
+ "Unable to create App: server returned null operation name." );
330
+ throw new FirebaseProjectManagementException ( ErrorCode . INTERNAL , message , response );
329
331
}
330
332
return operationResponseInstance .name ;
331
333
}
332
334
};
333
335
}
334
336
337
+ private String buildMessage (String resourceId , String resourceIdName , String description ) {
338
+ return String .format ("%s \" %s\" : %s" , resourceIdName , resourceId , description );
339
+ }
340
+
335
341
private String pollOperation (String projectId , String operationName )
336
342
throws FirebaseProjectManagementException {
337
343
String url = String .format ("%s/v1/%s" , FIREBASE_PROJECT_MANAGEMENT_URL , operationName );
@@ -341,27 +347,29 @@ private String pollOperation(String projectId, String operationName)
341
347
* Math .pow (POLL_EXPONENTIAL_BACKOFF_FACTOR , currentAttempt ));
342
348
sleepOrThrow (projectId , delayMillis );
343
349
OperationResponse operationResponseInstance = new OperationResponse ();
344
- httpHelper .makeGetRequest (url , operationResponseInstance , projectId , "Project ID" );
350
+ IncomingHttpResponse response = httpHelper .makeGetRequest (
351
+ url , operationResponseInstance , projectId , "Project ID" );
345
352
if (!operationResponseInstance .done ) {
346
353
continue ;
347
354
}
348
355
// The Long Running Operation API guarantees that when done == true, exactly one of 'response'
349
356
// or 'error' is set.
350
357
if (operationResponseInstance .response == null
351
358
|| Strings .isNullOrEmpty (operationResponseInstance .response .appId )) {
352
- throw HttpHelper . createFirebaseProjectManagementException (
359
+ String message = buildMessage (
353
360
projectId ,
354
361
"Project ID" ,
355
- "Unable to create App: internal server error." ,
356
- /* cause= */ null );
362
+ "Unable to create App: internal server error." );
363
+ throw new FirebaseProjectManagementException ( ErrorCode . INTERNAL , message , response );
357
364
}
358
365
return operationResponseInstance .response .appId ;
359
366
}
360
- throw HttpHelper .createFirebaseProjectManagementException (
367
+
368
+ String message = buildMessage (
361
369
projectId ,
362
370
"Project ID" ,
363
- "Unable to create App: deadline exceeded." ,
364
- /* cause= */ null );
371
+ "Unable to create App: deadline exceeded." );
372
+ throw new FirebaseProjectManagementException ( ErrorCode . DEADLINE_EXCEEDED , message , null );
365
373
}
366
374
367
375
/**
@@ -420,19 +428,22 @@ private WaitOperationRunnable(
420
428
public void run () {
421
429
String url = String .format ("%s/v1/%s" , FIREBASE_PROJECT_MANAGEMENT_URL , operationName );
422
430
OperationResponse operationResponseInstance = new OperationResponse ();
431
+ IncomingHttpResponse httpResponse ;
423
432
try {
424
- httpHelper .makeGetRequest (url , operationResponseInstance , projectId , "Project ID" );
433
+ httpResponse = httpHelper .makeGetRequest (
434
+ url , operationResponseInstance , projectId , "Project ID" );
425
435
} catch (FirebaseProjectManagementException e ) {
426
436
settableFuture .setException (e );
427
437
return ;
428
438
}
429
439
if (!operationResponseInstance .done ) {
430
440
if (numberOfPreviousPolls + 1 >= MAXIMUM_POLLING_ATTEMPTS ) {
431
- settableFuture .setException (HttpHelper .createFirebaseProjectManagementException (
432
- projectId ,
441
+ String message = buildMessage (projectId ,
433
442
"Project ID" ,
434
- "Unable to create App: deadline exceeded." ,
435
- /* cause= */ null ));
443
+ "Unable to create App: deadline exceeded." );
444
+ FirebaseProjectManagementException exception = new FirebaseProjectManagementException (
445
+ ErrorCode .DEADLINE_EXCEEDED , message , httpResponse );
446
+ settableFuture .setException (exception );
436
447
} else {
437
448
long delayMillis = (long ) (
438
449
POLL_BASE_WAIT_TIME_MILLIS
@@ -451,11 +462,12 @@ public void run() {
451
462
// or 'error' is set.
452
463
if (operationResponseInstance .response == null
453
464
|| Strings .isNullOrEmpty (operationResponseInstance .response .appId )) {
454
- settableFuture .setException (HttpHelper .createFirebaseProjectManagementException (
455
- projectId ,
465
+ String message = buildMessage (projectId ,
456
466
"Project ID" ,
457
- "Unable to create App: internal server error." ,
458
- /* cause= */ null ));
467
+ "Unable to create App: internal server error." );
468
+ FirebaseProjectManagementException exception = new FirebaseProjectManagementException (
469
+ ErrorCode .INTERNAL , message , httpResponse );
470
+ settableFuture .setException (exception );
459
471
} else {
460
472
settableFuture .set (operationResponseInstance .response .appId );
461
473
}
@@ -765,11 +777,10 @@ private void sleepOrThrow(String projectId, long delayMillis)
765
777
try {
766
778
sleeper .sleep (delayMillis );
767
779
} catch (InterruptedException e ) {
768
- throw HttpHelper .createFirebaseProjectManagementException (
769
- projectId ,
780
+ String message = buildMessage (projectId ,
770
781
"Project ID" ,
771
- "Unable to create App: exponential backoff interrupted." ,
772
- /* cause= */ null );
782
+ "Unable to create App: exponential backoff interrupted." );
783
+ throw new FirebaseProjectManagementException ( ErrorCode . ABORTED , message , null );
773
784
}
774
785
}
775
786
0 commit comments