public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) : new HttpResponseException(command, response); message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), response.getStatusLine()); switch (response.getStatusCode()) { case 400: break; case 401: case 403: exception = new AuthorizationException(message, exception); break; case 404: if (!command.getCurrentRequest().getMethod().equals("DELETE")) { exception = new ResourceNotFoundException(message, exception); } break; } command.setException(exception); } }
public Void apply(Exception from) { HttpResponseException exception = Throwables2.getFirstThrowableOfType(from, HttpResponseException.class); if (exception != null && exception.getCommand().getCurrentRequest().getMethod().equals(HttpMethod.DELETE) && exception.getResponse().getStatusCode() == 302) { return null; } throw Throwables.propagate(from); } }
@Override public Void createOrPropagate(Throwable t) throws Exception { if (checkNotNull(t, "throwable") instanceof HttpResponseException) { HttpResponseException hre = HttpResponseException.class.cast(t); if (hre.getResponse().getStatusCode() == 503 || hre.getResponse().getStatusCode() == 401 || MESSAGE_PATTERN.matcher(hre.getMessage()).matches()) return null; } else if (t instanceof AuthorizationException) { return null; } throw propagate(t); } }
@Test(expectedExceptions = ResourceNotFoundException.class) public void test404ToResourceNotFoundException() throws Exception { fn.create(new HttpResponseException(command, HttpResponse.builder().statusCode(404).build())); }
@Test public void testMaximumRetriesExceeded() throws InterruptedException, ExecutionException { beginToFailOnRequestNumber = 0; endFailuresOnRequestNumber = 6; requestCount = 0; try { submitGetRequest(); fail("Request should not succeed within " + endFailuresOnRequestNumber + " requests"); } catch (HttpResponseException e) { assertEquals(e.getResponse().getStatusCode(), 500); } }
public void handleError(HttpCommand command, HttpResponse from) { String content; try { content = from.getPayload() != null ? Strings2.toString(from.getPayload()) : null; command.setException(new HttpResponseException(command, from, content)); } catch (IOException e) { command.setException(new HttpResponseException(command, from)); } finally { releasePayload(from); } } }
public static HttpResponseException returnResponseException(int code) { HttpResponse response = HttpResponse.builder().statusCode(code).build(); return new HttpResponseException(new HttpCommand(HttpRequest.builder().method("GET").endpoint("http://stub") .build()), response); }
public void handleError(HttpCommand command, HttpResponse response) { // it is important to always read fully and close streams byte[] data = closeClientButKeepContentStream(response); Exception exception; if (data == null) { exception = new HttpResponseException(command, response); } else { exception = new HttpResponseException(command, response, new String(data)); } switch (response.getStatusCode()) { case 401: exception = new AuthorizationException(exception.getMessage(), exception); break; case 409: exception = new IllegalStateException(exception.getMessage(), exception); break; } command.setException(exception); } }
@Override public T apply(final HttpResponse from) { InputStream xml = from.getPayload().getInput(); try { return apply(xml); } catch (Exception e) { StringBuilder message = new StringBuilder(); message.append("Error parsing input"); logger.error(e, message.toString()); throw new HttpResponseException(message.toString() + "\n" + from, null, from, e); } finally { releasePayload(from); } }
return; } catch (HttpResponseException hre) { HttpResponse hr = hre.getResponse(); if (hr == null) { response.sendError( return; int status = hr.getStatusCode(); switch (status) { case 416:
public void test404() { Exception from = new HttpResponseException("message", null, HttpResponse.builder().statusCode(404).message("not found").build()); assertEquals(returnValueOnCodeOrNull(from, true, equalTo(404)), Boolean.TRUE); }
public void testNullResponse() { Exception from = new HttpResponseException("message", null, null); assertEquals(returnValueOnCodeOrNull(from, true, equalTo(404)), null); } }
@Override public ListenableFuture<Object> create(Throwable t) { if (!(t instanceof HttpResponseException)) throw propagate(t); HttpResponse response = HttpResponseException.class.cast(t).getResponse(); if (response == null) return immediateFuture(null); // https://tools.ietf.org/html/rfc2616#section-14.37 String retryAfter = response.getFirstHeaderOrNull(HttpHeaders.RETRY_AFTER); if (retryAfter != null) { Optional<RetryAfterException> retryException = tryCreateRetryAfterException(t, retryAfter); if (retryException.isPresent()) throw retryException.get(); } return immediateFuture(null); }
@Override public boolean isMappable(HttpResponseException exception) { return exception.getResponse() != null; } }
private void putBlobWithRetries(Blob blob, int retries) { for (int i = 1; i <= retries; i++) { try { blobStore.putBlob(container, blob); return; } catch (HttpResponseException e) { LOGGER.warn(MessageFormat.format(Messages.BLOB_STORE_PUT_BLOB_FAILED, e.getMessage()), e); if (i == retries) { throw e; } } sleep(i * RETRY_BASE_WAIT_TIME_IN_MILLIS); } }
System.err.println(exception.getMessage()); exception.printStackTrace(); } finally {
@Test(expectedExceptions = AuthorizationException.class) public void test401ToAuthorizationException() throws Exception { fn.create(new HttpResponseException(command, HttpResponse.builder().statusCode(401).build())); }
@Test(groups = { "integration", "live" }) public void deleteObjectNoContainer() { try { view.getBlobStore().removeBlob("donb", "test"); } catch (HttpResponseException e) { assertEquals(e.getResponse().getStatusCode(), 404); } catch (ContainerNotFoundException e) { } }
public void handleError(HttpCommand command, HttpResponse from) { String content; try { content = from.getPayload() != null ? Strings2.toString(from.getPayload()) : null; command.setException(new HttpResponseException(command, from, content)); } catch (IOException e) { command.setException(new HttpResponseException(command, from)); } finally { releasePayload(from); } } }
public static HttpResponseException returnResponseException(int code) { HttpResponse response = HttpResponse.builder().statusCode(code).build(); return new HttpResponseException(new HttpCommand(HttpRequest.builder().method("GET").endpoint("http://stub") .build()), response); }