When using the SpotRestApi, errors returned from API calls can be downcast to errors::ConnectorError to identify specific failure modes. This allows you to distinguish between client-side issues (like invalid parameters), authentication issues (invalid API keys), rate limiting, and server-side errors.
To handle these errors, use e.downcast_ref::<errors::ConnectorError>() on the error returned by the API call.
Common ConnectorError variants include:
ConnectorClientError: Check request parameters.UnauthorizedError: Invalid API credentials.ForbiddenError: Check API key permissions.TooManyRequestsError: Rate limit exceeded.RateLimitBanError: IP address banned due to excessive rate limits.ServerError: Binance server-side error (includes status_code).NetworkError: Connectivity issues.NotFoundError: The requested resource does not exist.BadRequestError: Verify input parameters.
use binance_sdk::spot;
use binance_sdk::config;
use binance_sdk::errors;
let configuration = config::ConfigurationRestApi::builder()
.api_key("your-api-key")
.api_secret("your-api-secret")
.build()?;
let client = spot::SpotRestApi::production(configuration);
let params = spot::rest_api::ExchangeInfoParams::default();
match client.exchange_info(params).await {
Ok(response) => response,
Err(e) => {
if let Some(conn_err) = e.downcast_ref::<errors::ConnectorError>() {
match conn_err {
errors::ConnectorError::ConnectorClientError { msg, code } => {
eprintln!("Client error: Check your request parameters. {}", msg);
}
errors::ConnectorError::UnauthorizedError { msg, code } => {
eprintln!("Unauthorized: Invalid API credentials. {}", msg);
}
errors::ConnectorError::ForbiddenError { msg, code } => {
eprintln!("Forbidden: Check your API key permissions. {}", msg);
}
errors::ConnectorError::TooManyRequestsError { msg, code } => {
eprintln!("Rate limit exceeded. Please wait and try again. {}", msg);
}
errors::ConnectorError::RateLimitBanError { msg, code } => {
eprintln!("IP address banned due to excessive rate limits. {}", msg);
}
errors::ConnectorError::ServerError { msg, status_code } => {
eprintln!("Server error: {} (status code: {:?})", msg, status_code);
}
errors::ConnectorError::NetworkError { msg, code } => {
eprintln!("Network error: Check your internet connection. {}", msg);
}
errors::ConnectorError::NotFoundError { msg, code } => {
eprintln!("Resource not found. {}", msg);
}
errors::ConnectorError::BadRequestError { msg, code } => {
eprintln!("Bad request: Verify your input parameters. {}", msg);
}
other => {
eprintln!("Unexpected ConnectorError variant: {:?}", other);
}
}
} else {
eprintln!("An unexpected error occurred: {:#}", e);
}
return Err(e);
}
};