View Javadoc
1   /*
2    * Copyright 2020-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.exception;
18  
19  import org.springframework.http.HttpStatusCode;
20  import org.springframework.util.ObjectUtils;
21  import org.springframework.web.reactive.function.client.WebClientResponseException;
22  
23  /**
24   * The implementation of a rest api exception mapper for spring web flux.
25   *
26   * @author Christian Bremer
27   */
28  public class RestApiExceptionMapperForWebFlux extends RestApiExceptionMapperForWeb {
29  
30    /**
31     * Instantiates a new rest api exception mapper.
32     *
33     * @param properties the properties
34     * @param applicationName the application name
35     */
36    public RestApiExceptionMapperForWebFlux(
37        RestApiExceptionMapperProperties properties,
38        String applicationName) {
39      super(properties, applicationName);
40    }
41  
42    @Override
43    protected HttpStatusCode detectHttpStatus(Throwable exception, Object handler) {
44      if (exception instanceof WebClientResponseException cre) {
45        return cre.getStatusCode();
46      }
47      return super.detectHttpStatus(exception, handler);
48    }
49  
50    @Override
51    protected String getError(Throwable exception, HttpStatusCode httpStatusCode) {
52      if ((exception instanceof WebClientResponseException cre)
53          && !(ObjectUtils.isEmpty(cre.getStatusText()))) {
54        return cre.getStatusText();
55      }
56      return super.getError(exception, httpStatusCode);
57    }
58  }