Skip to main content

map_err

Function map_err 

Source
pub fn map_err(err: BcsApiError) -> Error
Expand description

Maps a failed read (or a failed step before a command was sent) to a public error.

Protocol errorPublic error
RateLimitExceeded, or HttpError with status 429[Error::RateLimited] with retry_after of 1 second (the broker does not say how long: the value is a fixed guess)
AuthError (the token is refused)[Error::Config] with the broker’s message: fix the token, a repeat will not help
SubscriptionError[Error::Transport] with the broker’s message
HttpError (any other status or no status), WsError, ParseError[Error::Transport] with the message
NotFound (HTTP 404, type = "NOT_FOUND")[Error::Rejected] with code not_found (an unknown instrument or order, as at the other brokers)
ValidationError (HTTP 400, type = "VALIDATION_ERROR")[Error::Rejected] with code validation and the field errors as the message

All [Error::Transport] results are retryable for a read. Do not use this function for a command: it would turn a lost answer into a plain transport error. Use map_command_err.

§Examples

use brokapi_bcs_protocol::error::BcsApiError;
use brokapi_bcs::error::map_err;
use brokapi_core::Error;

assert!(matches!(map_err(BcsApiError::RateLimitExceeded), Error::RateLimited { .. }));
assert!(map_err(BcsApiError::NotFound { trace_id: None }).is_not_found());
assert!(matches!(map_err(BcsApiError::AuthError { message: "bad token".into() }), Error::Config(_)));
let validation = BcsApiError::ValidationError { trace_id: None, errors: vec![] };
assert!(matches!(map_err(validation), Error::Rejected { .. }));