Revision 8faab8c3
Added by Andreas Müller over 6 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/common/IoResultBase.java | ||
---|---|---|
31 | 31 |
private static final long serialVersionUID = -8077358746590123757L; |
32 | 32 |
String message; |
33 | 33 |
Exception exception; |
34 |
String location; |
|
34 |
String codeLocation; |
|
35 |
String dataLocation; |
|
35 | 36 |
private IoInfo(String msg, Exception e){ |
36 | 37 |
this.message = msg; |
37 | 38 |
this.exception = e; |
38 | 39 |
} |
39 |
private IoInfo(String msg, Exception e, String location){ |
|
40 |
// private IoInfo(String msg, Exception e, String location){ |
|
41 |
// this.message = msg; |
|
42 |
// this.exception = e; |
|
43 |
// this.codeLocation = location; |
|
44 |
// } |
|
45 |
private IoInfo(String msg, Exception e, String codeLocation, String dataLocation){ |
|
40 | 46 |
this.message = msg; |
41 | 47 |
this.exception = e; |
42 |
this.location = location; |
|
48 |
this.codeLocation = codeLocation; |
|
49 |
this.dataLocation = dataLocation; |
|
43 | 50 |
} |
44 | 51 |
|
45 | 52 |
public String getMessage(){ |
... | ... | |
49 | 56 |
public Exception getException(){ |
50 | 57 |
return exception; |
51 | 58 |
} |
59 |
public String getCodeLocation(){ |
|
60 |
return codeLocation; |
|
61 |
} |
|
62 |
public String getDataLocation(){ |
|
63 |
return dataLocation; |
|
64 |
} |
|
52 | 65 |
} |
53 | 66 |
|
54 | 67 |
|
... | ... | |
58 | 71 |
public List<IoInfo> getErrors() {return errors;} |
59 | 72 |
public void setErrors(List<IoInfo> ioInfos) {this.errors = ioInfos;} |
60 | 73 |
public void addError(String message) { |
61 |
addError(message, null, null);
|
|
74 |
addError(message, null, getLocationByException());
|
|
62 | 75 |
} |
63 | 76 |
public void addError(String message, Exception e) { |
64 |
addError(message, e, null); |
|
77 |
addError(message, e, null, null);
|
|
65 | 78 |
} |
66 | 79 |
public void addError(String message, int location) { |
67 |
addError(message, null, String.valueOf(location)); |
|
80 |
addError(message, null, getLocationByException(), String.valueOf(location)); |
|
81 |
} |
|
82 |
public void addError(String message, String codeLocation) { |
|
83 |
addError(message, null, codeLocation, null); |
|
68 | 84 |
} |
69 |
public void addError(String message, String location) {
|
|
70 |
addError(message, null, location);
|
|
85 |
public void addError(String message, Exception e, String codeLocation) {
|
|
86 |
addError(message, e, codeLocation, null);
|
|
71 | 87 |
} |
72 |
public void addError(String message, Exception e, String location) {
|
|
73 |
errors.add(new IoInfo(message, e, makeLocation(e, location)));
|
|
88 |
public void addError(String message, Exception e, String codeLocation, String dataLocation) {
|
|
89 |
errors.add(new IoInfo(message, e, makeLocation(e, codeLocation), dataLocation));
|
|
74 | 90 |
} |
75 | 91 |
|
92 |
|
|
76 | 93 |
public List<IoInfo> getWarnings() {return warnings;} |
77 | 94 |
public void setWarnings(List<IoInfo> warnings) {this.warnings = warnings;} |
78 | 95 |
public void addWarning(String message) { |
79 |
// warnings.add(warning.getBytes(StandardCharsets.UTF_8)); |
|
80 |
addWarning(message, null); |
|
96 |
addWarning(message, getLocationByException(), null); |
|
81 | 97 |
} |
82 | 98 |
public void addWarning(String message, int location) { |
83 |
addWarning(message, String.valueOf(location)); |
|
99 |
addWarning(message, null, String.valueOf(location)); |
|
100 |
} |
|
101 |
public void addWarning(String message, String codeLocation) { |
|
102 |
addWarning(message, codeLocation, null); |
|
84 | 103 |
} |
85 |
public void addWarning(String message, String location) {
|
|
86 |
warnings.add(new IoInfo(message, null, location));
|
|
104 |
public void addWarning(String message, String codeLocation, String dataLocation) {
|
|
105 |
warnings.add(new IoInfo(message, null, codeLocation, dataLocation));
|
|
87 | 106 |
} |
88 | 107 |
|
108 |
|
|
109 |
|
|
89 | 110 |
public List<IoInfo> getExceptions() {return exceptions;} |
90 | 111 |
public void setExceptions(List<IoInfo> exceptions) {this.exceptions = exceptions;} |
91 | 112 |
public void addException(Exception e) { |
92 |
addException(e, null, null); |
|
93 |
setExceptionState(); |
|
113 |
addException(e, null, null, null); |
|
94 | 114 |
} |
95 | 115 |
public void addException(Exception e, String message) { |
96 |
addException(e, message, null); |
|
97 |
setExceptionState(); |
|
116 |
addException(e, message, null, null); |
|
98 | 117 |
} |
99 |
public void addException(Exception e, String message, String location) { |
|
100 |
exceptions.add(new IoInfo(message, e, makeLocation(e, location))); |
|
118 |
public void addException(Exception e, String message, String codeLocation) { |
|
119 |
addException(e, message, codeLocation, null); |
|
120 |
} |
|
121 |
public void addException(Exception e, String message, String codeLocation, String dataLocation) { |
|
122 |
exceptions.add(new IoInfo(message, e, makeLocation(e, codeLocation), dataLocation)); |
|
101 | 123 |
setExceptionState(); |
102 | 124 |
} |
103 | 125 |
|
... | ... | |
115 | 137 |
StackTraceElement[] stackTrace = e.getStackTrace(); |
116 | 138 |
if (stackTrace != null && stackTrace.length > 0){ |
117 | 139 |
StackTraceElement el = stackTrace[0]; |
118 |
location = el.getMethodName() + "(" + el.getClassName() + ":" + el.getLineNumber() + ")";
|
|
140 |
location = locByStackTraceElement(el);
|
|
119 | 141 |
} |
120 | 142 |
} |
121 | 143 |
return location; |
122 | 144 |
} |
145 |
private String getLocationByException() { |
|
146 |
try { |
|
147 |
throw new RuntimeException(); |
|
148 |
} catch (Exception e) { |
|
149 |
StackTraceElement st = e.getStackTrace()[2]; |
|
150 |
return locByStackTraceElement(st); |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
private String locByStackTraceElement(StackTraceElement st) { |
|
155 |
return st.getMethodName() + "(" + st.getClassName()+ ":" + st.getLineNumber() + ")"; |
|
156 |
} |
|
123 | 157 |
|
124 | 158 |
protected abstract void setExceptionState(); |
125 | 159 |
|
... | ... | |
168 | 202 |
if (!list.isEmpty()){ |
169 | 203 |
report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "=")); |
170 | 204 |
for (IoInfo ioInfo : list){ |
171 |
String location = ioInfo.location == null ? "" : (ioInfo.location + ": ");
|
|
205 |
String location = ioInfo.codeLocation == null ? "" : (ioInfo.codeLocation + ": ");
|
|
172 | 206 |
String message = ioInfo.message != null ? ioInfo.message : ioInfo.exception != null ? ioInfo.exception.getMessage() : ""; |
173 | 207 |
|
174 | 208 |
message = StringUtils.isBlank(message)? "no message" : message; |
Also available in: Unified diff
ref #6519 improve result handling by codeLocation extraction and adding dataLocation