ResponseStatusExceptionHandler.java 1.3 KB
Newer Older
R
Rossen Stoyanchev 已提交
1
/*
2
 * Copyright 2002-2016 the original author or authors.
R
Rossen Stoyanchev 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
16 17

package org.springframework.web.server.handler;
R
Rossen Stoyanchev 已提交
18

19
import reactor.core.publisher.Mono;
R
Rossen Stoyanchev 已提交
20

21
import org.springframework.web.server.ResponseStatusException;
22
import org.springframework.web.server.ServerWebExchange;
23
import org.springframework.web.server.WebExceptionHandler;
R
Rossen Stoyanchev 已提交
24 25 26 27 28

/**
 * Handle {@link ResponseStatusException} by setting the response status.
 *
 * @author Rossen Stoyanchev
29
 * @since 5.0
R
Rossen Stoyanchev 已提交
30
 */
R
Rossen Stoyanchev 已提交
31
public class ResponseStatusExceptionHandler implements WebExceptionHandler {
R
Rossen Stoyanchev 已提交
32 33

	@Override
34
	public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
R
Rossen Stoyanchev 已提交
35
		if (ex instanceof ResponseStatusException) {
36
			exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getStatus());
37
			return Mono.empty();
R
Rossen Stoyanchev 已提交
38
		}
39
		return Mono.error(ex);
R
Rossen Stoyanchev 已提交
40 41 42
	}

}