Skip to content

Commit b3b4c50

Browse files
yazongliliya
andauthored
[ISSUE701]Use classifier (#720)
Co-authored-by: liliya <lly_testfox@foxmail.com>
1 parent 2a0fd8e commit b3b4c50

3 files changed

Lines changed: 140 additions & 54 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.geaflow.console.common.util.exception;
21+
22+
import org.apache.commons.lang3.StringUtils;
23+
import org.apache.geaflow.console.common.util.type.GeaflowApiResponseCode;
24+
25+
public class GeaflowExceptionClassifier {
26+
27+
public GeaflowExceptionClassificationResult classify(Throwable error) {
28+
GeaflowApiResponseCode code;
29+
String message = error.getMessage();
30+
31+
if (StringUtils.isBlank(message)) {
32+
message = error.getClass().getSimpleName();
33+
}
34+
35+
if (error instanceof GeaflowSecurityException) {
36+
code = GeaflowApiResponseCode.FORBIDDEN;
37+
38+
} else if (error instanceof GeaflowIllegalException) {
39+
code = GeaflowApiResponseCode.ILLEGAL;
40+
41+
} else if (error instanceof GeaflowCompileException) {
42+
code = GeaflowApiResponseCode.ERROR;
43+
message = ((GeaflowCompileException) error).getDisplayMessage();
44+
45+
} else if (error instanceof GeaflowException) {
46+
code = GeaflowApiResponseCode.ERROR;
47+
48+
} else if (error instanceof IllegalArgumentException) {
49+
code = GeaflowApiResponseCode.ILLEGAL;
50+
51+
} else if (error instanceof NullPointerException) {
52+
code = GeaflowApiResponseCode.ERROR;
53+
54+
} else {
55+
code = GeaflowApiResponseCode.FAIL;
56+
// Traverse to root cause for better error message
57+
while (error.getCause() != null) {
58+
error = error.getCause();
59+
}
60+
message = error.getMessage();
61+
if (StringUtils.isBlank(message)) {
62+
message = error.getClass().getSimpleName();
63+
}
64+
}
65+
66+
return new GeaflowExceptionClassificationResult(code, message);
67+
}
68+
69+
public static class GeaflowExceptionClassificationResult {
70+
private final GeaflowApiResponseCode code;
71+
private final String message;
72+
73+
public GeaflowExceptionClassificationResult(GeaflowApiResponseCode code, String message) {
74+
this.code = code;
75+
this.message = message;
76+
}
77+
78+
public GeaflowApiResponseCode getCode() {
79+
return code;
80+
}
81+
82+
public String getMessage() {
83+
return message;
84+
}
85+
}
86+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.geaflow.console.web.api;
21+
22+
import javax.servlet.http.HttpServletResponse;
23+
import org.apache.geaflow.console.common.util.context.ContextHolder;
24+
import org.springframework.http.HttpHeaders;
25+
26+
public class ErrorApiCorsConfigurer {
27+
28+
private ErrorApiCorsConfigurer() {
29+
}
30+
31+
public static void configure(HttpServletResponse response) {
32+
String originKey = HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN;
33+
String credentialsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS;
34+
String headersKey = HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS;
35+
String methodsKey = HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS;
36+
String ageKey = HttpHeaders.ACCESS_CONTROL_MAX_AGE;
37+
38+
response.setHeader(originKey, ContextHolder.get().getRequest().getHeader(HttpHeaders.ORIGIN));
39+
response.setHeader(methodsKey, "OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE,TRACE");
40+
response.setHeader(headersKey, "Origin,X-Requested-With,Content-Type,Accept,geaflow-token,geaflow-task-token");
41+
response.setHeader(credentialsKey, "true");
42+
response.setHeader(ageKey, "3600");
43+
}
44+
}

geaflow-console/app/web/src/main/java/org/apache/geaflow/console/web/api/ErrorApiResponse.java

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,78 +21,34 @@
2121

2222
import javax.servlet.http.HttpServletResponse;
2323
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;
3226

3327
@Getter
3428
public class ErrorApiResponse<T> extends GeaflowApiResponse<T> {
3529

30+
private static final GeaflowExceptionClassifier EXCEPTION_CLASSIFIER = new GeaflowExceptionClassifier();
31+
3632
private final GeaflowApiRequest<?> request;
3733

3834
private final String message;
3935

4036
protected ErrorApiResponse(Throwable error) {
4137
super(false);
4238

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();
7343

7444
this.request = GeaflowApiRequest.currentRequest();
75-
this.message = message;
7645
}
7746

7847
@Override
7948
public void write(HttpServletResponse response) {
8049
response.reset();
81-
configCors(response);
50+
// Use centralized CORS configuration
51+
ErrorApiCorsConfigurer.configure(response);
8252
super.write(response);
8353
}
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-
}
9854
}

0 commit comments

Comments
 (0)