View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *     http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.gbif.ws.client;
15  
16  import org.gbif.api.exception.ServiceUnavailableException;
17  import org.gbif.ws.MethodNotAllowedException;
18  import org.gbif.ws.NotFoundException;
19  
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.net.URI;
23  import java.nio.charset.StandardCharsets;
24  
25  import javax.validation.ValidationException;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.security.access.AccessDeniedException;
31  
32  import feign.Response;
33  import feign.codec.ErrorDecoder;
34  
35  public class ClientErrorDecoder implements ErrorDecoder {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(ClientErrorDecoder.class);
38  
39    @Override
40    public Exception decode(String methodKey, Response response) {
41      String message = null;
42  
43      if (response.body() != null) {
44        try (Reader reader = response.body().asReader(StandardCharsets.UTF_8)) {
45          // Easy way to read the stream and get a String object
46          message = IOUtils.toString(reader);
47          LOG.error("Client exception: {}", message);
48        } catch (IOException e) {
49          LOG.error("Exception during reading client error response", e);
50        }
51      }
52  
53      switch (response.status()) {
54        case 400:
55          return new IllegalArgumentException("A bad request received");
56        case 401:
57          return new AccessDeniedException("Unauthorized request received");
58        case 403:
59          return new AccessDeniedException("Forbidden request received");
60        case 404:
61          return new NotFoundException("Resource not found", URI.create(response.request().url()));
62        case 405:
63          return new MethodNotAllowedException("Method not allowed to user");
64        case 422:
65          return message != null
66              ? new ValidationException(extractValidationErrorMessage(message))
67              : new ValidationException();
68        case 500:
69          return new ServiceUnavailableException(
70              "An internal server error occurred, please try again later");
71        case 501:
72          return new UnsupportedOperationException(
73              message != null ? message : "Method not implement yet");
74        default:
75          return new RuntimeException(message != null ? message : "Unexpected exception");
76      }
77    }
78  
79    private String extractValidationErrorMessage(String message) {
80      return message.replace("<ul><li>", "").replace("</li></ul>", "").replace("<li></li>", ", ");
81    }
82  }