001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.client;
018
019import java.nio.charset.Charset;
020
021import org.springframework.http.HttpHeaders;
022import org.springframework.http.HttpStatus;
023import org.springframework.lang.Nullable;
024
025/**
026 * Exception thrown when an HTTP 5xx is received.
027 *
028 * @author Arjen Poutsma
029 * @since 3.0
030 * @see DefaultResponseErrorHandler
031 */
032public class HttpServerErrorException extends HttpStatusCodeException {
033
034        private static final long serialVersionUID = -2915754006618138282L;
035
036
037        /**
038         * Constructor with a status code only.
039         */
040        public HttpServerErrorException(HttpStatus statusCode) {
041                super(statusCode);
042        }
043
044        /**
045         * Constructor with a status code and status text.
046         */
047        public HttpServerErrorException(HttpStatus statusCode, String statusText) {
048                super(statusCode, statusText);
049        }
050
051        /**
052         * Constructor with a status code and status text, and content.
053         */
054        public HttpServerErrorException(
055                        HttpStatus statusCode, String statusText, @Nullable byte[] body, @Nullable Charset charset) {
056
057                super(statusCode, statusText, body, charset);
058        }
059
060        /**
061         * Constructor with a status code and status text, headers, and content.
062         */
063        public HttpServerErrorException(HttpStatus statusCode, String statusText,
064                        @Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset) {
065
066                super(statusCode, statusText, headers, body, charset);
067        }
068
069        /**
070         * Constructor with a status code and status text, headers, content, and an
071         * prepared message.
072         * @since 5.2.2
073         */
074        public HttpServerErrorException(String message, HttpStatus statusCode, String statusText,
075                        @Nullable HttpHeaders headers, @Nullable byte[] body, @Nullable Charset charset) {
076
077                super(message, statusCode, statusText, headers, body, charset);
078        }
079
080        /**
081         * Create an {@code HttpServerErrorException} or an HTTP status specific sub-class.
082         * @since 5.1
083         */
084        public static HttpServerErrorException create(HttpStatus statusCode,
085                        String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
086
087                return create(null, statusCode, statusText, headers, body, charset);
088        }
089
090        /**
091         * Variant of {@link #create(String, HttpStatus, String, HttpHeaders, byte[], Charset)}
092         * with an optional prepared message.
093         * @since 5.2.2.
094         */
095        public static HttpServerErrorException create(@Nullable String message, HttpStatus statusCode,
096                        String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
097
098                switch (statusCode) {
099                        case INTERNAL_SERVER_ERROR:
100                                return message != null ?
101                                                new HttpServerErrorException.InternalServerError(message, statusText, headers, body, charset) :
102                                                new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
103                        case NOT_IMPLEMENTED:
104                                return message != null ?
105                                                new HttpServerErrorException.NotImplemented(message, statusText, headers, body, charset) :
106                                                new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
107                        case BAD_GATEWAY:
108                                return message != null ?
109                                                new HttpServerErrorException.BadGateway(message, statusText, headers, body, charset) :
110                                                new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
111                        case SERVICE_UNAVAILABLE:
112                                return message != null ?
113                                                new HttpServerErrorException.ServiceUnavailable(message, statusText, headers, body, charset) :
114                                                new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
115                        case GATEWAY_TIMEOUT:
116                                return message != null ?
117                                                new HttpServerErrorException.GatewayTimeout(message, statusText, headers, body, charset) :
118                                                new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
119                        default:
120                                return message != null ?
121                                                new HttpServerErrorException(message, statusCode, statusText, headers, body, charset) :
122                                                new HttpServerErrorException(statusCode, statusText, headers, body, charset);
123                }
124        }
125
126
127        // Subclasses for specific HTTP status codes
128
129        /**
130         * {@link HttpServerErrorException} for status HTTP 500 Internal Server Error.
131         * @since 5.1
132         */
133        @SuppressWarnings("serial")
134        public static final class InternalServerError extends HttpServerErrorException {
135
136                private InternalServerError(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
137                        super(HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset);
138                }
139
140                private InternalServerError(String message, String statusText,
141                                HttpHeaders headers, byte[] body, @Nullable Charset charset) {
142
143                        super(message, HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset);
144                }
145        }
146
147        /**
148         * {@link HttpServerErrorException} for status HTTP 501 Not Implemented.
149         * @since 5.1
150         */
151        @SuppressWarnings("serial")
152        public static final class NotImplemented extends HttpServerErrorException {
153
154                private NotImplemented(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
155                        super(HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset);
156                }
157
158                private NotImplemented(String message, String statusText,
159                                HttpHeaders headers, byte[] body, @Nullable Charset charset) {
160
161                        super(message, HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset);
162                }
163        }
164
165        /**
166         * {@link HttpServerErrorException} for status HTTP HTTP 502 Bad Gateway.
167         * @since 5.1
168         */
169        @SuppressWarnings("serial")
170        public static final class BadGateway extends HttpServerErrorException {
171
172                private BadGateway(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
173                        super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
174                }
175
176                private BadGateway(String message, String statusText,
177                                HttpHeaders headers, byte[] body, @Nullable Charset charset) {
178
179                        super(message, HttpStatus.BAD_GATEWAY, statusText, headers, body, charset);
180                }
181        }
182
183        /**
184         * {@link HttpServerErrorException} for status HTTP 503 Service Unavailable.
185         * @since 5.1
186         */
187        @SuppressWarnings("serial")
188        public static final class ServiceUnavailable extends HttpServerErrorException {
189
190                private ServiceUnavailable(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
191                        super(HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset);
192                }
193
194                private ServiceUnavailable(String message, String statusText,
195                                HttpHeaders headers, byte[] body, @Nullable Charset charset) {
196
197                        super(message, HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset);
198                }
199        }
200
201        /**
202         * {@link HttpServerErrorException} for status HTTP 504 Gateway Timeout.
203         * @since 5.1
204         */
205        @SuppressWarnings("serial")
206        public static final class GatewayTimeout extends HttpServerErrorException {
207
208                private GatewayTimeout(String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
209                        super(HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset);
210                }
211
212                private GatewayTimeout(String message, String statusText,
213                                HttpHeaders headers, byte[] body, @Nullable Charset charset) {
214
215                        super(message, HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset);
216                }
217        }
218
219}