xiangpei
2024-08-05 c221aef40109fe7296450bd51b872ce51ee3e59c
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
37
38
39
40
41
42
43
44
45
46
package com.ycl.feign;
 
import com.ycl.exception.FeignException;
import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
 
/**
 * feign的错误解码器
 *
 * @author:xp
 * @date:2024/7/14 11:30
 */
@Slf4j
@Configuration
public class FeignErrorDecoder implements ErrorDecoder {
 
    @Override
    public Exception decode(String methodKey, Response response) {
        String msg = "";
        switch (response.status()) {
            case 400:
                msg = "远程调用异常:参数错误";
                break;
            case 401:
                msg = "远程调用异常:未登录,无法操作";
                break;
            case 403:
                msg = "远程调用异常:无权限执行操作";
                break;
            case 404:
                msg = "远程调用异常:调用接口不存在";
                break;
            case 500:
                msg = "远程调用异常:对方服务500异常";
                break;
            default:
                msg = "远程调用异常:未知异常";
                break;
        }
        log.error(msg);
        log.error(response.reason());
        return new FeignException(msg);
    }
}