Exception GoogleJsonResponseException (1.35.2)

public class GoogleJsonResponseException extends HttpResponseException

Exception thrown when an error status code is detected in an HTTP response to a Google API that uses the JSON format, using the format specified in Error Responses.

To execute a request, call #execute(JsonFactory, HttpRequest). This will throw a GoogleJsonResponseException on an error response. To get the structured details, use #getDetails().


 static void executeShowingError(JsonFactory factory, HttpRequest request) throws IOException {
   try {
     GoogleJsonResponseException.execute(factory, request);
   } catch (GoogleJsonResponseException e) {
     System.err.println(e.getDetails());
   }
 }
 

Static Methods

execute(JsonFactory jsonFactory, HttpRequest request)

public static HttpResponse execute(JsonFactory jsonFactory, HttpRequest request)

Executes an HTTP request using HttpRequest#execute(), but throws a GoogleJsonResponseException on error instead of HttpResponseException.

Callers should call HttpResponse#disconnect when the returned HTTP response object is no longer needed. However, HttpResponse#disconnect does not have to be called if the response stream is properly closed. Example usage:


 HttpResponse response = GoogleJsonResponseException.execute(jsonFactory, request);
 try {
   // process the HTTP response object
 } finally {
   response.disconnect();
 }
 
Parameters
NameDescription
jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

requestcom.google.api.client.http.HttpRequest

HTTP request

Returns
TypeDescription
com.google.api.client.http.HttpResponse

HTTP response for an HTTP success code (or error code if HttpRequest#getThrowExceptionOnExecuteError())

Exceptions
TypeDescription

for an HTTP error code (only if not HttpRequest#getThrowExceptionOnExecuteError())

IOException

for an HTTP error code (only if not HttpRequest#getThrowExceptionOnExecuteError())

from(JsonFactory jsonFactory, HttpResponse response)

public static GoogleJsonResponseException from(JsonFactory jsonFactory, HttpResponse response)

Returns a new instance of GoogleJsonResponseException.

If there is a JSON error response, it is parsed using GoogleJsonError, which can be inspected using #getDetails(). Otherwise, the full response content is read and included in the exception message.

Parameters
NameDescription
jsonFactorycom.google.api.client.json.JsonFactory

JSON factory

responsecom.google.api.client.http.HttpResponse

HTTP response

Returns
TypeDescription
GoogleJsonResponseException

new instance of GoogleJsonResponseException

Constructors

GoogleJsonResponseException(HttpResponseException.Builder builder, GoogleJsonError details)

public GoogleJsonResponseException(HttpResponseException.Builder builder, GoogleJsonError details)
Parameters
NameDescription
buildercom.google.api.client.http.HttpResponseException.Builder

builder

detailsGoogleJsonError

Google JSON error details

Methods

getDetails()

public final GoogleJsonError getDetails()

Returns the Google JSON error details or null for none (for example if response is not JSON).

Returns
TypeDescription
GoogleJsonError