GlobalErrorAttributes.java
1.96 KB
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
47
48
49
50
51
52
53
package com.glsx.plat.gateway.exception;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author payu
*/
@Slf4j
@Component
public class GlobalErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
//简单返回
if (Objects.isNull(options)) return assembleError(request);
Map<String, Object> map = super.getErrorAttributes(request, options);
if (getError(request) instanceof GlobalException) {
GlobalException ex = (GlobalException) getError(request);
map.put(R.MSG_KEY, ex.getMessage());
map.put(R.CODE_KEY, ex.getStatus().value());
return map;
}
map.put(R.CODE_KEY, map.getOrDefault("status", HttpStatus.INTERNAL_SERVER_ERROR.value()));
map.put(R.MSG_KEY, "System Error , Check logs!");
return map;
}
private Map<String, Object> assembleError(ServerRequest request) {
Map<String, Object> errorAttributes = new LinkedHashMap<>();
Throwable error = getError(request);
if (error instanceof GlobalException) {
GlobalException ex = (GlobalException) getError(request);
errorAttributes.put(R.CODE_KEY, ex.getStatus().value());
errorAttributes.put(R.MSG_KEY, ex.getMessage());
} else {
errorAttributes.put(R.CODE_KEY, HttpStatus.INTERNAL_SERVER_ERROR.value());
errorAttributes.put(R.MSG_KEY, "INTERNAL SERVER ERROR");
}
return errorAttributes;
}
}