Skip to content

Commit 4cdb2d4

Browse files
authored
Preserve cause when rethrowing exceptions (#52)
1 parent 5720d52 commit 4cdb2d4

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/main/java/net/juniper/netconf/Device.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public Device(JSch sshClient,
139139
try {
140140
builder = factory.newDocumentBuilder();
141141
} catch (ParserConfigurationException e) {
142-
throw new NetconfException(String.format("Error creating XML Parser: %s", e.getMessage()));
142+
throw new NetconfException(String.format("Error creating XML Parser: %s", e.getMessage()), e);
143143
}
144144

145145
this.netconfCapabilities = (netconfCapabilities != null) ? netconfCapabilities : getDefaultClientCapabilities();
@@ -190,7 +190,7 @@ private NetconfSession createNetconfSession() throws NetconfException {
190190
sshClient.setKnownHosts(hostKeysFileName);
191191
}
192192
} catch (JSchException e) {
193-
throw new NetconfException(String.format("Error loading known hosts file: %s", e.getMessage()));
193+
throw new NetconfException(String.format("Error loading known hosts file: %s", e.getMessage()), e);
194194
}
195195
sshClient.setHostKeyRepository(sshClient.getHostKeyRepository());
196196
log.info("Connecting to host {} on port {}.", hostName, port);
@@ -203,7 +203,7 @@ private NetconfSession createNetconfSession() throws NetconfException {
203203
try {
204204
sshSession.setTimeout(connectionTimeout);
205205
} catch (JSchException e) {
206-
throw new NetconfException(String.format("Error setting session timeout: %s", e.getMessage()));
206+
throw new NetconfException(String.format("Error setting session timeout: %s", e.getMessage()), e);
207207
}
208208
if (sshSession.isConnected()) {
209209
log.info("Connected to host {} - Timeout set to {} msecs.", hostName, sshSession.getTimeout());
@@ -231,7 +231,7 @@ private Session loginWithUserPass(int timeoutMilliSeconds) throws NetconfExcepti
231231
return session;
232232
} catch (JSchException e) {
233233
throw new NetconfException(String.format("Error connecting to host: %s - Error: %s",
234-
hostName, e.getMessage()));
234+
hostName, e.getMessage()), e);
235235
}
236236
}
237237

@@ -266,7 +266,7 @@ private void loadPrivateKey() throws NetconfException {
266266
try {
267267
sshClient.addIdentity(pemKeyFile);
268268
} catch (JSchException e) {
269-
throw new NetconfException(String.format("Error parsing the pemKeyFile: %s", e.getMessage()));
269+
throw new NetconfException(String.format("Error parsing the pemKeyFile: %s", e.getMessage()), e);
270270
}
271271
}
272272

@@ -332,7 +332,7 @@ public String runShellCommand(String command) throws IOException {
332332
try {
333333
channel = (ChannelExec) sshSession.openChannel("exec");
334334
} catch (JSchException e) {
335-
throw new NetconfException(String.format("Failed to open exec session: %s", e.getMessage()));
335+
throw new NetconfException(String.format("Failed to open exec session: %s", e.getMessage()), e);
336336
}
337337
channel.setCommand(command);
338338
InputStream stdout;
@@ -347,7 +347,7 @@ public String runShellCommand(String command) throws IOException {
347347
try {
348348
line = bufferReader.readLine();
349349
} catch (Exception e) {
350-
throw new NetconfException(e.getMessage());
350+
throw new NetconfException(e.getMessage(), e);
351351
}
352352
if (line == null || line.equals(NetconfConstants.EMPTY_LINE))
353353
break;
@@ -377,7 +377,7 @@ public BufferedReader runShellCommandRunning(String command)
377377
try {
378378
channel = (ChannelExec) sshSession.openChannel("exec");
379379
} catch (JSchException e) {
380-
throw new NetconfException(String.format("Failed to open exec session: %s", e.getMessage()));
380+
throw new NetconfException(String.format("Failed to open exec session: %s", e.getMessage()), e);
381381
}
382382
InputStream stdout = channel.getInputStream();
383383
return new BufferedReader(new InputStreamReader(stdout, Charset.defaultCharset()));

src/main/java/net/juniper/netconf/NetconfSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class NetconfSession {
100100
netconfChannel.connect(connectionTimeout);
101101
} catch (JSchException e) {
102102
throw new NetconfException("Failed to create Netconf session:" +
103-
e.getMessage());
103+
e.getMessage(), e);
104104
}
105105
this.netconfChannel = netconfChannel;
106106
this.commandTimeout = commandTimeout;

0 commit comments

Comments
 (0)