Java Code Examples for com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo#setReason()

The following examples show how to use com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo#setReason() . 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: ApiErrorExtractorTest.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  accessDenied =
      googleJsonResponseException(
          HttpStatusCodes.STATUS_CODE_FORBIDDEN, "Forbidden", "Forbidden");
  statusOk = googleJsonResponseException(HttpStatusCodes.STATUS_CODE_OK, "A reason", "ok");
  notFound =
      googleJsonResponseException(
          HttpStatusCodes.STATUS_CODE_NOT_FOUND, "Not found", "Not found");
  badRange =
      googleJsonResponseException(
          ApiErrorExtractor.STATUS_CODE_RANGE_NOT_SATISFIABLE, "Bad range", "Bad range");
  alreadyExists = googleJsonResponseException(409, "409", "409");
  resourceNotReady =
      googleJsonResponseException(
          400, ApiErrorExtractor.RESOURCE_NOT_READY_REASON, "Resource not ready");

  // This works because googleJsonResponseException takes final ErrorInfo
  ErrorInfo errorInfo = new ErrorInfo();
  errorInfo.setReason(ApiErrorExtractor.RATE_LIMITED_REASON);
  notRateLimited = googleJsonResponseException(POSSIBLE_RATE_LIMIT, errorInfo, "");
  errorInfo.setDomain(ApiErrorExtractor.USAGE_LIMITS_DOMAIN);
  rateLimited = googleJsonResponseException(POSSIBLE_RATE_LIMIT, errorInfo, "");
  errorInfo.setDomain(ApiErrorExtractor.GLOBAL_DOMAIN);
  bigqueryRateLimited = googleJsonResponseException(POSSIBLE_RATE_LIMIT, errorInfo, "");
}
 
Example 2
Source File: CoreSocketFactoryTest.java    From cloud-sql-jdbc-socket-factory with Apache License 2.0 5 votes vote down vote up
private static GoogleJsonResponseException fakeGoogleJsonResponseException(
    int httpStatus, String reason, String message) throws IOException {
  ErrorInfo errorInfo = new ErrorInfo();
  errorInfo.setReason(reason);
  errorInfo.setMessage(message);
  return fakeGoogleJsonResponseException(httpStatus, errorInfo, message);
}
 
Example 3
Source File: GcsUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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: BigQueryServicesImplTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** A helper that generates the error JSON payload that Google APIs produce. */
private static GoogleJsonErrorContainer errorWithReasonAndStatus(String reason, int status) {
  ErrorInfo info = new ErrorInfo();
  info.setReason(reason);
  info.setDomain("global");
  // GoogleJsonError contains one or more ErrorInfo objects; our utiities read the first one.
  GoogleJsonError error = new GoogleJsonError();
  error.setErrors(ImmutableList.of(info));
  error.setCode(status);
  error.setMessage(reason);
  // The actual JSON response is an error container.
  GoogleJsonErrorContainer container = new GoogleJsonErrorContainer();
  container.setError(error);
  return container;
}
 
Example 5
Source File: PackageUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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 6
Source File: ApiErrorExtractorTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/** Builds a fake GoogleJsonResponseException for testing API error handling. */
private static GoogleJsonResponseException googleJsonResponseException(
    int httpStatus, String reason, String message, String httpStatusString) throws IOException {
  ErrorInfo errorInfo = new ErrorInfo();
  errorInfo.setReason(reason);
  errorInfo.setMessage(message);
  return googleJsonResponseException(httpStatus, errorInfo, httpStatusString);
}
 
Example 7
Source File: BatchRequestTest.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(ErrorOutput.ErrorBody e, HttpHeaders responseHeaders) throws IOException {
  GoogleJsonErrorContainer errorContainer = new GoogleJsonErrorContainer();

  if (e.hasError()) {
    ErrorOutput.ErrorProto errorProto = e.getError();

    GoogleJsonError error = new GoogleJsonError();
    if (errorProto.hasCode()) {
      error.setCode(errorProto.getCode());
    }
    if (errorProto.hasMessage()) {
      error.setMessage(errorProto.getMessage());
    }

    List<ErrorInfo> errorInfos = new ArrayList<ErrorInfo>(errorProto.getErrorsCount());
    for (ErrorOutput.IndividualError individualError : errorProto.getErrorsList()) {
      ErrorInfo errorInfo = new ErrorInfo();
      if (individualError.hasDomain()) {
        errorInfo.setDomain(individualError.getDomain());
      }
      if (individualError.hasMessage()) {
        errorInfo.setMessage(individualError.getMessage());
      }
      if (individualError.hasReason()) {
        errorInfo.setReason(individualError.getReason());
      }
      errorInfos.add(errorInfo);
    }
    error.setErrors(errorInfos);
    errorContainer.setError(error);
  }
  callback.onFailure(errorContainer, responseHeaders);
}