1
2
3
4
5
6 package pt.digitalis.dif.exception;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import pt.digitalis.dif.utils.ObjectFormatter;
12
13
14
15
16
17
18
19
20 abstract public class DIFException extends Exception implements IContextException {
21
22
23 private static final long serialVersionUID = -462214831205636575L;
24
25
26 private Map<String, Object> exceptionContext;
27
28
29
30
31
32
33
34 public DIFException(Exception exception)
35 {
36 super(exception);
37 }
38
39
40
41
42
43
44
45 public DIFException(String reason)
46 {
47 super(reason);
48 }
49
50
51
52
53
54
55
56
57
58 public DIFException(String reason, Exception exception)
59 {
60 super(reason, exception);
61 }
62
63
64
65
66 public void addToExceptionContext(String key, Object value)
67 {
68 if (this.exceptionContext == null)
69 this.exceptionContext = new HashMap<String, Object>();
70
71 this.exceptionContext.put(key, value);
72 }
73
74
75
76
77 public Map<String, Object> getExceptionContext()
78 {
79 return exceptionContext;
80 }
81
82
83
84
85 public String getRenderedExceptionContext()
86 {
87
88 ObjectFormatter formatter = new ObjectFormatter();
89 formatter.addItemIfNotNull("Cause", getCause());
90 formatter.addItemIfNotNull("Message", getMessage());
91 formatter.addItemIfNotNull("Exception Context", exceptionContext);
92
93 return "DIF Exception:\n" + formatter.getFormatedObject() + "\n";
94 }
95
96
97
98
99 public void setExceptionContext(Map<String, Object> exceptionContext)
100 {
101 if (this.exceptionContext == null)
102 this.exceptionContext = new HashMap<String, Object>();
103
104 this.exceptionContext = exceptionContext;
105 }
106 }