com.naharoo.commons.mstoolkit.rest.exceptionhandler.client.FeignErrorDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ms-toolkit-rest-exception-handler-starter Show documentation
Show all versions of ms-toolkit-rest-exception-handler-starter Show documentation
Common exceptions handler as a Spring Boot Starter
package com.naharoo.commons.mstoolkit.rest.exceptionhandler.client;
import feign.Request;
import feign.Response;
import feign.codec.ErrorDecoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnClass(name = "feign.codec.ErrorDecoder")
public class FeignErrorDecoder implements ErrorDecoder {
@Autowired
private MsExceptionFactory msExceptionFactory;
@Override
public Exception decode(String methodKey, Response response) {
final Request request = response.request();
final int status = response.status();
if (status < 400) {
throw new IllegalStateException(
"Only errors can be handled by " + FeignErrorDecoder.class.getSimpleName());
}
if (status >= 500) {
return new IllegalStateException(
String.format(
"Failed to process HTTP %s %s request. %s resource returned %s",
request.httpMethod().name(), request.url(), methodKey, status
));
}
final Response.Body body = response.body();
return msExceptionFactory.createInstance(status, body == null ? null : body::asInputStream);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy