|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2022 the original author or authors. |
|
|
|
* Copyright 2002-2024 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -131,15 +131,17 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { |
|
|
|
* {@link HttpStatus} enum range. |
|
|
|
* {@link HttpStatus} enum range. |
|
|
|
* </ul> |
|
|
|
* </ul> |
|
|
|
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code |
|
|
|
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code |
|
|
|
* @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode) |
|
|
|
* @see #handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void handleError(ClientHttpResponse response) throws IOException { |
|
|
|
public void handleError(ClientHttpResponse response) throws IOException { |
|
|
|
handleError(null, null, response); |
|
|
|
HttpStatusCode statusCode = response.getStatusCode(); |
|
|
|
|
|
|
|
handleError(response, statusCode, null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Handle the error in the given response with the given resolved status code. |
|
|
|
* Handle the error in the given response with the given resolved status code |
|
|
|
|
|
|
|
* and extra information providing access to the request URL and HTTP method. |
|
|
|
* <p>The default implementation throws: |
|
|
|
* <p>The default implementation throws: |
|
|
|
* <ul> |
|
|
|
* <ul> |
|
|
|
* <li>{@link HttpClientErrorException} if the status code is in the 4xx |
|
|
|
* <li>{@link HttpClientErrorException} if the status code is in the 4xx |
|
|
|
@ -152,58 +154,51 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { |
|
|
|
* {@link HttpStatus} enum range. |
|
|
|
* {@link HttpStatus} enum range. |
|
|
|
* </ul> |
|
|
|
* </ul> |
|
|
|
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code |
|
|
|
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code |
|
|
|
* @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode) |
|
|
|
* @since 6.2 |
|
|
|
|
|
|
|
* @see #handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { |
|
|
|
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException { |
|
|
|
HttpStatusCode statusCode = response.getStatusCode(); |
|
|
|
HttpStatusCode statusCode = response.getStatusCode(); |
|
|
|
handleError(url, method, response, statusCode); |
|
|
|
handleError(response, statusCode, url, method); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Return error message with details from the response body. For example: |
|
|
|
* Return error message with details from the response body. For example: |
|
|
|
* <pre> |
|
|
|
* <pre> |
|
|
|
* 404 Not Found: [{'id': 123, 'message': 'my message'}] |
|
|
|
* 404 Not Found on GET request for "https://example.com": [{'id': 123, 'message': 'my message'}] |
|
|
|
* </pre> |
|
|
|
* </pre> |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private String getErrorMessage( |
|
|
|
private String getErrorMessage(int rawStatusCode, String statusText, @Nullable byte[] responseBody, @Nullable Charset charset, |
|
|
|
int rawStatusCode, String statusText, @Nullable byte[] responseBody, @Nullable Charset charset, @Nullable URI url, @Nullable HttpMethod method) { |
|
|
|
@Nullable URI url, @Nullable HttpMethod method) { |
|
|
|
|
|
|
|
|
|
|
|
String preface = getPreface(rawStatusCode, statusText, url, method); |
|
|
|
StringBuilder msg = new StringBuilder(rawStatusCode + " " + statusText); |
|
|
|
|
|
|
|
if (method != null) { |
|
|
|
|
|
|
|
msg.append(" on ").append(method).append(" request"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (url != null) { |
|
|
|
|
|
|
|
msg.append(" for \""); |
|
|
|
|
|
|
|
String urlString = url.toString(); |
|
|
|
|
|
|
|
int idx = urlString.indexOf('?'); |
|
|
|
|
|
|
|
if (idx != -1) { |
|
|
|
|
|
|
|
msg.append(urlString, 0, idx); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
msg.append(urlString); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
msg.append("\""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
msg.append(": "); |
|
|
|
if (ObjectUtils.isEmpty(responseBody)) { |
|
|
|
if (ObjectUtils.isEmpty(responseBody)) { |
|
|
|
return preface + "[no body]"; |
|
|
|
msg.append("[no body]"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
charset = (charset != null ? charset : StandardCharsets.UTF_8); |
|
|
|
charset = (charset != null ? charset : StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
String bodyText = new String(responseBody, charset); |
|
|
|
String bodyText = new String(responseBody, charset); |
|
|
|
bodyText = LogFormatUtils.formatValue(bodyText, -1, true); |
|
|
|
bodyText = LogFormatUtils.formatValue(bodyText, -1, true); |
|
|
|
msg.append(bodyText); |
|
|
|
|
|
|
|
|
|
|
|
return preface + bodyText; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getPreface(int rawStatusCode, String statusText, @Nullable URI url, @Nullable HttpMethod method) { |
|
|
|
|
|
|
|
StringBuilder preface = new StringBuilder(rawStatusCode + " " + statusText); |
|
|
|
|
|
|
|
if (!ObjectUtils.isEmpty(method) && !ObjectUtils.isEmpty(url)) { |
|
|
|
|
|
|
|
preface.append(" after ").append(method).append(" ").append(url).append(" "); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
preface.append(": "); |
|
|
|
return msg.toString(); |
|
|
|
return preface.toString(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Handle the error based on the resolved status code. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>The default implementation delegates to |
|
|
|
|
|
|
|
* {@link HttpClientErrorException#create} for errors in the 4xx range, to |
|
|
|
|
|
|
|
* {@link HttpServerErrorException#create} for errors in the 5xx range, |
|
|
|
|
|
|
|
* or otherwise raises {@link UnknownHttpStatusCodeException}. |
|
|
|
|
|
|
|
* @since 5.0 |
|
|
|
|
|
|
|
* @see HttpClientErrorException#create |
|
|
|
|
|
|
|
* @see HttpServerErrorException#create |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode) throws IOException { |
|
|
|
|
|
|
|
handleError(null, null, response, statusCode); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -213,12 +208,11 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { |
|
|
|
* {@link HttpClientErrorException#create} for errors in the 4xx range, to |
|
|
|
* {@link HttpClientErrorException#create} for errors in the 4xx range, to |
|
|
|
* {@link HttpServerErrorException#create} for errors in the 5xx range, |
|
|
|
* {@link HttpServerErrorException#create} for errors in the 5xx range, |
|
|
|
* or otherwise raises {@link UnknownHttpStatusCodeException}. |
|
|
|
* or otherwise raises {@link UnknownHttpStatusCodeException}. |
|
|
|
* @since 5.0 |
|
|
|
* @since 6.2 |
|
|
|
* @see HttpClientErrorException#create |
|
|
|
* @see HttpClientErrorException#create |
|
|
|
* @see HttpServerErrorException#create |
|
|
|
* @see HttpServerErrorException#create |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void handleError(@Nullable URI url, @Nullable HttpMethod method, ClientHttpResponse response, |
|
|
|
protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode, @Nullable URI url, @Nullable HttpMethod method) throws IOException { |
|
|
|
HttpStatusCode statusCode) throws IOException { |
|
|
|
|
|
|
|
String statusText = response.getStatusText(); |
|
|
|
String statusText = response.getStatusText(); |
|
|
|
HttpHeaders headers = response.getHeaders(); |
|
|
|
HttpHeaders headers = response.getHeaders(); |
|
|
|
byte[] body = getResponseBody(response); |
|
|
|
byte[] body = getResponseBody(response); |
|
|
|
|