Skip to content

Commit cc520a7

Browse files
hiranya911MathBunnyegilmorezkevinthecheung
authored
change: New error handling APIs (merging v7 branch into master) (#465)
* Added core types for the error handling revamp (#350) * Added core types for the error handling revamp * Fixed copyright year * Added ErrorHandlingHttpClient API (#353) * Added core error handling abstractions * Added unit tests for the new functionality * Exposed getErrorCode as getErrorCodeNew * Enabled error code assertions * Error handling revamp for the FirebaseMessaging API (#357) * Added core error handling abstractions * Added unit tests for the new functionality * Exposed getErrorCode as getErrorCodeNew * Enabled error code assertions * Error handling revamp for FCM APIs * Cleaned up the FirebaseMessagingException * Cleaned up the AbstractHttpErrorHandler class * Updated tests * Error handling revamp for FirebaseInstanceId API (#359) * Delete instance ID API error handling revamp * Added tests for IO and parse errors * fix(auth): Migrated user management APIs to the new error handling scheme (#360) * Error handling revamp in FirebaseUserManager * Updated integration tests; Added documentation * Assigning the correct ErrorCode for auth errors * Moved AuthErrorHandler to a separate top-level class * Error handling revamp for token verification APIs (#362) * Error handling revamp for token verification APIs * Updated javadocs * Error handling revamp for the custom token creation API (#366) * Error handlign revamp for the custom token creation API * Using the correct authorized HTTP client for IAM requests * Error handling revamp for the project management API (#367) * Error handling revamp for the project management API * Minor code and test cleanup * Fixed some lint errors; Removed requestFactory reference from project mgt service impl * Renamed getErrorCodeNew() to getErrorCode() (#379) * Minor code and test cleanup * Renamed getErrorCodeNew() to getErrorCode() in FirebaseException * Fixing checkstyle error * Fixing some deprecation warnings (#380) * Handling IID error codes correctly (#381) * Removed old deprecated APIs (#383) * fix: Removed unused FirebaseAppStore abstraction (#427) * fix: Removed unused FirebaseAppStore abstraction * Using the keySet of App instances to populate the app names list * chore: Removing redundant test dependency (#441) * chore: Make user import hashing classes final (#425) * chore: Merged with v7 branch with master (#456) * fix(fcm): Replacing deprecated Batch API constructor (#460) * fix: Handling http method override in ErrorHandlingHttpClient (#459) * fix: Handling JSON serialization/response interception at ErrorHandlingHttpClient (#462) * fix: Handling JSON serialization and response interception at ErrorHandlingHttpClient * fix: Removing redundant method override header * feat: Added new error codes for IdP management and multitenancy (#458) * feat: Added new error codes for IdP management and multitenancy * fix: Updated integration tests * fix: Renamed helper method * fix: Removing some calls to deprecated APIs (#464) * chore: Support for specifying query parameters in HttpRequestInfo (#463) * chore: Support for specifying query parameters in HttpRequestInfo * fix: Removing redundant JsonObjectParser from HttpClient * fix: Fixing a series of javadoc warnings (#466) * fix: Made some APIs on AbstractFirebaseAuth.Builder package-protected for consistency * Apply suggestions from code review Co-authored-by: egilmorez <[email protected]> Co-authored-by: Kevin Cheung <[email protected]> * fix: Minor updates to API ref docs based on code review comments * fix: Fixing API doc wording Co-authored-by: Horatiu Lazu <[email protected]> Co-authored-by: egilmorez <[email protected]> Co-authored-by: Kevin Cheung <[email protected]>
1 parent 36eb4ea commit cc520a7

File tree

124 files changed

+4687
-1797
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+4687
-1797
lines changed

pom.xml

-6
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,6 @@
477477
<version>0.6</version>
478478
<scope>test</scope>
479479
</dependency>
480-
<dependency>
481-
<groupId>com.cedarsoftware</groupId>
482-
<artifactId>java-util</artifactId>
483-
<version>1.26.0</version>
484-
<scope>test</scope>
485-
</dependency>
486480
<dependency>
487481
<groupId>junit</groupId>
488482
<artifactId>junit</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2020 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase;
18+
19+
/**
20+
* Platform-wide error codes that can be raised by Admin SDK APIs.
21+
*/
22+
public enum ErrorCode {
23+
24+
/**
25+
* Client specified an invalid argument.
26+
*/
27+
INVALID_ARGUMENT,
28+
29+
/**
30+
* Request cannot be executed in the current system state, such as deleting a non-empty
31+
* directory.
32+
*/
33+
FAILED_PRECONDITION,
34+
35+
/**
36+
* Client specified an invalid range.
37+
*/
38+
OUT_OF_RANGE,
39+
40+
/**
41+
* Request not authenticated due to missing, invalid, or expired OAuth token.
42+
*/
43+
UNAUTHENTICATED,
44+
45+
/**
46+
* Client does not have sufficient permission. This can happen because the OAuth token does
47+
* not have the right scopes, the client doesn't have permission, or the API has not been
48+
* enabled for the client project.
49+
*/
50+
PERMISSION_DENIED,
51+
52+
/**
53+
* A specified resource is not found, or the request is rejected for unknown reasons,
54+
* such as a blocked network address.
55+
*/
56+
NOT_FOUND,
57+
58+
/**
59+
* Concurrency conflict, such as read-modify-write conflict.
60+
*/
61+
CONFLICT,
62+
63+
/**
64+
* Concurrency conflict, such as read-modify-write conflict.
65+
*/
66+
ABORTED,
67+
68+
/**
69+
* The resource that a client tried to create already exists.
70+
*/
71+
ALREADY_EXISTS,
72+
73+
/**
74+
* Either out of resource quota or rate limited.
75+
*/
76+
RESOURCE_EXHAUSTED,
77+
78+
/**
79+
* Request cancelled by the client.
80+
*/
81+
CANCELLED,
82+
83+
/**
84+
* Unrecoverable data loss or data corruption. The client should report the error to the user.
85+
*/
86+
DATA_LOSS,
87+
88+
/**
89+
* Unknown server error. Typically a server bug.
90+
*/
91+
UNKNOWN,
92+
93+
/**
94+
* Internal server error. Typically a server bug.
95+
*/
96+
INTERNAL,
97+
98+
/**
99+
* Service unavailable. Typically the server is down.
100+
*/
101+
UNAVAILABLE,
102+
103+
/**
104+
* Request deadline exceeded. This happens only if the caller sets a deadline that is
105+
* shorter than the method's default deadline (i.e. requested deadline is not enough for the
106+
* server to process the request) and the request did not finish within the deadline.
107+
*/
108+
DEADLINE_EXCEEDED,
109+
}

src/main/java/com/google/firebase/FirebaseApp.java

+10-32
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
import com.google.common.base.Joiner;
3535
import com.google.common.base.MoreObjects;
3636
import com.google.common.base.Strings;
37-
import com.google.common.base.Supplier;
3837
import com.google.common.collect.ImmutableList;
39-
import com.google.firebase.internal.FirebaseAppStore;
4038
import com.google.firebase.internal.FirebaseScheduledExecutor;
4139
import com.google.firebase.internal.FirebaseService;
4240
import com.google.firebase.internal.ListenableFuture2ApiFuture;
@@ -48,10 +46,8 @@
4846
import java.util.ArrayList;
4947
import java.util.Collections;
5048
import java.util.HashMap;
51-
import java.util.HashSet;
5249
import java.util.List;
5350
import java.util.Map;
54-
import java.util.Set;
5551
import java.util.concurrent.Callable;
5652
import java.util.concurrent.Future;
5753
import java.util.concurrent.ScheduledExecutorService;
@@ -121,7 +117,6 @@ private FirebaseApp(String name, FirebaseOptions options, TokenRefresher.Factory
121117

122118
/** Returns a list of all FirebaseApps. */
123119
public static List<FirebaseApp> getApps() {
124-
// TODO: reenable persistence. See b/28158809.
125120
synchronized (appsLock) {
126121
return ImmutableList.copyOf(instances.values());
127122
}
@@ -221,21 +216,16 @@ public static FirebaseApp initializeApp(FirebaseOptions options, String name) {
221216

222217
static FirebaseApp initializeApp(FirebaseOptions options, String name,
223218
TokenRefresher.Factory tokenRefresherFactory) {
224-
FirebaseAppStore appStore = FirebaseAppStore.initialize();
225219
String normalizedName = normalize(name);
226-
final FirebaseApp firebaseApp;
227220
synchronized (appsLock) {
228221
checkState(
229222
!instances.containsKey(normalizedName),
230223
"FirebaseApp name " + normalizedName + " already exists!");
231224

232-
firebaseApp = new FirebaseApp(normalizedName, options, tokenRefresherFactory);
225+
FirebaseApp firebaseApp = new FirebaseApp(normalizedName, options, tokenRefresherFactory);
233226
instances.put(normalizedName, firebaseApp);
227+
return firebaseApp;
234228
}
235-
236-
appStore.persistApp(firebaseApp);
237-
238-
return firebaseApp;
239229
}
240230

241231
@VisibleForTesting
@@ -251,19 +241,13 @@ static void clearInstancesForTest() {
251241
}
252242

253243
private static List<String> getAllAppNames() {
254-
Set<String> allAppNames = new HashSet<>();
244+
List<String> allAppNames;
255245
synchronized (appsLock) {
256-
for (FirebaseApp app : instances.values()) {
257-
allAppNames.add(app.getName());
258-
}
259-
FirebaseAppStore appStore = FirebaseAppStore.getInstance();
260-
if (appStore != null) {
261-
allAppNames.addAll(appStore.getAllPersistedAppNames());
262-
}
246+
allAppNames = new ArrayList<>(instances.keySet());
263247
}
264-
List<String> sortedNameList = new ArrayList<>(allAppNames);
265-
Collections.sort(sortedNameList);
266-
return sortedNameList;
248+
249+
Collections.sort(allAppNames);
250+
return ImmutableList.copyOf(allAppNames);
267251
}
268252

269253
/** Normalizes the app name. */
@@ -359,11 +343,6 @@ public void delete() {
359343
synchronized (appsLock) {
360344
instances.remove(name);
361345
}
362-
363-
FirebaseAppStore appStore = FirebaseAppStore.getInstance();
364-
if (appStore != null) {
365-
appStore.removeApp(name);
366-
}
367346
}
368347

369348
private void checkNotDeleted() {
@@ -582,18 +561,17 @@ enum State {
582561
private static FirebaseOptions getOptionsFromEnvironment() throws IOException {
583562
String defaultConfig = System.getenv(FIREBASE_CONFIG_ENV_VAR);
584563
if (Strings.isNullOrEmpty(defaultConfig)) {
585-
return new FirebaseOptions.Builder()
564+
return FirebaseOptions.builder()
586565
.setCredentials(APPLICATION_DEFAULT_CREDENTIALS)
587566
.build();
588567
}
589568
JsonFactory jsonFactory = Utils.getDefaultJsonFactory();
590-
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
569+
FirebaseOptions.Builder builder = FirebaseOptions.builder();
591570
JsonParser parser;
592571
if (defaultConfig.startsWith("{")) {
593572
parser = jsonFactory.createJsonParser(defaultConfig);
594573
} else {
595-
FileReader reader;
596-
reader = new FileReader(defaultConfig);
574+
FileReader reader = new FileReader(defaultConfig);
597575
parser = jsonFactory.createJsonParser(reader);
598576
}
599577
parser.parseAndClose(builder);

src/main/java/com/google/firebase/FirebaseAppLifecycleListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* A listener which gets notified when {@link com.google.firebase.FirebaseApp} gets deleted.
2121
*/
22-
// TODO: consider making it public in a future release.
22+
@Deprecated
2323
interface FirebaseAppLifecycleListener {
2424

2525
/**

src/main/java/com/google/firebase/FirebaseException.java

+41-10
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,55 @@
1717
package com.google.firebase;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
2021

2122
import com.google.common.base.Strings;
2223
import com.google.firebase.internal.NonNull;
24+
import com.google.firebase.internal.Nullable;
2325

24-
/** Base class for all Firebase exceptions. */
26+
/**
27+
* Base class for all Firebase exceptions.
28+
*/
2529
public class FirebaseException extends Exception {
2630

27-
// TODO(b/27677218): Exceptions should have non-empty messages.
28-
@Deprecated
29-
protected FirebaseException() {}
31+
private final ErrorCode errorCode;
32+
private final IncomingHttpResponse httpResponse;
33+
34+
public FirebaseException(
35+
@NonNull ErrorCode errorCode,
36+
@NonNull String message,
37+
@Nullable Throwable cause,
38+
@Nullable IncomingHttpResponse httpResponse) {
39+
super(message, cause);
40+
checkArgument(!Strings.isNullOrEmpty(message), "Message must not be null or empty");
41+
this.errorCode = checkNotNull(errorCode, "ErrorCode must not be null");
42+
this.httpResponse = httpResponse;
43+
}
44+
45+
public FirebaseException(
46+
@NonNull ErrorCode errorCode,
47+
@NonNull String message,
48+
@Nullable Throwable cause) {
49+
this(errorCode, message, cause, null);
50+
}
3051

31-
public FirebaseException(@NonNull String detailMessage) {
32-
super(detailMessage);
33-
checkArgument(!Strings.isNullOrEmpty(detailMessage), "Detail message must not be empty");
52+
/**
53+
* Returns the platform-wide error code associated with this exception.
54+
*
55+
* @return A Firebase error code.
56+
*/
57+
public final ErrorCode getErrorCode() {
58+
return errorCode;
3459
}
3560

36-
public FirebaseException(@NonNull String detailMessage, Throwable cause) {
37-
super(detailMessage, cause);
38-
checkArgument(!Strings.isNullOrEmpty(detailMessage), "Detail message must not be empty");
61+
/**
62+
* Returns the HTTP response that resulted in this exception. If the exception was not caused by
63+
* an HTTP error response, returns null.
64+
*
65+
* @return An HTTP response or null.
66+
*/
67+
@Nullable
68+
public final IncomingHttpResponse getHttpResponse() {
69+
return httpResponse;
3970
}
4071
}

src/main/java/com/google/firebase/FirebaseOptions.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ public static Builder builder() {
223223
return new Builder();
224224
}
225225

226+
/**
227+
* Creates a new {@code Builder} from the options object.
228+
*
229+
* <p>The new builder is not backed by this object's values; that is, changes made to the new
230+
* builder don't change the values of the origin object.
231+
*/
232+
public Builder toBuilder() {
233+
return new Builder(this);
234+
}
235+
226236
/**
227237
* Builder for constructing {@link FirebaseOptions}.
228238
*/
@@ -249,15 +259,23 @@ public static final class Builder {
249259
private int connectTimeout;
250260
private int readTimeout;
251261

252-
/** Constructs an empty builder. */
262+
/**
263+
* Constructs an empty builder.
264+
*
265+
* @deprecated Use {@link FirebaseOptions#builder()} instead.
266+
*/
267+
@Deprecated
253268
public Builder() {}
254269

255270
/**
256271
* Initializes the builder's values from the options object.
257272
*
258273
* <p>The new builder is not backed by this object's values, that is changes made to the new
259274
* builder don't change the values of the origin object.
275+
*
276+
* @deprecated Use {@link FirebaseOptions#toBuilder()} instead.
260277
*/
278+
@Deprecated
261279
public Builder(FirebaseOptions options) {
262280
databaseUrl = options.databaseUrl;
263281
storageBucket = options.storageBucket;

0 commit comments

Comments
 (0)