Skip to main content

map_status

Function map_status 

Source
pub fn map_status(status: &Status) -> Error
Expand description

Status of a read-only call (snapshots, reference data).

  • ResourceExhausted gives [Error::RateLimited], with retry_after taken from the x-ratelimit-reset header (seconds), else 1 s (60 s for the codes 80003 and 80006, which the broker counts per minute). Except the codes that are not a limit of time: 80001, 80004, 80005 and 80007 give [Error::Rejected] with the code streams_limit, no_subscriptions, stream_lifetime or stop_orders_limit, because a repeat cannot help.
  • InvalidArgument with the code 30261 (too many stop orders in the answer) gives [Error::Rejected] with the code stop_orders_response_limit.
  • NotFound gives [Error::Rejected] with the code not_found ([brokapi_core::NOT_FOUND], the same at every broker).
  • InvalidArgument, PermissionDenied, FailedPrecondition, Unimplemented, AlreadyExists, OutOfRange gives [Error::Rejected], with the status code name as code (for example "InvalidArgument") and the status message as message.
  • Unauthenticated gives [Error::Config]: the token is wrong or expired, fix it.
  • everything else (Unavailable, DeadlineExceeded, Internal, …) gives [Error::Transport].

§Examples

use brokapi_core::Error;
use brokapi_tbank::error::map_status;
use tonic::Status;

match map_status(&Status::not_found("50002")) {
    Error::Rejected { code, message } => {
        assert_eq!(code, "not_found");
        assert_eq!(message, "50002");
    }
    other => panic!("unexpected {other:?}"),
}
assert!(matches!(map_status(&Status::resource_exhausted("80002")), Error::RateLimited { .. }));
// 80007 is a business refusal, not a limit of time.
match map_status(&Status::resource_exhausted("80007")) {
    Error::Rejected { code, .. } => assert_eq!(code, "stop_orders_limit"),
    other => panic!("unexpected {other:?}"),
}
assert!(matches!(map_status(&Status::unauthenticated("40003")), Error::Config(_)));