Repository navigation
Added core types for the error handling revamp #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright 2020 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase; | ||
|
|
||
| /** | ||
| * Platform-wide error codes that can be raised by Admin SDK APIs. | ||
| */ | ||
| public enum ErrorCode { | ||
|
|
||
| /** | ||
| * Client specified an invalid argument. | ||
| */ | ||
| INVALID_ARGUMENT, | ||
|
|
||
| /** | ||
| * Request can not be executed in the current system state, such as deleting a non-empty | ||
| * directory. | ||
| */ | ||
| FAILED_PRECONDITION, | ||
|
|
||
| /** | ||
| * Client specified an invalid range. | ||
| */ | ||
| OUT_OF_RANGE, | ||
|
|
||
| /** | ||
| * Request not authenticated due to missing, invalid, or expired OAuth token. | ||
| */ | ||
| UNAUTHENTICATED, | ||
|
|
||
| /** | ||
| * Client does not have sufficient permission. This can happen because the OAuth token does | ||
| * not have the right scopes, the client doesn't have permission, or the API has not been | ||
| * enabled for the client project. | ||
| */ | ||
| PERMISSION_DENIED, | ||
|
|
||
| /** | ||
| * A specified resource is not found, or the request is rejected by undisclosed reasons, | ||
| * such as whitelisting. | ||
| */ | ||
| NOT_FOUND, | ||
|
|
||
| /** | ||
| * Concurrency conflict, such as read-modify-write conflict. | ||
| */ | ||
| CONFLICT, | ||
|
|
||
| /** | ||
| * Concurrency conflict, such as read-modify-write conflict. | ||
| */ | ||
| ABORTED, | ||
|
|
||
| /** | ||
| * The resource that a client tried to create already exists. | ||
| */ | ||
| ALREADY_EXISTS, | ||
|
|
||
| /** | ||
| * Either out of resource quota or reaching rate limiting. | ||
| */ | ||
| RESOURCE_EXHAUSTED, | ||
|
|
||
| /** | ||
| * Request cancelled by the client. | ||
| */ | ||
| CANCELLED, | ||
|
|
||
| /** | ||
| * Unrecoverable data loss or data corruption. The client should report the error to the user. | ||
| */ | ||
| DATA_LOSS, | ||
|
|
||
| /** | ||
| * Unknown server error. Typically a server bug. | ||
| */ | ||
| UNKNOWN, | ||
|
|
||
| /** | ||
| * Internal server error. Typically a server bug. | ||
| */ | ||
| INTERNAL, | ||
|
|
||
| /** | ||
| * Service unavailable. Typically the server is down. | ||
| */ | ||
| UNAVAILABLE, | ||
|
|
||
| /** | ||
| * Request deadline exceeded. This will happen only if the caller sets a deadline that is | ||
| * shorter than the method's default deadline (i.e. requested deadline is not enough for the | ||
| * server to process the request) and the request did not finish within the deadline. | ||
| */ | ||
| DEADLINE_EXCEEDED, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/com/google/firebase/IncomingHttpResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright 2020 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.client.http.HttpRequest; | ||
| import com.google.api.client.http.HttpResponse; | ||
| import com.google.api.client.http.HttpResponseException; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.firebase.database.annotations.Nullable; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Contains the information that describe an HTTP response received by the SDK. | ||
| */ | ||
| public final class IncomingHttpResponse { | ||
|
|
||
| private final int statusCode; | ||
| private final String content; | ||
| private final Map<String, Object> headers; | ||
| private final OutgoingHttpRequest request; | ||
|
|
||
| IncomingHttpResponse(HttpResponse response, @Nullable String content) { | ||
| checkNotNull(response, "response must not be null"); | ||
| this.statusCode = response.getStatusCode(); | ||
| this.content = content; | ||
| this.headers = ImmutableMap.copyOf(response.getHeaders()); | ||
| this.request = new OutgoingHttpRequest(response.getRequest()); | ||
| } | ||
|
|
||
| IncomingHttpResponse(HttpResponseException e, HttpRequest request) { | ||
| this(e, new OutgoingHttpRequest(request)); | ||
| } | ||
|
|
||
| IncomingHttpResponse(HttpResponseException e, OutgoingHttpRequest request) { | ||
| checkNotNull(e, "exception must not be null"); | ||
| this.statusCode = e.getStatusCode(); | ||
| this.content = e.getContent(); | ||
| this.headers = ImmutableMap.copyOf(e.getHeaders()); | ||
| this.request = checkNotNull(request, "request must not be null"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the status code of the response. | ||
| * | ||
| * @return An HTTP status code (e.g. 500). | ||
| */ | ||
| public int getStatusCode() { | ||
| return this.statusCode; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the content of the response as a string. | ||
| * | ||
| * @return HTTP content or null. | ||
| */ | ||
| @Nullable | ||
| public String getContent() { | ||
| return this.content; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the headers set on the response. | ||
| * | ||
| * @return An immutable map of headers (possibly empty). | ||
| */ | ||
| public Map<String, Object> getHeaders() { | ||
| return this.headers; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the request that resulted in this response. | ||
| * | ||
| * @return An HTTP request. | ||
| */ | ||
| public OutgoingHttpRequest getRequest() { | ||
| return request; | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/google/firebase/OutgoingHttpRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright 2020 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.client.http.HttpContent; | ||
| import com.google.api.client.http.HttpRequest; | ||
| import com.google.common.base.Strings; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.firebase.internal.Nullable; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Contains the information that describe an HTTP request made by the SDK. | ||
| */ | ||
| public final class OutgoingHttpRequest { | ||
|
|
||
| private final String method; | ||
| private final String url; | ||
| private final HttpContent content; | ||
| private final Map<String, Object> headers; | ||
|
|
||
| OutgoingHttpRequest(String method, String url) { | ||
| checkArgument(!Strings.isNullOrEmpty(method), "method must not be null or empty"); | ||
| checkArgument(!Strings.isNullOrEmpty(url), "url must not be empty"); | ||
| this.method = method; | ||
| this.url = url; | ||
| this.content = null; | ||
| this.headers = ImmutableMap.of(); | ||
| } | ||
|
|
||
| OutgoingHttpRequest(HttpRequest request) { | ||
| checkNotNull(request, "request must not be null"); | ||
| this.method = request.getRequestMethod(); | ||
| this.url = request.getUrl().toString(); | ||
| this.content = request.getContent(); | ||
| this.headers = ImmutableMap.copyOf(request.getHeaders()); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the HTTP method of the request. | ||
| * | ||
| * @return An HTTP method string (e.g. GET). | ||
| */ | ||
| public String getMethod() { | ||
| return method; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the URL of the request. | ||
| * | ||
| * @return An absolute HTTP URL. | ||
| */ | ||
| public String getUrl() { | ||
| return url; | ||
| } | ||
|
|
||
| /** | ||
| * Returns any content that was sent with the request. | ||
| * | ||
| * @return HTTP content or null. | ||
| */ | ||
| @Nullable | ||
| public HttpContent getContent() { | ||
| return content; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the headers set on the request. | ||
| * | ||
| * @return An immutable map of headers (possibly empty). | ||
| */ | ||
| public Map<String, Object> getHeaders() { | ||
| return headers; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure, concurrency conflict and aborted are caused by the same reasons?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CONFLICTis really an error code we made up. This is because HTTP 409 can be mapped to bothABORTEDandALREADY_EXISTS: https://cloud.google.com/apis/design/errors#handling_errorsTo mitigate this ambiguity we introduced an error code
CONFLICT, so that if we only have the HTTP status code to identify an error condition, we will map it toCONFLICTinstead of one of the other two.