use of com.google.api.client.http.HttpResponseException in project copybara by google.
the class GitHubApiTransportImpl method post.
@SuppressWarnings("unchecked")
@Override
public <T> T post(String path, Object request, Type responseType) throws RepoException, ValidationException {
HttpRequestFactory requestFactory = getHttpRequestFactory(getCredentials());
GenericUrl url = new GenericUrl(URI.create(API_URL + "/" + path));
try {
HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(JSON_FACTORY, request));
HttpResponse response = httpRequest.execute();
return (T) response.parseAs(responseType);
} catch (HttpResponseException e) {
try {
throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "POST", path, JSON_FACTORY.toPrettyString(request), e.getContent());
} catch (IOException e1) {
logger.log(Level.SEVERE, "Error serializing request for error", e1);
throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "POST", path, "unknown request", e.getContent());
}
} catch (IOException e) {
throw new RepoException("Error running GitHub API operation " + path, e);
}
}
use of com.google.api.client.http.HttpResponseException in project copybara by google.
the class GitHubApiTransportImpl method get.
@SuppressWarnings("unchecked")
@Override
public <T> T get(String path, Type responseType) throws RepoException, ValidationException {
HttpRequestFactory requestFactory = getHttpRequestFactory(getCredentialsIfPresent());
GenericUrl url = new GenericUrl(URI.create(API_URL + "/" + path));
try {
HttpRequest httpRequest = requestFactory.buildGetRequest(url);
HttpResponse response = httpRequest.execute();
return (T) response.parseAs(responseType);
} catch (HttpResponseException e) {
throw new GitHubApiException(e.getStatusCode(), parseErrorOrIgnore(e), "GET", path, null, e.getContent());
} catch (IOException e) {
throw new RepoException("Error running GitHub API operation " + path, e);
}
}
use of com.google.api.client.http.HttpResponseException in project kork by spinnaker.
the class StackdriverWriter method writeRegistryHelper.
/**
* Implementation of writeRegistry wrapped for timing.
*/
private void writeRegistryHelper(Registry registry) {
MonitoredResource resource = determineMonitoredResource();
if (resource == null) {
log.warn("Cannot determine the managed resource - not flushing metrics.");
return;
}
List<TimeSeries> tsList = registryToTimeSeries(registry);
if (tsList.isEmpty()) {
log.debug("No metric data points.");
return;
}
CreateTimeSeriesRequest tsRequest = new CreateTimeSeriesRequest();
int offset = 0;
int failed = 0;
List<TimeSeries> nextN;
log.debug("Writing metrics...");
while (offset < tsList.size()) {
if (offset + MAX_TS_PER_REQUEST < tsList.size()) {
nextN = tsList.subList(offset, offset + MAX_TS_PER_REQUEST);
offset += MAX_TS_PER_REQUEST;
} else {
nextN = tsList.subList(offset, tsList.size());
offset = tsList.size();
}
tsRequest.setTimeSeries(nextN);
try {
service.projects().timeSeries().create(projectResourceName, tsRequest).execute();
} catch (HttpResponseException rex) {
handleTimeSeriesResponseException(rex, "creating time series", nextN);
failed += nextN.size();
} catch (IOException ioex) {
log.error("Caught Exception creating time series " + ioex);
failed += nextN.size();
}
}
log.debug("Wrote {} values", tsList.size() - failed);
}
use of com.google.api.client.http.HttpResponseException in project jib by google.
the class BlobCheckerTest method testHandleHttpResponseException_invalidStatusCode.
@Test
public void testHandleHttpResponseException_invalidStatusCode() throws RegistryErrorException {
HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
Mockito.when(mockHttpResponseException.getStatusCode()).thenReturn(-1);
try {
testBlobChecker.handleHttpResponseException(mockHttpResponseException);
Assert.fail("Non-404 status codes should not be handled");
} catch (HttpResponseException ex) {
Assert.assertEquals(mockHttpResponseException, ex);
}
}
use of com.google.api.client.http.HttpResponseException in project jib by google.
the class BlobCheckerTest method testHandleHttpResponseException_hasOtherErrors.
@Test
public void testHandleHttpResponseException_hasOtherErrors() throws IOException, RegistryErrorException {
HttpResponseException mockHttpResponseException = Mockito.mock(HttpResponseException.class);
Mockito.when(mockHttpResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate().addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message")).addError(new ErrorEntryTemplate(ErrorCodes.MANIFEST_UNKNOWN.name(), "some message"));
Mockito.when(mockHttpResponseException.getContent()).thenReturn(Blobs.writeToString(JsonTemplateMapper.toBlob(emptyErrorResponseTemplate)));
try {
testBlobChecker.handleHttpResponseException(mockHttpResponseException);
Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
} catch (HttpResponseException ex) {
Assert.assertEquals(mockHttpResponseException, ex);
}
}
Aggregations