Move ResponseStatusException into server sub-package

This commit is contained in:
Rossen Stoyanchev 2016-04-19 11:18:57 -04:00
parent 00eff4b611
commit 66707835fe
7 changed files with 78 additions and 55 deletions

View File

@ -1,45 +0,0 @@
/*
* Copyright 2002-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web;
import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpStatus;
/**
* Exception wrapper to associate an exception with a status code at runtime.
*
* @author Rossen Stoyanchev
*/
public class ResponseStatusException extends NestedRuntimeException {
private final HttpStatus httpStatus;
public ResponseStatusException(HttpStatus status) {
this(status, null);
}
public ResponseStatusException(HttpStatus status, Throwable cause) {
super("Request processing failure with status code: " + status, cause);
this.httpStatus = status;
}
public HttpStatus getHttpStatus() {
return this.httpStatus;
}
}

View File

@ -20,7 +20,7 @@ import java.util.function.Function;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.bind.annotation.ResponseStatus;
/**

View File

@ -17,7 +17,7 @@ package org.springframework.web.reactive;
import reactor.core.publisher.Mono;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.WebExceptionHandler;
import org.springframework.web.server.ServerWebExchange;
@ -32,7 +32,7 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof ResponseStatusException) {
exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getHttpStatus());
exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getStatus());
return Mono.empty();
}
return Mono.error(ex);

View File

@ -0,0 +1,68 @@
/*
* Copyright 2002-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.server;
import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
/**
* Base class for exceptions associated with specific HTTP response status codes.
*
* @author Rossen Stoyanchev
*/
public class ResponseStatusException extends NestedRuntimeException {
private final HttpStatus status;
private final String reason;
/**
* Constructor with a response code and a reason to add to the exception
* message as explanation.
*/
public ResponseStatusException(HttpStatus status, String reason) {
this(status, reason, null);
}
/**
* Constructor with a nested exception.
*/
public ResponseStatusException(HttpStatus status, String reason, Throwable cause) {
super("Request failure [status: " + status + ", reason: \"" + reason + "\"]", cause);
Assert.notNull(status, "'status' is required");
Assert.notNull(reason, "'reason' is required");
this.status = status;
this.reason = reason;
}
/**
* The HTTP status that fits the exception.
*/
public HttpStatus getStatus() {
return this.status;
}
/**
* The reason explaining the exception.
*/
public String getReason() {
return this.reason;
}
}

View File

@ -42,7 +42,7 @@ import org.springframework.http.server.reactive.MockServerHttpRequest;
import org.springframework.http.server.reactive.MockServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

View File

@ -22,7 +22,7 @@ import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.bind.annotation.ResponseStatus;
import static org.junit.Assert.assertEquals;
@ -47,7 +47,7 @@ public class DispatcherHandlerExceptionMapperTests {
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) ex).getHttpStatus());
assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) ex).getStatus());
}
@ -57,7 +57,7 @@ public class DispatcherHandlerExceptionMapperTests {
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.NOT_ACCEPTABLE, ((ResponseStatusException) ex).getHttpStatus());
assertEquals(HttpStatus.NOT_ACCEPTABLE, ((ResponseStatusException) ex).getStatus());
}
@Test
@ -66,7 +66,7 @@ public class DispatcherHandlerExceptionMapperTests {
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getHttpStatus());
assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getStatus());
}
@Test
@ -75,7 +75,7 @@ public class DispatcherHandlerExceptionMapperTests {
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getHttpStatus());
assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getStatus());
}

View File

@ -26,7 +26,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.MockServerHttpRequest;
import org.springframework.http.server.reactive.MockServerHttpResponse;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.WebSessionManager;