001/*
002 * Copyright 2002-2018 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.reactive.handler;
018
019import org.springframework.core.annotation.AnnotatedElementUtils;
020import org.springframework.http.HttpStatus;
021import org.springframework.lang.Nullable;
022import org.springframework.web.bind.annotation.ResponseStatus;
023import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
024
025/**
026 * Common WebFlux exception handler that detects instances of
027 * {@link org.springframework.web.server.ResponseStatusException}
028 * (inherited from the base class) as well as exceptions annotated with
029 * {@link ResponseStatus @ResponseStatus} by determining the HTTP status
030 * for them and updating the status of the response accordingly.
031 *
032 * <p>If the response is already committed, the error remains unresolved
033 * and is propagated.
034 *
035 * @author Juergen Hoeller
036 * @author Rossen Stoyanchev
037 * @since 5.0.5
038 */
039public class WebFluxResponseStatusExceptionHandler extends ResponseStatusExceptionHandler {
040
041        @Override
042        @Nullable
043        protected HttpStatus determineStatus(Throwable ex) {
044                HttpStatus status = super.determineStatus(ex);
045                if (status == null) {
046                        ResponseStatus ann = AnnotatedElementUtils.findMergedAnnotation(ex.getClass(), ResponseStatus.class);
047                        if (ann != null) {
048                                status = ann.code();
049                        }
050                }
051                return status;
052        }
053
054}