Java Code Examples for com.google.api.client.googleapis.json.GoogleJsonResponseException#from()
The following examples show how to use
com.google.api.client.googleapis.json.GoogleJsonResponseException#from() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DirectoryGroupsConnectionTest.java From nomulus with Apache License 2.0 | 6 votes |
/** Returns a valid GoogleJsonResponseException for the given status code and error message. */ private GoogleJsonResponseException makeResponseException( final int statusCode, final String message) throws Exception { HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) { return new MockLowLevelHttpRequest() { @Override public LowLevelHttpResponse execute() { MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); response.setStatusCode(statusCode); response.setContentType(Json.MEDIA_TYPE); response.setContent(String.format( "{\"error\":{\"code\":%d,\"message\":\"%s\",\"domain\":\"global\"," + "\"reason\":\"duplicate\"}}", statusCode, message)); return response; }}; }}; HttpRequest request = transport.createRequestFactory() .buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL) .setThrowExceptionOnExecuteError(false); return GoogleJsonResponseException.from(new JacksonFactory(), request.execute()); }
Example 2
Source File: CoreSocketFactoryTest.java From cloud-sql-jdbc-socket-factory with Apache License 2.0 | 5 votes |
private static GoogleJsonResponseException fakeGoogleJsonResponseException( int status, ErrorInfo errorInfo, String message) throws IOException { final JsonFactory jsonFactory = new JacksonFactory(); HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { errorInfo.setFactory(jsonFactory); GoogleJsonError jsonError = new GoogleJsonError(); jsonError.setCode(status); jsonError.setErrors(Collections.singletonList(errorInfo)); jsonError.setMessage(message); jsonError.setFactory(jsonFactory); GenericJson errorResponse = new GenericJson(); errorResponse.set("error", jsonError); errorResponse.setFactory(jsonFactory); return new MockLowLevelHttpRequest() .setResponse( new MockLowLevelHttpResponse() .setContent(errorResponse.toPrettyString()) .setContentType(Json.MEDIA_TYPE) .setStatusCode(status)); } }; HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); request.setThrowExceptionOnExecuteError(false); HttpResponse response = request.execute(); return GoogleJsonResponseException.from(jsonFactory, response); }
Example 3
Source File: GcsUtilTest.java From beam with Apache License 2.0 | 5 votes |
/** Builds a fake GoogleJsonResponseException for testing API error handling. */ private static GoogleJsonResponseException googleJsonResponseException( final int status, final String reason, final String message) throws IOException { final JsonFactory jsonFactory = new JacksonFactory(); HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { ErrorInfo errorInfo = new ErrorInfo(); errorInfo.setReason(reason); errorInfo.setMessage(message); errorInfo.setFactory(jsonFactory); GenericJson error = new GenericJson(); error.set("code", status); error.set("errors", Arrays.asList(errorInfo)); error.setFactory(jsonFactory); GenericJson errorResponse = new GenericJson(); errorResponse.set("error", error); errorResponse.setFactory(jsonFactory); return new MockLowLevelHttpRequest() .setResponse( new MockLowLevelHttpResponse() .setContent(errorResponse.toPrettyString()) .setContentType(Json.MEDIA_TYPE) .setStatusCode(status)); } }; HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); request.setThrowExceptionOnExecuteError(false); HttpResponse response = request.execute(); return GoogleJsonResponseException.from(jsonFactory, response); }
Example 4
Source File: PackageUtilTest.java From beam with Apache License 2.0 | 5 votes |
/** Builds a fake GoogleJsonResponseException for testing API error handling. */ private static GoogleJsonResponseException googleJsonResponseException( final int status, final String reason, final String message) throws IOException { final JsonFactory jsonFactory = new JacksonFactory(); HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { ErrorInfo errorInfo = new ErrorInfo(); errorInfo.setReason(reason); errorInfo.setMessage(message); errorInfo.setFactory(jsonFactory); GenericJson error = new GenericJson(); error.set("code", status); error.set("errors", Arrays.asList(errorInfo)); error.setFactory(jsonFactory); GenericJson errorResponse = new GenericJson(); errorResponse.set("error", error); errorResponse.setFactory(jsonFactory); return new MockLowLevelHttpRequest() .setResponse( new MockLowLevelHttpResponse() .setContent(errorResponse.toPrettyString()) .setContentType(Json.MEDIA_TYPE) .setStatusCode(status)); } }; HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); request.setThrowExceptionOnExecuteError(false); HttpResponse response = request.execute(); return GoogleJsonResponseException.from(jsonFactory, response); }
Example 5
Source File: ApiErrorExtractorTest.java From hadoop-connectors with Apache License 2.0 | 5 votes |
private static GoogleJsonResponseException googleJsonResponseException( int status, ErrorInfo errorInfo, String httpStatusString) throws IOException { final JsonFactory jsonFactory = new JacksonFactory(); HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { errorInfo.setFactory(jsonFactory); GoogleJsonError jsonError = new GoogleJsonError(); jsonError.setCode(status); jsonError.setErrors(Collections.singletonList(errorInfo)); jsonError.setMessage(httpStatusString); jsonError.setFactory(jsonFactory); GenericJson errorResponse = new GenericJson(); errorResponse.set("error", jsonError); errorResponse.setFactory(jsonFactory); return new MockLowLevelHttpRequest() .setResponse( new MockLowLevelHttpResponse() .setContent(errorResponse.toPrettyString()) .setContentType(Json.MEDIA_TYPE) .setStatusCode(status)); } }; HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); request.setThrowExceptionOnExecuteError(false); HttpResponse response = request.execute(); return GoogleJsonResponseException.from(jsonFactory, response); }
Example 6
Source File: AbstractGoogleJsonClientRequest.java From google-api-java-client with Apache License 2.0 | 4 votes |
@Override protected GoogleJsonResponseException newExceptionOnError(HttpResponse response) { return GoogleJsonResponseException.from(getAbstractGoogleClient().getJsonFactory(), response); }
Example 7
Source File: GoogleJsonResponseExceptionFactoryTesting.java From google-api-java-client with Apache License 2.0 | 4 votes |
/** * Convenience factory method that builds a {@link GoogleJsonResponseException} * from its arguments. The method builds a dummy {@link HttpRequest} and * {@link HttpResponse}, sets the response's status to a user-specified HTTP * error code, suppresses exceptions, and executes the request. This forces * the underlying framework to create, but not throw, a * {@link GoogleJsonResponseException}, which the method retrieves and returns * to the invoker. * * @param jsonFactory the JSON factory that will create all JSON required * by the underlying framework * @param httpCode the desired HTTP error code. Note: do nut specify any codes * that indicate successful completion, e.g. 2XX. * @param reasonPhrase the HTTP reason code that explains the error. For example, * if {@code httpCode} is {@code 404}, the reason phrase should be * {@code NOT FOUND}. * @return the generated {@link GoogleJsonResponseException}, as specified. * @throws IOException if request transport fails. */ public static GoogleJsonResponseException newMock(JsonFactory jsonFactory, int httpCode, String reasonPhrase) throws IOException { MockLowLevelHttpResponse otherServiceUnavaiableLowLevelResponse = new MockLowLevelHttpResponse() .setStatusCode(httpCode) .setReasonPhrase(reasonPhrase) .setContentType(Json.MEDIA_TYPE) .setContent("{ \"error\": { \"errors\": [ { \"reason\": \"" + reasonPhrase + "\" } ], " + "\"code\": " + httpCode + " } }"); MockHttpTransport otherTransport = new MockHttpTransport.Builder() .setLowLevelHttpResponse(otherServiceUnavaiableLowLevelResponse) .build(); HttpRequest otherRequest = otherTransport .createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); otherRequest.setThrowExceptionOnExecuteError(false); HttpResponse otherServiceUnavailableResponse = otherRequest.execute(); return GoogleJsonResponseException.from(jsonFactory, otherServiceUnavailableResponse); }
Example 8
Source File: GoogleJsonResponseExceptionHelper.java From java-monitoring-client-library with Apache License 2.0 | 2 votes |
/** * @param statusCode the status code that should be in the returned {@link * GoogleJsonResponseException} * @return a {@link GoogleJsonResponseException} with the status code {@code statusCode} * @throws IOException shouldn't occur */ public static GoogleJsonResponseException create(int statusCode) throws IOException { HttpResponse response = createHttpResponse(statusCode, null); return GoogleJsonResponseException.from(new JacksonFactory(), response); }
Example 9
Source File: GoogleJsonResponseExceptionHelper.java From nomulus with Apache License 2.0 | 2 votes |
/** * @param statusCode the status code that should be in the returned {@link * GoogleJsonResponseException} * @return a {@link GoogleJsonResponseException} with the status code {@code statusCode} * @throws IOException shouldn't occur */ public static GoogleJsonResponseException create(int statusCode) throws IOException { HttpResponse response = createHttpResponse(statusCode, null); return GoogleJsonResponseException.from(new JacksonFactory(), response); }