Skip to content

Commit 90e7016

Browse files
authored
Fix exception wrapping to retain original message (#1265)
* added the parameterised error handling * _ * added junit tests for the ZinggClientException class * _ * _ * remote redundent printstacktrace * error messages changed
1 parent 7ee7c55 commit 90e7016

25 files changed

Lines changed: 122 additions & 56 deletions

File tree

common/client/src/main/java/zingg/common/client/Client.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public Client(IZArgs args, ClientOptions options, String zFactory) throws ZinggC
6262
setZingg(args, options);
6363
}
6464
catch (Exception e) {
65-
e.printStackTrace();
66-
throw new ZinggClientException("An error has occured while setting up the client" + e.getMessage());
65+
throw new ZinggClientException("An error has occured while setting up the client", e);
6766
}
6867
}
6968

common/client/src/main/java/zingg/common/client/ZinggClientException.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@
1010
public class ZinggClientException extends Throwable {
1111

1212
private static final long serialVersionUID = 1L;
13-
14-
public String message;
1513

1614
public ZinggClientException(String m) {
1715
super(m);
18-
this.message = m;
1916
}
2017

2118
public ZinggClientException(String m, Throwable cause) {
2219
super(m, cause);
23-
this.message = m;
2420
}
2521

2622
public ZinggClientException(Throwable cause) {
2723
super(cause);
28-
this.message = cause.getMessage();
2924
}
3025

3126
}

common/client/src/main/java/zingg/common/client/arguments/loader/template/TemplateFileArgumentLoader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public A load(String path) throws ZinggClientException {
2323
String substituted = substitutor.substitute(content, System.getenv());
2424
return objectMapper.readValue(substituted, argsClass);
2525
} catch (Exception | ZinggClientException exception) {
26-
exception.printStackTrace();
27-
throw new ZinggClientException("Unable to load template from: " + path);
26+
throw new ZinggClientException("Unable to load template from: " + path, exception);
2827
}
2928
}
3029
}

common/client/src/main/java/zingg/common/client/arguments/writer/FileArgumentsWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void write(String filePath, IZArgs args) throws ZinggClientException {
1212
try {
1313
objectMapper.writeValue(new File(filePath), args);
1414
} catch (Exception exception) {
15-
throw new ZinggClientException("Error writing config to file: " + filePath);
15+
throw new ZinggClientException("Error writing config to file: " + filePath, exception);
1616
}
1717
}
1818
}

common/client/src/main/java/zingg/common/client/util/vertical/VerticalDisplayUtility.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ZFrame<D, R, C> convertVertical(ZFrame<D, R, C> zFrame) throws ZinggClien
4747
}
4848
return dfObjectUtil.getDFFromObjectList(samples, VerticalDisplayTwoRowModel.class);
4949
} catch (Exception exception) {
50-
throw new ZinggClientException("Error occurred while converting to vertical, " + exception.getMessage());
50+
throw new ZinggClientException("Error occurred while converting to vertical, ", exception);
5151
}
5252
}
5353

common/client/src/main/java/zingg/common/client/util/writer/PipeUtilWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public final void write(ZFrame<D, R, C> toWriteOrig, Pipe<D, R, C>... pipes) thr
1818
writer.write(pipe);
1919
}
2020
} catch (Exception ex) {
21-
throw new ZinggClientException(ex.getMessage());
21+
throw new ZinggClientException("Error writing output", ex);
2222
}
2323
}
2424
protected abstract IDFWriter<D,R,C> getWriter(ZFrame<D, R, C> input, Pipe<D, R, C> pipe);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package zingg.common.client;
2+
import static org.junit.jupiter.api.Assertions.*;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
class ZinggClientExceptionTest {
7+
8+
@Test
9+
void testMessageOnlyConstructor() {
10+
String msg = "simple error";
11+
12+
ZinggClientException ex = new ZinggClientException(msg);
13+
14+
assertEquals(msg, ex.getMessage());
15+
assertNull(ex.getCause());
16+
}
17+
18+
@Test
19+
void testMessageAndCauseConstructor() {
20+
String msg = "wrapped error";
21+
Throwable cause = new Exception("root cause");
22+
23+
ZinggClientException ex = new ZinggClientException(msg, cause);
24+
25+
assertEquals(msg, ex.getMessage());
26+
assertNotNull(ex.getCause());
27+
assertSame(cause, ex.getCause());
28+
assertEquals("root cause", ex.getCause().getMessage());
29+
}
30+
31+
@Test
32+
void testCauseOnlyConstructor() {
33+
Throwable cause = new Exception("original failure");
34+
35+
ZinggClientException ex = new ZinggClientException(cause);
36+
37+
assertEquals(cause.toString(), ex.getMessage());
38+
assertNotNull(ex.getCause());
39+
assertSame(cause, ex.getCause());
40+
}
41+
42+
@Test
43+
void testExceptionChainingDepth() {
44+
Throwable root = new Exception("level 1");
45+
ZinggClientException level2 = new ZinggClientException("level 2", root);
46+
ZinggClientException level3 = new ZinggClientException("level 3", level2);
47+
48+
assertEquals("level 3", level3.getMessage());
49+
assertSame(level2, level3.getCause());
50+
assertSame(root, level3.getCause().getCause());
51+
assertNull(level3.getCause().getCause().getCause());
52+
}
53+
54+
@Test
55+
void shouldPreserveMultipleExceptionLayers() {
56+
57+
ZinggClientException top =
58+
assertThrows(ZinggClientException.class, () -> {
59+
60+
try {
61+
try {
62+
throw new Exception("root exception");
63+
} catch (Exception e) {
64+
throw new ZinggClientException("level 1 exception", e);
65+
}
66+
} catch (ZinggClientException e) {
67+
throw new ZinggClientException("level 2 exception", e);
68+
}
69+
70+
});
71+
72+
assertEquals("level 2 exception", top.getMessage());
73+
74+
Throwable level1 = top.getCause();
75+
assertNotNull(level1);
76+
assertTrue(level1 instanceof ZinggClientException);
77+
assertEquals("level 1 exception", level1.getMessage());
78+
79+
Throwable root = level1.getCause();
80+
assertNotNull(root);
81+
assertEquals(Exception.class, root.getClass());
82+
assertEquals("root exception", root.getMessage());
83+
84+
assertNull(root.getCause());
85+
}
86+
}

common/core/src/main/java/zingg/common/core/documenter/DataDocumenter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public void process() throws ZinggClientException {
4646
}
4747
LOG.info("Data document generation finishes");
4848
} catch (Exception e) {
49-
e.printStackTrace();
50-
throw new ZinggClientException(e.getMessage());
49+
throw new ZinggClientException("Error while documenting data ", e);
5150
}
5251
}
5352

common/core/src/main/java/zingg/common/core/documenter/DocumenterBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public void writeDocument(String template, Map<String, Object> root, String file
6363
temp.process(root, file);
6464
file.close();
6565
} catch (Exception e) {
66-
e.printStackTrace();
67-
throw new ZinggClientException(e.getMessage());
66+
throw new ZinggClientException("Error while writing document " + fileName, e);
6867
}
6968
}
7069

common/core/src/main/java/zingg/common/core/documenter/ModelDocumenter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public void createModelDocument() throws ZinggClientException {
5858

5959
LOG.info("Model document generation finishes");
6060
} catch (Exception e) {
61-
e.printStackTrace();
62-
throw new ZinggClientException(e.getMessage());
61+
throw new ZinggClientException("Error while documenting model ", e);
6362
}
6463
}
6564

0 commit comments

Comments
 (0)