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

Commit

Permalink
catch cdt casting exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: kuba-- <kuba@sourced.tech>
  • Loading branch information
kuba-- authored and dennwc committed Sep 20, 2019
1 parent 5db4160 commit 23b0c02
Showing 1 changed file with 64 additions and 5 deletions.
69 changes: 64 additions & 5 deletions native/src/main/java/tech/sourced/babelfish/DriverResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,68 @@

public class DriverResponse {
static class ResponseSendException extends IOException {
private final static String CDT_PACKAGE = "org.eclipse.cdt";
private final static int MAX_DEPTH = 3;
private final Throwable e;

ResponseSendException(Throwable e) {
super(e);
this.e = e;
}

/**
* isCdtCastException tries to find a java.lang.ClassCastException
* in the exception stack trace.
* The function checks maximum MAX_DEPTH stack trace elements.
*
* For true cases, the function should return in the first iteration.
* Less likely the ClassCastException will be wrapped by more than two
* exceptions.
*
* @return true if the exception is ClassCastException and it comes from
* CDT package.
*/
boolean isCdtCastException() {
Throwable t = this.e;
for (int i = 0; i < MAX_DEPTH; i++) {
if (t == null) {
break;
}
if (!(t instanceof ClassCastException)) {
t = t.getCause();
continue;
}

final StackTraceElement[] st = t.getStackTrace();
return st.length > 0 && st[0].getClassName().startsWith(CDT_PACKAGE);
}

return false;
}
}

enum Status {
ok {
@Override public String toString() {
return "ok";
}
},
error {
@Override public String toString() {
return "error";
}
},
fatal {
@Override public String toString() {
return "fatal";
}
}
}

public String driver = "1.0.0";
public String language = "C++";
public String languageVersion = "14";
public String status = "ok";
public Status status = Status.ok;
public ArrayList<String> errors = new ArrayList<String>(0);
@JsonProperty("ast")

Expand Down Expand Up @@ -54,17 +107,23 @@ void send() throws ResponseSendException {
}
}

void sendError(Exception e, String errorString)
throws IOException {

void sendError(Exception e, String errorString) throws IOException {
translationUnit = null;
errors.add(e.getClass().getCanonicalName());
errors.add(errorString + e.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
errors.add(sw.toString());
status = "fatal";

if (e instanceof ResponseSendException) {
status = ((ResponseSendException)e).isCdtCastException() ?
Status.error :
Status.fatal;
} else {
status = Status.fatal;
}

send();
}
}

0 comments on commit 23b0c02

Please sign in to comment.