/** * Update query and header parameters based on authentication settings. * * @param authNames The authentications to apply * @param queryParams The query parameters * @param headerParams The header parameters */ private void updateParamsForAuth(String[] authNames, MultiValueMap<String, String> queryParams, HttpHeaders headerParams) { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { throw new RestClientException("Authentication undefined: " + authName); } auth.applyToParams(queryParams, headerParams); } }
@Override public <T> T execute(URI url, HttpMethod method, @Nullable RequestCallback callback, @Nullable ResponseExtractor<T> extractor) throws RestClientException { try { extractor.extractData(this.responses.remove()); } catch (Throwable t) { throw new RestClientException("Failed to invoke extractor", t); } return null; } }
throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]");
} else { throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", ex); throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]");
@Test void rest_client_exception_is_propagated() { template = mock(RestTemplate.class); when(template.getForObject(any(URI.class), any())).thenThrow(new RestClientException("mock")); assertThrows(RestClientException.class, () -> cache.getUrlContent(uri, template)); }
void terminateSpan(@Nullable ClientResponse clientResponse, @Nullable Throwable throwable) { if (clientResponse == null || clientResponse.statusCode() == null) { if (log.isDebugEnabled()) { log.debug("No response was returned. Will close the span [" + this.span + "]"); } handleReceive(this.span, this.ws, clientResponse, throwable); return; } boolean error = clientResponse.statusCode().is4xxClientError() || clientResponse.statusCode().is5xxServerError(); if (error) { if (log.isDebugEnabled()) { log.debug( "Non positive status code was returned from the call. Will close the span [" + this.span + "]"); } throwable = new RestClientException("Status code of the response is [" + clientResponse.statusCode().value() + "] and the reason is [" + clientResponse.statusCode().getReasonPhrase() + "]"); } handleReceive(this.span, this.ws, clientResponse, throwable); }
message += " and content type [" + requestContentType + "]"; throw new RestClientException(message);
message += " and content type [" + requestContentType + "]"; throw new RestClientException(message);
@Override public void handleError(ClientHttpResponse response) throws IOException { final HttpStatus statusCode = getHttpStatusCode(response); switch (statusCode.series()) { case CLIENT_ERROR: throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); default: throw new RestClientException("Unknown status code [" + statusCode + "]"); } }
private RestClientException fail(final Class<?> type, @Nullable final MediaType contentType) { final String message = format( "Could not write request: no suitable HttpMessageConverter found for request type [%s]", type.getName()); if (contentType == null) { return new RestClientException(message); } else { return new RestClientException(format("%s and content type [%s]", message, contentType)); } }
private RestClientException fail(final Class<?> type, @Nullable final MediaType contentType) { final String message = format( "Could not write request: no suitable HttpMessageConverter found for request type [%s]", type.getName()); if (contentType == null) { return new RestClientException(message); } else { return new RestClientException(format("%s and content type [%s]", message, contentType)); } }
@Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = response.getStatusCode(); switch (statusCode.series()) { case CLIENT_ERROR: case SERVER_ERROR: throw getException(response); default: throw new RestClientException("Unknown status code [" + statusCode + "]"); } }
@Test public void ifRestTemplateFailsThenReturnEmptyVersion() { when(mockRestTemplate.exchange(eq("https://raw.githubusercontent.com/linuxserver/davos/LatestRelease/version.txt"), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class))).thenThrow(new RestClientException("")); Version version = settingsService.retrieveRemoteVersion(); assertThat(version.toString()).isEqualTo("0.0.0"); } }
@Test public void ifRestTemplateFailsThenDoNothing() { when(mockRestTemplate.exchange(eq("https://api.pushbullet.com/v2/pushes"), eq(HttpMethod.POST), any(HttpEntity.class), eq(Object.class))).thenThrow(new RestClientException("")); pushbulletNotifyAction.execute(new PostDownloadExecution()); }
@Test public void ifRestTemplateFailsThenDoNothing() { PostDownloadExecution execution = new PostDownloadExecution(); execution.fileName = "filename"; when(mockRestTemplate.exchange(eq("http://url"), eq(HttpMethod.POST), any(HttpEntity.class), eq(Object.class))) .thenThrow(new RestClientException("")); httpAPICallAction.execute(execution); }
@Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = response.getStatusCode(); switch (statusCode.series()) { case CLIENT_ERROR: throw getException(response); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText()); default: throw new RestClientException("Unknown status code [" + statusCode + "]"); } }
@Test public void shouldReturnEmptyOptionalIfWeatherServiceIsUnavailable() throws Exception { given(restTemplate.getForObject("http://localhost:8089/someAppId/53.5511,9.9937", WeatherResponse.class)) .willThrow(new RestClientException("something went wrong")); Optional<WeatherResponse> actualResponse = subject.fetchWeather(); assertThat(actualResponse, is(Optional.empty())); }
@Test(expected = PayFailException.class) public void customerService를_Mocking_하면_실패한다() { // given RequestDto requestDto = new RequestDto(); given(restTemplate.postForEntity(CustomerService.PAY_URL, requestDto, String.class)) .willThrow(new RestClientException("결제실패")); doNothing() .when(customerService) .alarm(requestDto); // when customerService.pay(requestDto); }
@Test(expected = PayFailException.class) public void restTemplate만_Mocking하면_실패한다() { // given RequestDto requestDto = new RequestDto(); given(restTemplate.postForEntity(CustomerService.PAY_URL, requestDto, String.class)) .willThrow(new RestClientException("결제실패")); // when customerService.pay(requestDto); }