@@ -86,20 +86,32 @@ public int hashCode() {
8686 private final boolean retryable ;
8787 private final String reason ;
8888 private final boolean idempotent ;
89+ private final String location ;
90+ private final String debugInfo ;
8991
9092 public BaseServiceException (IOException exception , boolean idempotent ) {
9193 super (message (exception ), exception );
94+ int code = UNKNOWN_CODE ;
95+ String reason = null ;
96+ String location = null ;
97+ String debugInfo = null ;
9298 if (exception instanceof GoogleJsonResponseException ) {
93- Error error = error (((GoogleJsonResponseException ) exception ).getDetails ());
94- this .code = error .code ;
95- this .reason = error .reason ;
96- this .retryable = error .isRetryable (retryableErrors ());
97- } else {
98- this .code = UNKNOWN_CODE ;
99- this .reason = null ;
100- this .retryable = idempotent && isRetryable (exception );
99+ GoogleJsonError jsonError = ((GoogleJsonResponseException ) exception ).getDetails ();
100+ Error error = error (jsonError );
101+ code = error .code ;
102+ reason = error .reason ;
103+ if (reason != null ) {
104+ GoogleJsonError .ErrorInfo errorInfo = jsonError .getErrors ().get (0 );
105+ location = errorInfo .getLocation ();
106+ debugInfo = (String ) errorInfo .get ("debugInfo" );
107+ }
101108 }
109+ this .code = code ;
110+ this .retryable = idempotent && isRetryable (exception );
111+ this .reason = reason ;
102112 this .idempotent = idempotent ;
113+ this .location = location ;
114+ this .debugInfo = debugInfo ;
103115 }
104116
105117 public BaseServiceException (GoogleJsonError error , boolean idempotent ) {
@@ -108,6 +120,8 @@ public BaseServiceException(GoogleJsonError error, boolean idempotent) {
108120 this .reason = reason (error );
109121 this .idempotent = idempotent ;
110122 this .retryable = idempotent && isRetryable (error );
123+ this .location = null ;
124+ this .debugInfo = null ;
111125 }
112126
113127 public BaseServiceException (int code , String message , String reason , boolean idempotent ) {
@@ -121,6 +135,8 @@ public BaseServiceException(int code, String message, String reason, boolean ide
121135 this .reason = reason ;
122136 this .idempotent = idempotent ;
123137 this .retryable = idempotent && new Error (code , reason ).isRetryable (retryableErrors ());
138+ this .location = null ;
139+ this .debugInfo = null ;
124140 }
125141
126142 protected Set <Error > retryableErrors () {
@@ -166,6 +182,18 @@ public boolean idempotent() {
166182 return idempotent ;
167183 }
168184
185+ /**
186+ * Returns the service location where the error causing the exception occurred. Returns
187+ * {@code null} if not set.
188+ */
189+ public String location () {
190+ return location ;
191+ }
192+
193+ protected String debugInfo () {
194+ return debugInfo ;
195+ }
196+
169197 protected static String reason (GoogleJsonError error ) {
170198 if (error .getErrors () != null && !error .getErrors ().isEmpty ()) {
171199 return error .getErrors ().get (0 ).getReason ();
0 commit comments