#Error Handling
The SDK provides typed error classes for better error handling.
#Error Types
import {
WalbucketError,
ValidationError,
NetworkError,
EncryptionError,
BlockchainError,
ConfigurationError
} from '@walbucket/sdk';typescript
#Error Handling Example
try {
await walbucket.upload(file);
} catch (error) {
if (error instanceof ValidationError) {
console.error('Validation error:', error.message);
} else if (error instanceof NetworkError) {
console.error('Network error:', error.message);
} else if (error instanceof EncryptionError) {
console.error('Encryption error:', error.message);
} else if (error instanceof BlockchainError) {
console.error('Blockchain error:', error.message);
} else if (error instanceof ConfigurationError) {
console.error('Configuration error:', error.message);
} else if (error instanceof WalbucketError) {
console.error('Error code:', error.code);
}
}typescript
#Error Classes
#ValidationError
Thrown when input validation fails.
try {
await walbucket.upload(null);
} catch (error) {
if (error instanceof ValidationError) {
// Handle validation error
}
}typescript
#NetworkError
Thrown when network requests fail.
try {
await walbucket.upload(file);
} catch (error) {
if (error instanceof NetworkError) {
// Handle network error (retry, etc.)
}
}typescript
#EncryptionError
Thrown when encryption/decryption fails.
try {
await walbucket.retrieve(assetId, { sessionKey });
} catch (error) {
if (error instanceof EncryptionError) {
// Handle encryption error
}
}typescript
#BlockchainError
Thrown when blockchain operations fail.
try {
await walbucket.upload(file);
} catch (error) {
if (error instanceof BlockchainError) {
// Handle blockchain error
}
}typescript
#ConfigurationError
Thrown when SDK configuration is invalid.
try {
const walbucket = new Walbucket({
apiKey: '', // Invalid
});
} catch (error) {
if (error instanceof ConfigurationError) {
// Handle configuration error
}
}typescript
#Best Practices
- Always wrap SDK calls in try-catch blocks
- Check error types for specific handling
- Provide user-friendly error messages
- Log errors for debugging
- Retry network errors when appropriate
#Related
- Configuration - Avoid configuration errors
- Encryption Policies - Handle encryption errors