forked from kohsuke/com4j
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComException.java
More file actions
145 lines (129 loc) · 4.09 KB
/
ComException.java
File metadata and controls
145 lines (129 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com4j;
/**
* Signals a failure in the COM method invocation.
*
* <p>
* Calling a wrapped COM method throws this exception
* when the underlying COM method returns a failure HRESULT code.
*
* @author Kohsuke Kawaguchi (kk@kohsuke.org)
*/
@SuppressWarnings("serial")
public class ComException extends RuntimeException {
private final int hresult;
private final String fileName;
private final int line;
private ErrorInfo errorInfo;
/**
* Constructs a new ComException with the given values
* @param msg the message text of the Exception
* @param hresult the HRESULT value of the COM error
* @param fileName the file name of the source where the error occurred
* @param line the line in the file where the error occurred
*/
public ComException( String msg, int hresult, String fileName, int line ) {
super(Integer.toHexString(hresult)+' '+cutEOL(msg));
this.hresult = hresult;
this.fileName = fileName;
this.line = line;
}
/**
* Constructs a new ComException with the given values.
* @param msg the message text of the Exception
* @param fileName the file name of the source where the error occurred
* @param line the line in the file where the error occurred
*/
public ComException( String msg, String fileName, int line ) {
super(msg);
this.hresult = -1;
this.fileName = fileName;
this.line = line;
}
/**
* Constructs a new ComException with the given values
* @param msg the message text of the Exception
* @param hresult the HRESULT value of the COM error
*/
public ComException(String msg, int hresult) {
this(msg,hresult,null,-1);
}
/**
* Constructs a new ComException with the given ComExcepton as cause.
* @param cause the line in the file where the error occurred
*/
public ComException(ComException cause) {
super(cause.getDetailMessage(),cause);
this.hresult = cause.hresult;
this.fileName = cause.fileName;
this.line = cause.line;
this.errorInfo = cause.errorInfo;
}
/**
* Sets the ErrorInfo of this ComException
* @param errorInfo the new ErrorInfo
*/
/*package*/ void setErrorInfo(ErrorInfo errorInfo) {
this.errorInfo = errorInfo;
}
/**
* Gets the {@link ErrorInfo} object associated with this error.
*
* <p>
* Some COM objects can report additional error information beyond
* simple HRESULT value. If an error came from such an COM object,
* this method returns a non-null value, and you can query the returned
* {@link ErrorInfo} object for more information about the error.
*
* @return
* null if this exception doesn't have such detailed information.
*/
public ErrorInfo getErrorInfo() {
return errorInfo;
}
/**
* Returns the HRESULT value of this error.
* @return the HRESULT value of this error
*/
public int getHRESULT() {
return hresult;
}
/**
* Cuts off the end of line characters.
* @param s the original String
* @return
*/
private static String cutEOL( String s ) {
if(s==null)
return "(Unknown error)";
if(s.endsWith("\r\n"))
return s.substring(0,s.length()-2);
else
return s;
}
@Override
public String getMessage() {
if(errorInfo!=null && errorInfo.getDescription()!=null) {
return super.getMessage()+" : "+errorInfo.getDescription();
}
String s = Native.getErrorMessage(hresult);
if(s!=null) {
return super.getMessage()+" : "+s;
}
return super.getMessage();
}
/**
* Returns the message of the superclass
* @return the message of the superclass
*/
public String getDetailMessage() {
return super.getMessage();
}
@Override
public String toString() {
String s = super.toString();
if(fileName!=null) {
s += " : "+fileName+':'+line;
}
return s;
}
}