ExceptionHandlingWebHandler.java 2.7 KB
Newer Older
R
Rossen Stoyanchev 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */
16

17
package org.springframework.web.server.handler;
R
Rossen Stoyanchev 已提交
18 19 20 21 22

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

23 24
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
25
import reactor.core.publisher.Mono;
R
Rossen Stoyanchev 已提交
26 27

import org.springframework.http.HttpStatus;
28
import org.springframework.web.server.ServerWebExchange;
29 30
import org.springframework.web.server.WebExceptionHandler;
import org.springframework.web.server.WebHandler;
R
Rossen Stoyanchev 已提交
31 32

/**
33 34
 * WebHandler that can invoke a target {@link WebHandler} and then apply
 * exception handling with one or more {@link WebExceptionHandler} instances.
R
Rossen Stoyanchev 已提交
35 36
 *
 * @author Rossen Stoyanchev
37
 * @since 5.0
R
Rossen Stoyanchev 已提交
38 39 40
 */
public class ExceptionHandlingWebHandler extends WebHandlerDecorator {

41 42
	private static Log logger = LogFactory.getLog(ExceptionHandlingWebHandler.class);

R
Rossen Stoyanchev 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56
	private final List<WebExceptionHandler> exceptionHandlers;


	public ExceptionHandlingWebHandler(WebHandler delegate, WebExceptionHandler... exceptionHandlers) {
		super(delegate);
		this.exceptionHandlers = initList(exceptionHandlers);
	}

	private static List<WebExceptionHandler> initList(WebExceptionHandler[] list) {
		return (list != null ? Collections.unmodifiableList(Arrays.asList(list)): Collections.emptyList());
	}


	/**
57
	 * Return a read-only list of the configured exception handlers.
R
Rossen Stoyanchev 已提交
58 59 60 61 62
	 */
	public List<WebExceptionHandler> getExceptionHandlers() {
		return this.exceptionHandlers;
	}

63

R
Rossen Stoyanchev 已提交
64
	@Override
65
	public Mono<Void> handle(ServerWebExchange exchange) {
R
Rossen Stoyanchev 已提交
66 67 68 69 70 71 72 73 74 75
		Mono<Void> mono;
		try {
			mono = getDelegate().handle(exchange);
		}
		catch (Throwable ex) {
			mono = Mono.error(ex);
		}
		for (WebExceptionHandler exceptionHandler : this.exceptionHandlers) {
			mono = mono.otherwise(ex -> exceptionHandler.handle(exchange, ex));
		}
76
		return mono.otherwise(ex -> handleUnresolvedException(exchange, ex));
R
Rossen Stoyanchev 已提交
77 78
	}

79
	private Mono<? extends Void> handleUnresolvedException(ServerWebExchange exchange, Throwable ex) {
80 81 82
		if (logger.isDebugEnabled()) {
			logger.debug("Could not complete request", ex);
		}
R
Rossen Stoyanchev 已提交
83 84 85 86 87
		exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		return Mono.empty();
	}

}