Feign ErrorDecoder not invoked

非 Y 不嫁゛ 提交于 2020-07-10 08:56:25

问题


Hello I created simple ErrorDecoder but it's not invoked:

The configuration:

@Bean
UserClient userClient ( @Value( "${url}" ) final String url )
{
    return Feign
            .builder()
            .client( new OkHttpClient() )
            .errorDecoder( new FeignErrorDecoder() )
            .encoder( new GsonEncoder() )
            .decoder( new GsonDecoder() )
            .logger( new Slf4jLogger( UserClient.class ) )
            .logLevel( Level.FULL )
            .target( UserClient.class, url );
}

ErrorDecoder:

@Slf4j
public class FeignErrorDecoder implements ErrorDecoder
{
    @Override
    public Exception decode ( String methodKey, Response response )
    {
        if(response.status() != 200) {
            log.error( "ERROR" );
        }
        return errorStatus(methodKey, response);
    }
}

Then the stacktrace shows invoaction of RetryableException and I don't see my log anywhere. Am i doing something wrong?


回答1:


Error Decoders are invoked only when a response is received and the response code is not 2xx. Error Decoders are not invoked if the request fails because the maximum number of retries is exhausted. In that case, the Exception thrown out to the calling method.

If you want to apply logic during a retry, you will need to supply your own Retryer in addition to your ErrorDecoder




回答2:


Attempt to return a non-200 status code.

Like this:

@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseEntity<String> defaultException(Exception ex) {
    LogBack.error(ex.getMessage(),ex);
    return ResponseEntity.status(400).body("");
}


来源:https://stackoverflow.com/questions/54258079/feign-errordecoder-not-invoked

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!