吴学文
2019-04-24 cec5799c7dbabbe6672399486303165e1b3586fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.java110.core.feign;
 
 
import com.netflix.hystrix.exception.HystrixBadRequestException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.IOException;
 
public class UserErrorDecoder implements ErrorDecoder {
 
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    public Exception decode(String methodKey, Response response) {
 
        Exception exception = null;
        try {
            String json = Util.toString(response.body().asReader());
 
            logger.error("调用方法出现异常了:"+json);
            exception = new RuntimeException(json);
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        }
        // 这里只封装4开头的请求异常ß
        if (400 <= response.status() && response.status() < 500){
            exception = new HystrixBadRequestException("request exception wrapper", exception);
        }else{
            logger.error(exception.getMessage(), exception);
        }
        return exception;
    }
}