Refactor solution for respones error details
See gh-1956
This commit is contained in:
@@ -1,138 +0,0 @@
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.URI;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Spring's default implementation of the {@link HttpErrorDetailsExtractor} interface.
|
||||
*
|
||||
* <p>This extractor will compose a short summary of the http error response, including:
|
||||
* <ul>
|
||||
* <li>request URI
|
||||
* <li>request method
|
||||
* <li>a 200-character preview of the response body, unformatted
|
||||
* </ul>
|
||||
*
|
||||
* An example:
|
||||
* <pre>
|
||||
* 404 Not Found after GET http://example.com:8080/my-endpoint : [{'id': 123, 'message': 'my very long... (500 bytes)]</code>
|
||||
* </pre>
|
||||
*
|
||||
* @author Jerzy Krolak
|
||||
* @since 5.1
|
||||
* @see DefaultResponseErrorHandler#setHttpErrorDetailsExtractor(HttpErrorDetailsExtractor)
|
||||
*/
|
||||
public class DefaultHttpErrorDetailsExtractor implements HttpErrorDetailsExtractor {
|
||||
|
||||
private static final int MAX_BODY_BYTES_LENGTH = 400;
|
||||
|
||||
private static final int MAX_BODY_CHARS_LENGTH = 200;
|
||||
|
||||
/**
|
||||
* Assemble a short summary of the HTTP error response.
|
||||
* @param rawStatusCode HTTP status code
|
||||
* @param statusText HTTP status text
|
||||
* @param responseBody response body
|
||||
* @param responseCharset response charset
|
||||
* @param url request URI
|
||||
* @param method request method
|
||||
* @return error details string. Example: <pre>404 Not Found after GET http://example.com:8080/my-endpoint : [{'id': 123, 'message': 'my very long... (500 bytes)]</code></pre>
|
||||
*/
|
||||
@Override
|
||||
@NotNull
|
||||
public String getErrorDetails(int rawStatusCode, String statusText, @Nullable byte[] responseBody,
|
||||
@Nullable Charset responseCharset, @Nullable URI url, @Nullable HttpMethod method) {
|
||||
|
||||
if (url == null || method == null) {
|
||||
return getSimpleErrorDetails(rawStatusCode, statusText);
|
||||
}
|
||||
|
||||
return getCompleteErrorDetails(rawStatusCode, statusText, responseBody, responseCharset, url, method);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getCompleteErrorDetails(int rawStatusCode, String statusText, @Nullable byte[] responseBody,
|
||||
@Nullable Charset responseCharset, @Nullable URI url, @Nullable HttpMethod method) {
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
result.append(getSimpleErrorDetails(rawStatusCode, statusText))
|
||||
.append(" after ")
|
||||
.append(method)
|
||||
.append(" ")
|
||||
.append(url)
|
||||
.append(" : ");
|
||||
|
||||
if (responseBody == null || responseBody.length == 0) {
|
||||
result.append("[no body]");
|
||||
}
|
||||
else {
|
||||
result
|
||||
.append("[")
|
||||
.append(getResponseBody(responseBody, responseCharset))
|
||||
.append("]");
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getSimpleErrorDetails(int rawStatusCode, String statusText) {
|
||||
return rawStatusCode + " " + statusText;
|
||||
}
|
||||
|
||||
private String getResponseBody(byte[] responseBody, @Nullable Charset responseCharset) {
|
||||
Charset charset = getCharsetOrDefault(responseCharset);
|
||||
if (responseBody.length < MAX_BODY_BYTES_LENGTH) {
|
||||
return getCompleteResponseBody(responseBody, charset);
|
||||
}
|
||||
return getResponseBodyPreview(responseBody, charset);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getCompleteResponseBody(byte[] responseBody, Charset responseCharset) {
|
||||
return new String(responseBody, responseCharset);
|
||||
}
|
||||
|
||||
private String getResponseBodyPreview(byte[] responseBody, Charset responseCharset) {
|
||||
try {
|
||||
String bodyPreview = readBodyAsString(responseBody, responseCharset);
|
||||
return bodyPreview + "... (" + responseBody.length + " bytes)";
|
||||
}
|
||||
catch (IOException e) {
|
||||
// should never happen
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String readBodyAsString(byte[] responseBody, Charset responseCharset) throws IOException {
|
||||
|
||||
Reader reader = new InputStreamReader(new ByteArrayInputStream(responseBody), responseCharset);
|
||||
CharBuffer result = CharBuffer.allocate(MAX_BODY_CHARS_LENGTH);
|
||||
|
||||
reader.read(result);
|
||||
reader.close();
|
||||
result.flip();
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private Charset getCharsetOrDefault(@Nullable Charset responseCharset) {
|
||||
if (responseCharset == null) {
|
||||
return StandardCharsets.ISO_8859_1;
|
||||
}
|
||||
return responseCharset;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,18 +16,21 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Spring's default implementation of the {@link ResponseErrorHandler} interface.
|
||||
@@ -46,17 +49,6 @@ import org.springframework.util.FileCopyUtils;
|
||||
*/
|
||||
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
private HttpErrorDetailsExtractor httpErrorDetailsExtractor = new DefaultHttpErrorDetailsExtractor();
|
||||
|
||||
/**
|
||||
* Set the error summary extractor.
|
||||
* <p>By default, DefaultResponseErrorHandler uses a {@link DefaultHttpErrorDetailsExtractor}.
|
||||
*/
|
||||
public void setHttpErrorDetailsExtractor(HttpErrorDetailsExtractor httpErrorDetailsExtractor) {
|
||||
Assert.notNull(httpErrorDetailsExtractor, "HttpErrorDetailsExtractor must not be null");
|
||||
this.httpErrorDetailsExtractor = httpErrorDetailsExtractor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or
|
||||
* {@link #hasError(int)} (for an unknown status code) with the response status code.
|
||||
@@ -101,31 +93,58 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@link #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)} with the
|
||||
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
|
||||
* response status code.
|
||||
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
|
||||
* @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)
|
||||
* @see #handleError(ClientHttpResponse, HttpStatus)
|
||||
*/
|
||||
@Override
|
||||
public void handleError(ClientHttpResponse response) throws IOException {
|
||||
handleError(null, null, response);
|
||||
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
|
||||
if (statusCode == null) {
|
||||
String message = getErrorMessage(
|
||||
response.getRawStatusCode(), response.getStatusText(),
|
||||
getResponseBody(response), getCharset(response));
|
||||
throw new UnknownHttpStatusCodeException(message,
|
||||
response.getRawStatusCode(), response.getStatusText(),
|
||||
response.getHeaders(), getResponseBody(response), getCharset(response));
|
||||
}
|
||||
handleError(response, statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@link #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)} with the
|
||||
* response status code.
|
||||
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
|
||||
* @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)
|
||||
* Return error message with details from the response body, possibly truncated:
|
||||
* <pre>
|
||||
* 404 Not Found: [{'id': 123, 'message': 'my very long... (500 bytes)]
|
||||
* </pre>
|
||||
*/
|
||||
@Override
|
||||
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
|
||||
HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
|
||||
if (statusCode == null) {
|
||||
String message = httpErrorDetailsExtractor.getErrorDetails(response.getRawStatusCode(), response.getStatusText(), getResponseBody(response), getCharset(response), url, method);
|
||||
throw new UnknownHttpStatusCodeException(message, response.getRawStatusCode(), response.getStatusText(),
|
||||
response.getHeaders(), getResponseBody(response), getCharset(response), url, method);
|
||||
private String getErrorMessage(
|
||||
int rawStatusCode, String statusText, @Nullable byte[] responseBody, @Nullable Charset charset) {
|
||||
|
||||
String preface = rawStatusCode + " " + statusText + ": ";
|
||||
if (ObjectUtils.isEmpty(responseBody)) {
|
||||
return preface + "[no body]";
|
||||
}
|
||||
|
||||
charset = charset == null ? StandardCharsets.UTF_8 : charset;
|
||||
int maxChars = 200;
|
||||
|
||||
if (responseBody.length < maxChars * 2) {
|
||||
return preface + "[" + new String(responseBody, charset) + "]";
|
||||
}
|
||||
|
||||
try {
|
||||
Reader reader = new InputStreamReader(new ByteArrayInputStream(responseBody), charset);
|
||||
CharBuffer buffer = CharBuffer.allocate(maxChars);
|
||||
reader.read(buffer);
|
||||
reader.close();
|
||||
buffer.flip();
|
||||
return preface + "[" + buffer.toString() + "... (" + responseBody.length + " bytes)]";
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// should never happen
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
handleError(url, method, response, statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,34 +159,19 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
* @see HttpServerErrorException#create
|
||||
*/
|
||||
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
|
||||
handleError(null, null, response, statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the error in the given response with the given resolved status code.
|
||||
* <p>This default implementation throws a {@link HttpClientErrorException} if the response status code
|
||||
* is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
|
||||
* if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
|
||||
* and a {@link RestClientException} in other cases.
|
||||
* @since 5.0
|
||||
*/
|
||||
protected void handleError(@Nullable URI url, @Nullable HttpMethod method, ClientHttpResponse response,
|
||||
HttpStatus statusCode) throws IOException {
|
||||
|
||||
String statusText = response.getStatusText();
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
byte[] body = getResponseBody(response);
|
||||
Charset charset = getCharset(response);
|
||||
String message = httpErrorDetailsExtractor.getErrorDetails(statusCode.value(), statusText, body, charset, url, method);
|
||||
String message = getErrorMessage(statusCode.value(), statusText, body, charset);
|
||||
|
||||
switch (statusCode.series()) {
|
||||
case CLIENT_ERROR:
|
||||
throw HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset, url, method);
|
||||
throw HttpClientErrorException.create(message, statusCode, statusText, headers, body, charset);
|
||||
case SERVER_ERROR:
|
||||
throw HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset, url, method);
|
||||
throw HttpServerErrorException.create(message, statusCode, statusText, headers, body, charset);
|
||||
default:
|
||||
throw new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body,
|
||||
charset, url, method);
|
||||
throw new UnknownHttpStatusCodeException(message, statusCode.value(), statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -69,48 +67,84 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with a status code and status text, headers, content, request URL and method.
|
||||
* Constructor with a status code and status text, headers, and content,
|
||||
* and an prepared message.
|
||||
* @since 5.2.2
|
||||
*/
|
||||
public HttpClientErrorException(String message, HttpStatus statusCode, String statusText,
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset responseCharset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset responseCharset) {
|
||||
|
||||
super(message, statusCode, statusText, headers, body, responseCharset, url, method);
|
||||
super(message, statusCode, statusText, headers, body, responseCharset);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create {@code HttpClientErrorException} or an HTTP status specific sub-class.
|
||||
* @since 5.1
|
||||
*/
|
||||
public static HttpClientErrorException create(
|
||||
String message, HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body,
|
||||
@Nullable Charset charset, @Nullable URI url, @Nullable HttpMethod method) {
|
||||
HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
return create(null, statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #create(HttpStatus, String, HttpHeaders, byte[], Charset)}
|
||||
* with an optional prepared message.
|
||||
* @since 5.2.2
|
||||
*/
|
||||
public static HttpClientErrorException create(@Nullable String message, HttpStatus statusCode,
|
||||
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
switch (statusCode) {
|
||||
case BAD_REQUEST:
|
||||
return new HttpClientErrorException.BadRequest(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.BadRequest(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.BadRequest(statusText, headers, body, charset);
|
||||
case UNAUTHORIZED:
|
||||
return new HttpClientErrorException.Unauthorized(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.Unauthorized(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.Unauthorized(statusText, headers, body, charset);
|
||||
case FORBIDDEN:
|
||||
return new HttpClientErrorException.Forbidden(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.Forbidden(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.Forbidden(statusText, headers, body, charset);
|
||||
case NOT_FOUND:
|
||||
return new HttpClientErrorException.NotFound(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.NotFound(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.NotFound(statusText, headers, body, charset);
|
||||
case METHOD_NOT_ALLOWED:
|
||||
return new HttpClientErrorException.MethodNotAllowed(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.MethodNotAllowed(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.MethodNotAllowed(statusText, headers, body, charset);
|
||||
case NOT_ACCEPTABLE:
|
||||
return new HttpClientErrorException.NotAcceptable(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.NotAcceptable(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.NotAcceptable(statusText, headers, body, charset);
|
||||
case CONFLICT:
|
||||
return new HttpClientErrorException.Conflict(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.Conflict(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.Conflict(statusText, headers, body, charset);
|
||||
case GONE:
|
||||
return new HttpClientErrorException.Gone(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.Gone(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.Gone(statusText, headers, body, charset);
|
||||
case UNSUPPORTED_MEDIA_TYPE:
|
||||
return new HttpClientErrorException.UnsupportedMediaType(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.UnsupportedMediaType(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.UnsupportedMediaType(statusText, headers, body, charset);
|
||||
case TOO_MANY_REQUESTS:
|
||||
return new HttpClientErrorException.TooManyRequests(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.TooManyRequests(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.TooManyRequests(statusText, headers, body, charset);
|
||||
case UNPROCESSABLE_ENTITY:
|
||||
return new HttpClientErrorException.UnprocessableEntity(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException.UnprocessableEntity(message, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException.UnprocessableEntity(statusText, headers, body, charset);
|
||||
default:
|
||||
return new HttpClientErrorException(message, statusCode, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpClientErrorException(message, statusCode, statusText, headers, body, charset) :
|
||||
new HttpClientErrorException(statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,12 +156,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class BadRequest extends HttpClientErrorException {
|
||||
public static final class BadRequest extends HttpClientErrorException {
|
||||
|
||||
BadRequest(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private BadRequest(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.BAD_REQUEST, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.BAD_REQUEST, statusText, headers, body, charset, url, method);
|
||||
private BadRequest(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.BAD_REQUEST, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,12 +174,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class Unauthorized extends HttpClientErrorException {
|
||||
public static final class Unauthorized extends HttpClientErrorException {
|
||||
|
||||
Unauthorized(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private Unauthorized(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.UNAUTHORIZED, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.UNAUTHORIZED, statusText, headers, body, charset, url, method);
|
||||
private Unauthorized(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.UNAUTHORIZED, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,12 +192,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class Forbidden extends HttpClientErrorException {
|
||||
public static final class Forbidden extends HttpClientErrorException {
|
||||
|
||||
Forbidden(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private Forbidden(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.FORBIDDEN, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.FORBIDDEN, statusText, headers, body, charset, url, method);
|
||||
private Forbidden(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.FORBIDDEN, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,12 +210,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class NotFound extends HttpClientErrorException {
|
||||
public static final class NotFound extends HttpClientErrorException {
|
||||
|
||||
NotFound(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private NotFound(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.NOT_FOUND, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.NOT_FOUND, statusText, headers, body, charset, url, method);
|
||||
private NotFound(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.NOT_FOUND, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,12 +228,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class MethodNotAllowed extends HttpClientErrorException {
|
||||
public static final class MethodNotAllowed extends HttpClientErrorException {
|
||||
|
||||
MethodNotAllowed(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private MethodNotAllowed(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.METHOD_NOT_ALLOWED, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.METHOD_NOT_ALLOWED, statusText, headers, body, charset, url, method);
|
||||
private MethodNotAllowed(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.METHOD_NOT_ALLOWED, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,12 +246,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class NotAcceptable extends HttpClientErrorException {
|
||||
public static final class NotAcceptable extends HttpClientErrorException {
|
||||
|
||||
NotAcceptable(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private NotAcceptable(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.NOT_ACCEPTABLE, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.NOT_ACCEPTABLE, statusText, headers, body, charset, url, method);
|
||||
private NotAcceptable(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.NOT_ACCEPTABLE, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,12 +264,14 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class Conflict extends HttpClientErrorException {
|
||||
public static final class Conflict extends HttpClientErrorException {
|
||||
|
||||
Conflict(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private Conflict(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.CONFLICT, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.CONFLICT, statusText, headers, body, charset, url, method);
|
||||
private Conflict(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(message, HttpStatus.CONFLICT, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,12 +280,14 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class Gone extends HttpClientErrorException {
|
||||
public static final class Gone extends HttpClientErrorException {
|
||||
|
||||
Gone(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private Gone(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.GONE, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.GONE, statusText, headers, body, charset, url, method);
|
||||
private Gone(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(message, HttpStatus.GONE, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,12 +296,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class UnsupportedMediaType extends HttpClientErrorException {
|
||||
public static final class UnsupportedMediaType extends HttpClientErrorException {
|
||||
|
||||
UnsupportedMediaType(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private UnsupportedMediaType(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.UNSUPPORTED_MEDIA_TYPE, statusText, headers, body, charset, url, method);
|
||||
private UnsupportedMediaType(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.UNSUPPORTED_MEDIA_TYPE, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,12 +314,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class UnprocessableEntity extends HttpClientErrorException {
|
||||
public static final class UnprocessableEntity extends HttpClientErrorException {
|
||||
|
||||
UnprocessableEntity(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private UnprocessableEntity(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.UNPROCESSABLE_ENTITY, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.UNPROCESSABLE_ENTITY, statusText, headers, body, charset, url, method);
|
||||
private UnprocessableEntity(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.UNPROCESSABLE_ENTITY, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,12 +332,16 @@ public class HttpClientErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class TooManyRequests extends HttpClientErrorException {
|
||||
public static final class TooManyRequests extends HttpClientErrorException {
|
||||
|
||||
TooManyRequests(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private TooManyRequests(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset, url, method);
|
||||
private TooManyRequests(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Strategy interface used by the {@link DefaultResponseErrorHandler} to compose
|
||||
* a summary of the http error.
|
||||
*
|
||||
* @author Jerzy Krolak
|
||||
* @since 5.1
|
||||
*/
|
||||
public interface HttpErrorDetailsExtractor {
|
||||
|
||||
/**
|
||||
* Assemble HTTP error response details string, based on the provided response details.
|
||||
* @param rawStatusCode HTTP status code
|
||||
* @param statusText HTTP status text
|
||||
* @param responseBody response body
|
||||
* @param responseCharset response charset
|
||||
* @param url request URI
|
||||
* @param method request method
|
||||
* @return error details string
|
||||
*/
|
||||
@NotNull
|
||||
String getErrorDetails(int rawStatusCode, String statusText, @Nullable byte[] responseBody,
|
||||
@Nullable Charset responseCharset, @Nullable URI url, @Nullable HttpMethod method);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -69,35 +67,59 @@ public class HttpServerErrorException extends HttpStatusCodeException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with a status code and status text, headers, content, request URL, and method.
|
||||
* Constructor with a status code and status text, headers, content, and an
|
||||
* prepared message.
|
||||
* @since 5.2.2
|
||||
*/
|
||||
public HttpServerErrorException(String message, HttpStatus statusCode, String statusText,
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
super(message, statusCode, statusText, headers, body, charset, url, method);
|
||||
@Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an {@code HttpServerErrorException} or an HTTP status specific sub-class.
|
||||
* @since 5.1
|
||||
*/
|
||||
public static HttpServerErrorException create(
|
||||
String message, HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body,
|
||||
@Nullable Charset charset, @Nullable URI url, @Nullable HttpMethod method) {
|
||||
public static HttpServerErrorException create(HttpStatus statusCode,
|
||||
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
return create(null, statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #create(String, HttpStatus, String, HttpHeaders, byte[], Charset)}
|
||||
* with an optional prepared message.
|
||||
* @since 5.2.2.
|
||||
*/
|
||||
public static HttpServerErrorException create(@Nullable String message, HttpStatus statusCode,
|
||||
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
switch (statusCode) {
|
||||
case INTERNAL_SERVER_ERROR:
|
||||
return new HttpServerErrorException.InternalServerError(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpServerErrorException.InternalServerError(message, statusText, headers, body, charset) :
|
||||
new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
|
||||
case NOT_IMPLEMENTED:
|
||||
return new HttpServerErrorException.NotImplemented(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpServerErrorException.NotImplemented(message, statusText, headers, body, charset) :
|
||||
new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
|
||||
case BAD_GATEWAY:
|
||||
return new HttpServerErrorException.BadGateway(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpServerErrorException.BadGateway(message, statusText, headers, body, charset) :
|
||||
new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
|
||||
case SERVICE_UNAVAILABLE:
|
||||
return new HttpServerErrorException.ServiceUnavailable(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpServerErrorException.ServiceUnavailable(message, statusText, headers, body, charset) :
|
||||
new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
|
||||
case GATEWAY_TIMEOUT:
|
||||
return new HttpServerErrorException.GatewayTimeout(message, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpServerErrorException.GatewayTimeout(message, statusText, headers, body, charset) :
|
||||
new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
|
||||
default:
|
||||
return new HttpServerErrorException(message, statusCode, statusText, headers, body, charset, url, method);
|
||||
return message != null ?
|
||||
new HttpServerErrorException(message, statusCode, statusText, headers, body, charset) :
|
||||
new HttpServerErrorException(statusCode, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,12 +131,16 @@ public class HttpServerErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class InternalServerError extends HttpServerErrorException {
|
||||
public static final class InternalServerError extends HttpServerErrorException {
|
||||
|
||||
InternalServerError(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private InternalServerError(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset, url, method);
|
||||
private InternalServerError(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,12 +149,16 @@ public class HttpServerErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class NotImplemented extends HttpServerErrorException {
|
||||
public static final class NotImplemented extends HttpServerErrorException {
|
||||
|
||||
NotImplemented(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private NotImplemented(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset, url, method);
|
||||
private NotImplemented(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,12 +167,16 @@ public class HttpServerErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class BadGateway extends HttpServerErrorException {
|
||||
public static final class BadGateway extends HttpServerErrorException {
|
||||
|
||||
BadGateway(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.BAD_GATEWAY, statusText, headers, body, charset, url, method);
|
||||
private BadGateway(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,12 +185,16 @@ public class HttpServerErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class ServiceUnavailable extends HttpServerErrorException {
|
||||
public static final class ServiceUnavailable extends HttpServerErrorException {
|
||||
|
||||
ServiceUnavailable(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private ServiceUnavailable(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset, url, method);
|
||||
private ServiceUnavailable(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,12 +203,16 @@ public class HttpServerErrorException extends HttpStatusCodeException {
|
||||
* @since 5.1
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public static class GatewayTimeout extends HttpServerErrorException {
|
||||
public static final class GatewayTimeout extends HttpServerErrorException {
|
||||
|
||||
GatewayTimeout(String message, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
private GatewayTimeout(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
super(HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset);
|
||||
}
|
||||
|
||||
super(message, HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset, url, method);
|
||||
private GatewayTimeout(String message, String statusText,
|
||||
HttpHeaders headers, byte[] body, @Nullable Charset charset) {
|
||||
|
||||
super(message, HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -85,8 +83,8 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio
|
||||
protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
|
||||
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
|
||||
|
||||
this(getMessage(statusCode, statusText), statusCode, statusText,
|
||||
responseHeaders, responseBody, responseCharset, null, null);
|
||||
this(getMessage(statusCode, statusText),
|
||||
statusCode, statusText, responseHeaders, responseBody, responseCharset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,15 +96,12 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio
|
||||
* @param responseHeaders the response headers, may be {@code null}
|
||||
* @param responseBody the response body content, may be {@code null}
|
||||
* @param responseCharset the response body charset, may be {@code null}
|
||||
* @param url the request URL, may be {@code null}
|
||||
* @param method the request method, may be {@code null}
|
||||
* @since 5.2.2
|
||||
*/
|
||||
protected HttpStatusCodeException(String message, HttpStatus statusCode, String statusText,
|
||||
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
|
||||
|
||||
super(message, statusCode.value(), statusText, responseHeaders, responseBody,
|
||||
responseCharset, url, method);
|
||||
super(message, statusCode.value(), statusText, responseHeaders, responseBody, responseCharset);
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,12 +17,10 @@
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@@ -50,12 +48,6 @@ public class RestClientResponseException extends RestClientException {
|
||||
@Nullable
|
||||
private final String responseCharset;
|
||||
|
||||
@Nullable
|
||||
private final HttpMethod method;
|
||||
|
||||
@Nullable
|
||||
private final URI url;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new instance of with the given response data.
|
||||
@@ -67,22 +59,6 @@ public class RestClientResponseException extends RestClientException {
|
||||
*/
|
||||
public RestClientResponseException(String message, int statusCode, String statusText,
|
||||
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
|
||||
this(message, statusCode, statusText, responseHeaders, responseBody, responseCharset, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new instance of with the given response data.
|
||||
* @param statusCode the raw status code value
|
||||
* @param statusText the status text
|
||||
* @param responseHeaders the response headers (may be {@code null})
|
||||
* @param responseBody the response body content (may be {@code null})
|
||||
* @param responseCharset the response body charset (may be {@code null})
|
||||
* @param url the request URL (may be {@code null})
|
||||
* @param method the request method (may be {@code null})
|
||||
*/
|
||||
public RestClientResponseException(String message, int statusCode, String statusText,
|
||||
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset,
|
||||
@Nullable URI url, @Nullable HttpMethod method) {
|
||||
|
||||
super(message);
|
||||
this.rawStatusCode = statusCode;
|
||||
@@ -90,8 +66,6 @@ public class RestClientResponseException extends RestClientException {
|
||||
this.responseHeaders = responseHeaders;
|
||||
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
|
||||
this.responseCharset = (responseCharset != null ? responseCharset.name() : null);
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,20 +98,6 @@ public class RestClientResponseException extends RestClientException {
|
||||
return this.responseBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the HTTP request method.
|
||||
*/
|
||||
public HttpMethod getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the HTTP request url.
|
||||
*/
|
||||
public URI getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the response body converted to String. The charset used is that
|
||||
* of the response "Content-Type" or otherwise {@code "UTF-8"}.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -48,7 +46,7 @@ public class UnknownHttpStatusCodeException extends RestClientResponseException
|
||||
@Nullable byte[] responseBody, @Nullable Charset responseCharset) {
|
||||
|
||||
this("Unknown status code [" + rawStatusCode + "]" + " " + statusText,
|
||||
rawStatusCode, statusText, responseHeaders, responseBody, responseCharset, null, null);
|
||||
rawStatusCode, statusText, responseHeaders, responseBody, responseCharset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,12 +57,11 @@ public class UnknownHttpStatusCodeException extends RestClientResponseException
|
||||
* @param responseHeaders the response headers (may be {@code null})
|
||||
* @param responseBody the response body content (may be {@code null})
|
||||
* @param responseCharset the response body charset (may be {@code null})
|
||||
* @param url the request URI (may be {@code null})
|
||||
* @param method the request HTTP method (may be {@code null})
|
||||
* @since 5.2.2
|
||||
*/
|
||||
public UnknownHttpStatusCodeException(String message, int rawStatusCode, String statusText, @Nullable HttpHeaders responseHeaders,
|
||||
@Nullable byte[] responseBody, @Nullable Charset responseCharset, @Nullable URI url, @Nullable HttpMethod method) {
|
||||
public UnknownHttpStatusCodeException(String message, int rawStatusCode, String statusText,
|
||||
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
|
||||
|
||||
super(message, rawStatusCode, statusText, responseHeaders, responseBody, responseCharset, url, method);
|
||||
super(message, rawStatusCode, statusText, responseHeaders, responseBody, responseCharset);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user