Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 23b0c02

Browse files
kuba--dennwc
authored andcommitted
catch cdt casting exceptions
Signed-off-by: kuba-- <[email protected]>
1 parent 5db4160 commit 23b0c02

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

native/src/main/java/tech/sourced/babelfish/DriverResponse.java

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,68 @@
88

99
public class DriverResponse {
1010
static class ResponseSendException extends IOException {
11+
private final static String CDT_PACKAGE = "org.eclipse.cdt";
12+
private final static int MAX_DEPTH = 3;
13+
private final Throwable e;
14+
1115
ResponseSendException(Throwable e) {
1216
super(e);
17+
this.e = e;
18+
}
19+
20+
/**
21+
* isCdtCastException tries to find a java.lang.ClassCastException
22+
* in the exception stack trace.
23+
* The function checks maximum MAX_DEPTH stack trace elements.
24+
*
25+
* For true cases, the function should return in the first iteration.
26+
* Less likely the ClassCastException will be wrapped by more than two
27+
* exceptions.
28+
*
29+
* @return true if the exception is ClassCastException and it comes from
30+
* CDT package.
31+
*/
32+
boolean isCdtCastException() {
33+
Throwable t = this.e;
34+
for (int i = 0; i < MAX_DEPTH; i++) {
35+
if (t == null) {
36+
break;
37+
}
38+
if (!(t instanceof ClassCastException)) {
39+
t = t.getCause();
40+
continue;
41+
}
42+
43+
final StackTraceElement[] st = t.getStackTrace();
44+
return st.length > 0 && st[0].getClassName().startsWith(CDT_PACKAGE);
45+
}
46+
47+
return false;
48+
}
49+
}
50+
51+
enum Status {
52+
ok {
53+
@Override public String toString() {
54+
return "ok";
55+
}
56+
},
57+
error {
58+
@Override public String toString() {
59+
return "error";
60+
}
61+
},
62+
fatal {
63+
@Override public String toString() {
64+
return "fatal";
65+
}
1366
}
1467
}
1568

1669
public String driver = "1.0.0";
1770
public String language = "C++";
1871
public String languageVersion = "14";
19-
public String status = "ok";
72+
public Status status = Status.ok;
2073
public ArrayList<String> errors = new ArrayList<String>(0);
2174
@JsonProperty("ast")
2275

@@ -54,17 +107,23 @@ void send() throws ResponseSendException {
54107
}
55108
}
56109

57-
void sendError(Exception e, String errorString)
58-
throws IOException {
59-
110+
void sendError(Exception e, String errorString) throws IOException {
60111
translationUnit = null;
61112
errors.add(e.getClass().getCanonicalName());
62113
errors.add(errorString + e.getMessage());
63114
StringWriter sw = new StringWriter();
64115
PrintWriter pw = new PrintWriter(sw);
65116
e.printStackTrace(pw);
66117
errors.add(sw.toString());
67-
status = "fatal";
118+
119+
if (e instanceof ResponseSendException) {
120+
status = ((ResponseSendException)e).isCdtCastException() ?
121+
Status.error :
122+
Status.fatal;
123+
} else {
124+
status = Status.fatal;
125+
}
126+
68127
send();
69128
}
70129
}

0 commit comments

Comments
 (0)