1 module libssh.errors; 2 3 import std.format; 4 5 import libssh.c_bindings.libssh; 6 import libssh.c_bindings.sftp; 7 import libssh.c_bindings.server; 8 import libssh.utils; 9 10 enum SSHError : int { 11 NoError = ssh_error_types_e.SSH_NO_ERROR, 12 RequestDenied = ssh_error_types_e.SSH_REQUEST_DENIED, 13 Fatal = ssh_error_types_e.SSH_FATAL, 14 EIntr = ssh_error_types_e.SSH_EINTR, 15 16 Error = SSH_ERROR, 17 Again = SSH_AGAIN, 18 EOF = SSH_EOF 19 } 20 21 enum SFTPError { 22 Ok = SSH_FX_OK, 23 Eof = SSH_FX_EOF, 24 NoSuchFile = SSH_FX_NO_SUCH_FILE, 25 PermissionDenied = SSH_FX_PERMISSION_DENIED, 26 Failure = SSH_FX_FAILURE, 27 BadMessage = SSH_FX_BAD_MESSAGE, 28 NoConnection = SSH_FX_NO_CONNECTION, 29 ConnectionLost = SSH_FX_CONNECTION_LOST, 30 OpUnsupported = SSH_FX_OP_UNSUPPORTED, 31 InvalidHandle = SSH_FX_INVALID_HANDLE, 32 NoSuchPath = SSH_FX_NO_SUCH_PATH, 33 FileAlreadyExists = SSH_FX_FILE_ALREADY_EXISTS, 34 WriteProtect = SSH_FX_WRITE_PROTECT, 35 NoMedia = SSH_FX_NO_MEDIA, 36 37 Unknown 38 } 39 40 class SSHException : Exception { 41 @property SSHError errorCode() { 42 return cast(SSHError) this._errorCode; 43 } 44 45 this(string errorMessage) { 46 this._errorCode = SSHError.Fatal; 47 super(errorMessage); 48 } 49 50 this(int errorCode) { 51 this._errorCode = errorCode; 52 super(format("SSH error with code %d", errorCode)); 53 } 54 55 this(int errorCode, string errorMessage) { 56 this._errorCode = errorCode; 57 super(errorMessage); 58 } 59 60 package this(ssh_session session) { 61 this._errorCode = ssh_get_error_code(session); 62 super(copyFromStrZ(ssh_get_error(session))); 63 } 64 65 package this(ssh_bind bind) { 66 this._errorCode = ssh_get_error_code(bind); 67 super(copyFromStrZ(ssh_get_error(bind))); 68 } 69 70 private int _errorCode; 71 } 72 73 class SFTPException : SSHException { 74 @property sftpErrorCode() { 75 return cast(SFTPError) this._sftpErrorCode; 76 } 77 78 this(int errorCode, ssh_session session) { 79 super(session); 80 _sftpErrorCode = errorCode; 81 } 82 83 private int _sftpErrorCode; 84 } 85 86 87 package { 88 void checkForRCError(T, CtorArgs...)(T rc, CtorArgs args) { 89 if (rc != SSH_OK) { 90 throw new SSHException(args); 91 } 92 } 93 94 void checkForNullError(T, CtorArgs...)(T rc, CtorArgs args) { 95 if (rc is null) { 96 throw new SSHException(args); 97 } 98 } 99 }