|
21 | 21 |
|
22 | 22 | import javax.servlet.http.HttpServletResponse; |
23 | 23 | import lombok.Getter; |
24 | | -import org.apache.commons.lang3.StringUtils; |
25 | | -import org.apache.geaflow.console.common.util.context.ContextHolder; |
26 | | -import org.apache.geaflow.console.common.util.exception.GeaflowCompileException; |
27 | | -import org.apache.geaflow.console.common.util.exception.GeaflowException; |
28 | | -import org.apache.geaflow.console.common.util.exception.GeaflowIllegalException; |
29 | | -import org.apache.geaflow.console.common.util.exception.GeaflowSecurityException; |
30 | | -import org.apache.geaflow.console.common.util.type.GeaflowApiResponseCode; |
31 | | -import org.springframework.http.HttpHeaders; |
| 24 | +import org.apache.geaflow.console.common.util.exception.GeaflowExceptionClassifier; |
| 25 | +import org.apache.geaflow.console.common.util.exception.GeaflowExceptionClassifier.GeaflowExceptionClassificationResult; |
32 | 26 |
|
33 | 27 | @Getter |
34 | 28 | public class ErrorApiResponse<T> extends GeaflowApiResponse<T> { |
35 | 29 |
|
| 30 | + private static final GeaflowExceptionClassifier EXCEPTION_CLASSIFIER = new GeaflowExceptionClassifier(); |
| 31 | + |
36 | 32 | private final GeaflowApiRequest<?> request; |
37 | 33 |
|
38 | 34 | private final String message; |
39 | 35 |
|
40 | 36 | protected ErrorApiResponse(Throwable error) { |
41 | 37 | super(false); |
42 | 38 |
|
43 | | - String message = error.getMessage(); |
44 | | - if (StringUtils.isBlank(message)) { |
45 | | - message = error.getClass().getSimpleName(); |
46 | | - } |
47 | | - |
48 | | - if (error instanceof GeaflowSecurityException) { |
49 | | - this.code = GeaflowApiResponseCode.FORBIDDEN; |
50 | | - |
51 | | - } else if (error instanceof GeaflowIllegalException) { |
52 | | - this.code = GeaflowApiResponseCode.ILLEGAL; |
53 | | - |
54 | | - } else if (error instanceof GeaflowCompileException) { |
55 | | - this.code = GeaflowApiResponseCode.ERROR; |
56 | | - message = ((GeaflowCompileException) error).getDisplayMessage(); |
57 | | - |
58 | | - } else if (error instanceof GeaflowException) { |
59 | | - this.code = GeaflowApiResponseCode.ERROR; |
60 | | - |
61 | | - } else if (error instanceof IllegalArgumentException) { |
62 | | - this.code = GeaflowApiResponseCode.ILLEGAL; |
63 | | - |
64 | | - } else if (error instanceof NullPointerException) { |
65 | | - this.code = GeaflowApiResponseCode.ERROR; |
66 | | - |
67 | | - } else { |
68 | | - this.code = GeaflowApiResponseCode.FAIL; |
69 | | - while (error.getCause() != null) { |
70 | | - error = error.getCause(); |
71 | | - } |
72 | | - } |
| 39 | + // Use classifier to classify the exception |
| 40 | + GeaflowExceptionClassificationResult result = EXCEPTION_CLASSIFIER.classify(error); |
| 41 | + this.code = result.getCode(); |
| 42 | + this.message = result.getMessage(); |
73 | 43 |
|
74 | 44 | this.request = GeaflowApiRequest.currentRequest(); |
75 | | - this.message = message; |
76 | 45 | } |
77 | 46 |
|
78 | 47 | @Override |
79 | 48 | public void write(HttpServletResponse response) { |
80 | 49 | response.reset(); |
81 | | - configCors(response); |
| 50 | + // Use centralized CORS configuration |
| 51 | + ErrorApiCorsConfigurer.configure(response); |
82 | 52 | super.write(response); |
83 | 53 | } |
84 | | - |
85 | | - private void configCors(HttpServletResponse response) { |
86 | | - String originKey = HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; |
87 | | - String credentialsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS; |
88 | | - String headersKey = HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS; |
89 | | - String methodsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS; |
90 | | - String ageKey = HttpHeaders.ACCESS_CONTROL_MAX_AGE; |
91 | | - |
92 | | - response.setHeader(originKey, ContextHolder.get().getRequest().getHeader(HttpHeaders.ORIGIN)); |
93 | | - response.setHeader(methodsKey, "OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE,TRACE"); |
94 | | - response.setHeader(headersKey, "Origin,X-Requested-With,Content-Type,Accept,geaflow-token,geaflow-task-token"); |
95 | | - response.setHeader(credentialsKey, "true"); |
96 | | - response.setHeader(ageKey, "3600"); |
97 | | - } |
98 | 54 | } |
0 commit comments