summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-09-17 22:10:48 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-09-17 22:27:42 -0700
commita056f36f6ccc46f9915732fbd7eddf603d4a67af (patch)
treee3524dd303d64a695ee3313c000a6549c96bfb0f /src/error.rs
parentbb029ea7cda848d51e19564919cc18a877a8791d (diff)
downloadssh2-rs-a056f36f6ccc46f9915732fbd7eddf603d4a67af.zip
Bind the SSH2 Agent
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs43
1 files changed, 41 insertions, 2 deletions
diff --git a/src/error.rs b/src/error.rs
index e3ea407..ae1f6c7 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,2 +1,41 @@
-#[deriving(Show)]
-pub struct Error(pub int);
+use std::fmt;
+use std::kinds::marker;
+use std::str;
+use libc;
+
+use {raw, Session};
+
+/// Representation of an error that can occur within libssh2
+pub struct Error {
+ code: libc::c_int,
+ msg: &'static str,
+ marker: marker::NoCopy,
+}
+
+impl Error {
+ /// Generate the last error that occurred for a `Session`.
+ ///
+ /// Returns `None` if there was no last error.
+ pub fn last_error(sess: &Session) -> Option<Error> {
+ unsafe {
+ let mut msg = 0 as *mut _;
+ let rc = raw::libssh2_session_last_error(sess.raw(), &mut msg,
+ 0 as *mut _, 0);
+ if rc == 0 { return None }
+ Some(Error {
+ code: rc,
+ msg: str::raw::c_str_to_static_slice(msg as *const _),
+ marker: marker::NoCopy,
+ })
+ }
+ }
+
+ /// Get the message corresponding to this error
+ pub fn message(&self) -> &str { self.msg }
+}
+
+impl fmt::Show for Error {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "[{}] {}", self.code, self.msg)
+ }
+}