-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSpannerException.java
More file actions
247 lines (223 loc) · 8.09 KB
/
Copy pathSpannerException.java
File metadata and controls
247 lines (223 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2017 Google LLC
*
* 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.cloud.spanner;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.ErrorDetails;
import com.google.cloud.grpc.BaseGrpcServiceException;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.protobuf.util.Durations;
import com.google.rpc.ResourceInfo;
import com.google.rpc.RetryInfo;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.protobuf.ProtoUtils;
import java.util.Map;
import javax.annotation.Nullable;
/** Base exception type for all exceptions produced by the Cloud Spanner service. */
public class SpannerException extends BaseGrpcServiceException {
/** Base exception type for NOT_FOUND exceptions for known resource types. */
public abstract static class ResourceNotFoundException extends SpannerException {
private final ResourceInfo resourceInfo;
ResourceNotFoundException(
DoNotConstructDirectly token,
@Nullable String message,
ResourceInfo resourceInfo,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(token, ErrorCode.NOT_FOUND, /* retryable */ false, message, cause, apiException);
this.resourceInfo = resourceInfo;
}
public String getResourceName() {
return resourceInfo.getResourceName();
}
}
private static final long serialVersionUID = 20150916L;
private static final Metadata.Key<RetryInfo> KEY_RETRY_INFO =
ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
private static final String PG_ERR_CODE_KEY = "pg_sqlerrcode";
private final ErrorCode code;
private final ApiException apiException;
private final XGoogSpannerRequestId requestId;
private String statement;
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
SpannerException(
DoNotConstructDirectly token,
ErrorCode code,
boolean retryable,
@Nullable String message,
@Nullable Throwable cause) {
this(token, code, retryable, message, cause, null);
}
/** Private constructor. Use {@link SpannerExceptionFactory} to create instances. */
SpannerException(
DoNotConstructDirectly token,
ErrorCode code,
boolean retryable,
@Nullable String message,
@Nullable Throwable cause,
@Nullable ApiException apiException) {
super(message, cause, code.getCode(), retryable);
if (token != DoNotConstructDirectly.ALLOWED) {
throw new AssertionError("Do not construct directly: use SpannerExceptionFactory");
}
this.code = Preconditions.checkNotNull(code);
this.apiException = apiException;
this.requestId = extractRequestId(cause);
}
@Override
public String getMessage() {
if (this.statement == null) {
return super.getMessage();
}
return String.format("%s - Statement: '%s'", super.getMessage(), this.statement);
}
@Override
public String toString() {
if (this.requestId == null) {
return super.toString();
}
return super.toString() + " - RequestId: " + this.requestId;
}
/** Returns the error code associated with this exception. */
public ErrorCode getErrorCode() {
return code;
}
/**
* Returns the PostgreSQL SQLState error code that is encoded in this exception, or null if this
* {@link SpannerException} does not include a PostgreSQL error code.
*/
public String getPostgreSQLErrorCode() {
ErrorDetails details = getErrorDetails();
if (details == null || details.getErrorInfo() == null) {
return null;
}
return details.getErrorInfo().getMetadataOrDefault(PG_ERR_CODE_KEY, null);
}
public String getRequestId() {
if (requestId == null) {
return "";
}
return requestId.toString();
}
enum DoNotConstructDirectly {
ALLOWED
}
/**
* Return the retry delay for operation in milliseconds. Return -1 if this does not specify any
* retry delay.
*/
public long getRetryDelayInMillis() {
return extractRetryDelay(this, this.apiException);
}
static long extractRetryDelay(Throwable cause) {
return extractRetryDelay(cause, null);
}
static long extractRetryDelay(Throwable cause, @Nullable ApiException apiException) {
ErrorDetails details = SpannerExceptionFactory.extractErrorDetails(cause, apiException);
if (details != null && details.getRetryInfo() != null) {
RetryInfo retryInfo = details.getRetryInfo();
if (retryInfo.hasRetryDelay()) {
return Durations.toMillis(retryInfo.getRetryDelay());
}
}
if (cause != null) {
Metadata trailers = Status.trailersFromThrowable(cause);
if (trailers != null && trailers.containsKey(KEY_RETRY_INFO)) {
RetryInfo retryInfo = trailers.get(KEY_RETRY_INFO);
if (retryInfo != null && retryInfo.hasRetryDelay()) {
return Durations.toMillis(retryInfo.getRetryDelay());
}
}
}
return -1L;
}
@Nullable
static XGoogSpannerRequestId extractRequestId(Throwable cause) {
if (cause != null) {
Metadata trailers = Status.trailersFromThrowable(cause);
if (trailers != null && trailers.containsKey(XGoogSpannerRequestId.REQUEST_ID_HEADER_KEY)) {
String requestId = trailers.get(XGoogSpannerRequestId.REQUEST_ID_HEADER_KEY);
if (!Strings.isNullOrEmpty(requestId)) {
return XGoogSpannerRequestId.of(requestId);
}
}
}
return null;
}
/**
* Checks the underlying reason of the exception and if it's {@link ApiException} then return the
* reason otherwise null.
*
* @see <a
* href="https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L117">Reason</a>
* @return the reason of an error.
*/
public String getReason() {
if (this.apiException != null) {
return this.apiException.getReason();
}
return null;
}
/**
* Checks the underlying reason of the exception and if it's {@link ApiException} then return the
* specific domain otherwise null.
*
* @see <a
* href="https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L125">Domain</a>
* @return the logical grouping to which the "reason" belongs.
*/
public String getDomain() {
if (this.apiException != null) {
return this.apiException.getDomain();
}
return null;
}
/**
* Checks the underlying reason of the exception and if it's {@link ApiException} then return a
* map of key-value pairs otherwise null.
*
* @see <a
* href="https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto#L135">Metadata</a>
* @return the map of additional structured details about an error.
*/
public Map<String, String> getMetadata() {
if (this.apiException != null) {
return this.apiException.getMetadata();
}
return null;
}
/**
* Checks the underlying reason of the exception and if it's {@link ApiException} then return the
* ErrorDetails otherwise null.
*
* @see <a
* href="https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto">Status</a>
* @see <a
* href="https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto">Error
* Details</a>
* @return An object containing getters for structured objects from error_details.proto.
*/
public ErrorDetails getErrorDetails() {
if (this.apiException != null) {
return this.apiException.getErrorDetails();
}
return SpannerExceptionFactory.extractErrorDetails(getCause(), null);
}
void setStatement(String statement) {
this.statement = statement;
}
}