Error Handling
NullOpsDevs.LibSsh reports errors by throwing SshException when operations fail. Understanding how errors are thrown and what they mean is essential for building robust SSH applications.
How Errors Are Thrown
The library throws SshException when SSH operations fail. This exception contains:
Message: Human-readable error description
Error: An
SshErrorenum value indicating the specific error typeInnerException: Optional underlying exception that caused the error
Basic Error Handling
Always wrap SSH operations in try-catch blocks:
Error Types Reference
Connection Errors
These errors occur during connection establishment:
Error Code | Description | Common Causes |
|---|---|---|
| No socket established | Called operation before connecting |
| Failed to receive SSH banner | Network issue, wrong port, non-SSH service |
| Failed to send SSH banner | Network issue, connection interrupted |
| No SSH banner exchanged | Protocol mismatch, connection failed |
| Socket disconnected | Network interruption, server closed connection |
| Failed to send data | Network issue, connection lost |
| Failed to receive data | Network issue, connection lost |
| Invalid socket descriptor | Socket closed or corrupted |
| Operation timed out | Network latency, server not responding |
| Socket operation timed out | Network issue, timeout settings too low |
Key Exchange Errors
These errors occur during the SSH handshake and key exchange:
Error Code | Description | Common Causes |
|---|---|---|
| Key exchange negotiation failed | No common algorithms supported |
| Key exchange process failed | Incompatible SSH versions, crypto failure |
| Host key initialization failed | Server key corrupted, unsupported key type |
| Host key signing failed | Server key error, crypto failure |
| Algorithm not supported | Client/server algorithm mismatch |
Authentication Errors
These errors occur during user authentication:
Error Code | Description | Common Causes |
|---|---|---|
| Authentication failed | Wrong credentials, method not allowed |
| Public key not verified | Key not authorized on server |
| Key file authentication failed | Invalid key file, wrong passphrase |
| Password has expired | Account password needs reset |
| No authentication method available | Server doesn't support requested method |
| SSH agent protocol error | Agent not running, communication failure |
| Expected authentication banner missing | Protocol issue, unusual server config |
Channel Errors
These errors occur during SSH channel operations (command execution, file transfers):
Error Code | Description | Common Causes |
|---|---|---|
| General channel failure | Channel operation failed |
| Channel request denied by server | Permission denied, invalid request |
| Unknown or invalid channel | Channel closed, internal error |
| Channel packets out of order | Protocol error, corrupted data |
| Channel has been closed | Channel already closed |
| EOF already sent on channel | Cannot send more data |
| Channel window size exceeded | Flow control issue |
| Channel packet size exceeded | Packet too large |
| Channel window is full | Cannot send until window adjusted |
File Transfer Errors
These errors occur during SCP or SFTP operations:
Error Code | Description | Common Causes |
|---|---|---|
| SCP protocol error | File doesn't exist, permission denied |
| SFTP protocol error | SFTP subsystem unavailable |
| File operation failed | File not found, permission denied |
Cryptographic Errors
These errors occur during encryption/decryption operations:
Error Code | Description | Common Causes |
|---|---|---|
| MAC verification failed | Data corrupted or tampered with |
| Decryption failed | Corrupted data, wrong key |
| Encryption failed | Crypto library error |
| MAC operation failed | Crypto operation error |
| Hash initialization failed | Crypto library issue |
| Hash calculation failed | Crypto operation error |
Resource Errors
These errors occur due to resource limitations:
Error Code | Description | Common Causes |
|---|---|---|
| Memory allocation failed | Out of memory |
| Buffer too small | Internal buffer sizing issue |
| Out-of-boundary access | Internal error, corrupted data |
| Random number generation failed | Crypto library issue |
Compression Errors
These errors occur during data compression:
Error Code | Description | Common Causes |
|---|---|---|
| Compression/decompression error | zlib error, corrupted data |
| Compression failed | Compression algorithm error |
Protocol Errors
These errors indicate SSH protocol violations:
Error Code | Description | Common Causes |
|---|---|---|
| SSH protocol error | Protocol violation, incompatible versions |
| Request denied by server | Server rejected request |
| Method not supported | Server doesn't support requested feature |
| Public key protocol error | Public key subsystem error |
Known Hosts Errors
These errors occur during known hosts operations:
Error Code | Description | Common Causes |
|---|---|---|
| Known hosts file operation failed | File access issue, parse error |
API Usage Errors
These errors indicate incorrect use of the library:
Error Code | Description | Common Causes |
|---|---|---|
| Incorrect API usage | Wrong parameter, invalid state |
| Invalid argument | Invalid parameter value |
| Invalid polling type | Non-blocking mode error |
| Developer error (custom) | Session in wrong state, invalid operation |
| Session init failed (custom) | Library initialization error |
| Inner exception occurred (custom) | See InnerException property |
Special Errors
Error Code | Description | Notes |
|---|---|---|
| No error occurred | Operation successful |
| Operation would block | Non-blocking mode only (library uses blocking mode) |
Handling Specific Errors
Connection Failures
Authentication Failures
Command Execution Errors
File Transfer Errors
Comprehensive Error Handling Example
Best Practices
Always use try-catch-finally:
Catch
SshExceptionfor SSH-specific errorsUse
finallyblock to dispose the session
Check specific error codes:
Use
whenclauses to handle specific errors differentlyProvide user-friendly messages based on error type
Don't ignore errors:
Log all errors for troubleshooting
Provide meaningful feedback to users
Dispose sessions properly:
Always dispose sessions in
finallyblocks or useusingstatementsPrevents resource leaks
Distinguish between SSH errors and command failures:
SSH errors throw exceptions
Command failures set
ExitCodeto non-zero but don't throw
Handle network errors gracefully:
Connection errors are common in network applications
Implement retry logic for transient failures
Check InnerException:
Some errors wrap underlying exceptions
InnerException may contain valuable diagnostic information
See Also
SshException(SshException.cs:13) - Exception class for SSH errorsSshError(SshError.cs:12) - Enumeration of all error codesSession Lifecycle - Understanding state-related errors
Authentication - Handling authentication-specific errors
Command Execution - Command execution error handling
File Transfer with SCP - File transfer error handling
Algorithm and Method Preferences - Algorithm negotiation errors
Session Timeouts - Timeout errors