01: package com.mockrunner.base;
02:
03: import java.io.PrintWriter;
04: import java.io.StringWriter;
05:
06: /**
07: * If Mockrunner catches an exception inside application code,
08: * it rethrows it as an instance of this class.
09: */
10: public class NestedApplicationException extends RuntimeException {
11: private Throwable nested;
12:
13: public NestedApplicationException(String message, Throwable nested) {
14: super (message);
15: this .nested = nested;
16: }
17:
18: public NestedApplicationException(Throwable nested) {
19: this .nested = nested;
20: }
21:
22: /**
23: * Returns the nested exception
24: * (which may also be a <code>NestedApplicationException</code>)
25: * @return the nested exception
26: */
27: public Throwable getNested() {
28: return nested;
29: }
30:
31: /**
32: * Returns the root cause, i.e. the first exception that is
33: * not an instance of <code>NestedApplicationException</code>.
34: * @return the root exception
35: */
36: public Throwable getRootCause() {
37: if (nested == null)
38: return null;
39: if (!(nested instanceof NestedApplicationException))
40: return nested;
41: return ((NestedApplicationException) nested).getRootCause();
42: }
43:
44: public String getMessage() {
45: StringWriter writer = new StringWriter();
46: PrintWriter printWriter = new PrintWriter(writer);
47: String message = super .getMessage();
48: if (null != message) {
49: printWriter.println(super .getMessage());
50: } else {
51: printWriter.println();
52: }
53: Throwable cause = getRootCause();
54: if (null != cause) {
55: printWriter.print("Cause: ");
56: cause.printStackTrace(printWriter);
57: }
58: writer.flush();
59: return writer.toString();
60: }
61: }
|