|
8 | 8 |
|
9 | 9 | public class DriverResponse { |
10 | 10 | 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 | + |
11 | 15 | ResponseSendException(Throwable e) { |
12 | 16 | 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 | + } |
13 | 66 | } |
14 | 67 | } |
15 | 68 |
|
16 | 69 | public String driver = "1.0.0"; |
17 | 70 | public String language = "C++"; |
18 | 71 | public String languageVersion = "14"; |
19 | | - public String status = "ok"; |
| 72 | + public Status status = Status.ok; |
20 | 73 | public ArrayList<String> errors = new ArrayList<String>(0); |
21 | 74 | @JsonProperty("ast") |
22 | 75 |
|
@@ -54,17 +107,23 @@ void send() throws ResponseSendException { |
54 | 107 | } |
55 | 108 | } |
56 | 109 |
|
57 | | - void sendError(Exception e, String errorString) |
58 | | - throws IOException { |
59 | | - |
| 110 | + void sendError(Exception e, String errorString) throws IOException { |
60 | 111 | translationUnit = null; |
61 | 112 | errors.add(e.getClass().getCanonicalName()); |
62 | 113 | errors.add(errorString + e.getMessage()); |
63 | 114 | StringWriter sw = new StringWriter(); |
64 | 115 | PrintWriter pw = new PrintWriter(sw); |
65 | 116 | e.printStackTrace(pw); |
66 | 117 | 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 | + |
68 | 127 | send(); |
69 | 128 | } |
70 | 129 | } |
0 commit comments