@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (this.statusMapping.containsKey(statusCode)) { extract(this.statusMapping.get(statusCode), response); } else if (this.seriesMapping.containsKey(statusCode.series())) { extract(this.seriesMapping.get(statusCode.series()), response); } else { super.handleError(response, statusCode); } }
@Before public void setup() throws Exception { HttpMessageConverter<Object> converter = new MappingJackson2HttpMessageConverter(); this.errorHandler = new ExtractingResponseErrorHandler( Collections.singletonList(converter)); this.errorHandler.setStatusMapping( Collections.singletonMap(HttpStatus.I_AM_A_TEAPOT, MyRestClientException.class)); this.errorHandler.setSeriesMapping(Collections .singletonMap(HttpStatus.Series.SERVER_ERROR, MyRestClientException.class)); }
@Test public void hasErrorOverride() throws Exception { this.errorHandler.setSeriesMapping(Collections .singletonMap(HttpStatus.Series.CLIENT_ERROR, null)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.I_AM_A_TEAPOT.value()); assertTrue(this.errorHandler.hasError(this.response)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); assertFalse(this.errorHandler.hasError(this.response)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); assertFalse(this.errorHandler.hasError(this.response)); }
@Test public void handleErrorStatusMatch() throws Exception { given(this.response.getRawStatusCode()).willReturn(HttpStatus.I_AM_A_TEAPOT.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); try { this.errorHandler.handleError(this.response); fail("MyRestClientException expected"); } catch (MyRestClientException ex) { assertEquals("bar", ex.getFoo()); } }
@Test public void handleNoMatchOverride() throws Exception { this.errorHandler.setSeriesMapping(Collections .singletonMap(HttpStatus.Series.CLIENT_ERROR, null)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); this.errorHandler.handleError(this.response); }
@Test public void hasError() throws Exception { given(this.response.getRawStatusCode()).willReturn(HttpStatus.I_AM_A_TEAPOT.value()); assertTrue(this.errorHandler.hasError(this.response)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR.value()); assertTrue(this.errorHandler.hasError(this.response)); given(this.response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); assertFalse(this.errorHandler.hasError(this.response)); }
@Test public void handleErrorSeriesMatch() throws Exception { given(this.response.getRawStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); try { this.errorHandler.handleError(this.response); fail("MyRestClientException expected"); } catch (MyRestClientException ex) { assertEquals("bar", ex.getFoo()); } }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (this.statusMapping.containsKey(statusCode)) { extract(this.statusMapping.get(statusCode), response); } else if (this.seriesMapping.containsKey(statusCode.series())) { extract(this.seriesMapping.get(statusCode.series()), response); } else { super.handleError(response, statusCode); } }
@Test public void handleNoMatch() throws Exception { given(this.response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value()); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); given(this.response.getHeaders()).willReturn(responseHeaders); byte[] body = "{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8); responseHeaders.setContentLength(body.length); given(this.response.getBody()).willReturn(new ByteArrayInputStream(body)); try { this.errorHandler.handleError(this.response); fail("HttpClientErrorException expected"); } catch (HttpClientErrorException ex) { assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode()); assertArrayEquals(body, ex.getResponseBodyAsByteArray()); } }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (this.statusMapping.containsKey(statusCode)) { extract(this.statusMapping.get(statusCode), response); } else if (this.seriesMapping.containsKey(statusCode.series())) { extract(this.seriesMapping.get(statusCode.series()), response); } else { super.handleError(response, statusCode); } }
@Override public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { // 异常处理入口:记录url,method super.handleError(url, method, response); }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (this.statusMapping.containsKey(statusCode)) { extract(this.statusMapping.get(statusCode), response); } else if (this.seriesMapping.containsKey(statusCode.series())) { extract(this.seriesMapping.get(statusCode.series()), response); } else { super.handleError(response, statusCode); } }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (statusCode.series() == HttpStatus.Series.CLIENT_ERROR) { throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } else if (statusCode.series() == HttpStatus.Series.SERVER_ERROR) { throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } else { try { HttpStatus.Series.valueOf(statusCode); super.handleError(response, statusCode); } catch (Exception e) { throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } } }