Skip to content

Commit f17aca4

Browse files
committed
Wrap open-channel exceptions
When .openChannel throws an exception, wrap it in an ex-info exception. This allows easier procession of the exceptions in consuming code.
1 parent fa1c4a5 commit f17aca4

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/clj_ssh/ssh.clj

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
ByteArrayInputStream ByteArrayOutputStream
3737
PipedInputStream PipedOutputStream]
3838
[com.jcraft.jsch
39-
JSch Session Channel ChannelShell ChannelExec ChannelSftp
39+
JSch Session Channel ChannelShell ChannelExec ChannelSftp JSchException
4040
Identity IdentityFile Logger KeyPair LocalIdentityRepository]))
4141

4242
;;; forward jsch's logging to java logging
@@ -373,7 +373,28 @@ keys. All other option key pairs will be passed as SSH config options."
373373
(defn open-channel
374374
"Open a channel of the specified type in the session."
375375
[^Session session session-type]
376-
(.openChannel session (name session-type)))
376+
(try
377+
(.openChannel session (name session-type))
378+
(catch JSchException e
379+
(let [msg (.getMessage e)]
380+
(cond
381+
(= msg "session is down")
382+
(throw (ex-info (format "clj-ssh open-channel failure: %s" msg)
383+
{:type :clj-ssh/open-channel-failure
384+
:reason :clj-ssh/session-down}
385+
e))
386+
(= msg "channel is not opened.")
387+
(throw (ex-info
388+
(format
389+
"clj-ssh open-channel failure: %s (possible session timeout)"
390+
msg)
391+
{:type :clj-ssh/open-channel-failure
392+
:reason :clj-ssh/channel-open-failed}
393+
e))
394+
:else (throw (ex-info (format "clj-ssh open-channel failure: %s" msg)
395+
{:type :clj-ssh/open-channel-failure
396+
:reason :clj-ssh/unknown}
397+
e)))))))
377398

378399
(defn sftp-channel
379400
"Open a SFTP channel in the session."

0 commit comments

Comments
 (0)