Add ResponseStatusException
This change adds a ResponseStatusException to associate an exception with a status code at runtime. Along with that is an ResponseStatusExceptionHandler that handles ResponseStatusException by setting the response status.
This commit is contained in:
parent
5231e7da7b
commit
1f15b7e074
|
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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.reactive;
|
||||||
|
|
||||||
|
import org.reactivestreams.Publisher;
|
||||||
|
import reactor.Publishers;
|
||||||
|
|
||||||
|
import org.springframework.http.server.reactive.HttpExceptionHandler;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.web.ResponseStatusException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle {@link ResponseStatusException} by setting the response status.
|
||||||
|
*
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
*/
|
||||||
|
public class ResponseStatusExceptionHandler implements HttpExceptionHandler {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Publisher<Void> handle(ServerHttpRequest request, ServerHttpResponse response, Throwable ex) {
|
||||||
|
if (ex instanceof ResponseStatusException) {
|
||||||
|
response.setStatusCode(((ResponseStatusException) ex).getHttpStatus());
|
||||||
|
return response.writeHeaders();
|
||||||
|
}
|
||||||
|
return Publishers.error(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* 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.reactive;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.reactivestreams.Publisher;
|
||||||
|
import reactor.rx.Streams;
|
||||||
|
import reactor.rx.action.Signal;
|
||||||
|
|
||||||
|
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 static junit.framework.TestCase.assertSame;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
*/
|
||||||
|
public class ResponseStatusExceptionHandlerTests {
|
||||||
|
|
||||||
|
private ResponseStatusExceptionHandler handler;
|
||||||
|
|
||||||
|
private MockServerHttpRequest request;
|
||||||
|
|
||||||
|
private MockServerHttpResponse response;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
this.handler = new ResponseStatusExceptionHandler();
|
||||||
|
this.request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path"));
|
||||||
|
this.response = new MockServerHttpResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handleException() throws Exception {
|
||||||
|
Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST);
|
||||||
|
Publisher<Void> publisher = this.handler.handle(this.request, this.response, ex);
|
||||||
|
|
||||||
|
Streams.wrap(publisher).toList().await(5, TimeUnit.SECONDS);
|
||||||
|
assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void unresolvedException() throws Exception {
|
||||||
|
Throwable ex = new IllegalStateException();
|
||||||
|
Publisher<Void> publisher = this.handler.handle(this.request, this.response, ex);
|
||||||
|
|
||||||
|
List<Signal<Void>> signals = Streams.wrap(publisher).materialize().toList().await(5, TimeUnit.SECONDS);
|
||||||
|
assertEquals(1, signals.size());
|
||||||
|
assertTrue(signals.get(0).hasError());
|
||||||
|
assertSame(ex, signals.get(0).getThrowable());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue