001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.ws.client; 015 016import org.gbif.api.exception.ServiceUnavailableException; 017import org.gbif.ws.MethodNotAllowedException; 018import org.gbif.ws.NotFoundException; 019 020import java.io.IOException; 021import java.io.Reader; 022import java.net.URI; 023import java.nio.charset.StandardCharsets; 024 025import javax.validation.ValidationException; 026 027import org.apache.commons.io.IOUtils; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030import org.springframework.security.access.AccessDeniedException; 031 032import feign.Response; 033import feign.codec.ErrorDecoder; 034 035public class ClientErrorDecoder implements ErrorDecoder { 036 037 private static final Logger LOG = LoggerFactory.getLogger(ClientErrorDecoder.class); 038 039 @Override 040 public Exception decode(String methodKey, Response response) { 041 String message = null; 042 043 if (response.body() != null) { 044 try (Reader reader = response.body().asReader(StandardCharsets.UTF_8)) { 045 // Easy way to read the stream and get a String object 046 message = IOUtils.toString(reader); 047 LOG.error("Client exception: {}", message); 048 } catch (IOException e) { 049 LOG.error("Exception during reading client error response", e); 050 } 051 } 052 053 switch (response.status()) { 054 case 400: 055 return new IllegalArgumentException("A bad request received"); 056 case 401: 057 return new AccessDeniedException("Unauthorized request received"); 058 case 403: 059 return new AccessDeniedException("Forbidden request received"); 060 case 404: 061 return new NotFoundException("Resource not found", URI.create(response.request().url())); 062 case 405: 063 return new MethodNotAllowedException("Method not allowed to user"); 064 case 422: 065 return message != null 066 ? new ValidationException(extractValidationErrorMessage(message)) 067 : new ValidationException(); 068 case 500: 069 return new ServiceUnavailableException( 070 "An internal server error occurred, please try again later"); 071 case 501: 072 return new UnsupportedOperationException( 073 message != null ? message : "Method not implement yet"); 074 default: 075 return new RuntimeException(message != null ? message : "Unexpected exception"); 076 } 077 } 078 079 private String extractValidationErrorMessage(String message) { 080 return message.replace("<ul><li>", "").replace("</li></ul>", "").replace("<li></li>", ", "); 081 } 082}