Improve error handling
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -8,6 +8,8 @@ use crate::{
|
||||
},
|
||||
};
|
||||
use axum::{extract::Path, Extension, Json};
|
||||
use backoff::{future::retry, ExponentialBackoff};
|
||||
use core::panic;
|
||||
use http::StatusCode;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
@@ -47,27 +49,34 @@ pub async fn add(
|
||||
return Err(StatusCode::CONFLICT);
|
||||
}
|
||||
|
||||
app_config.alpaca_rate_limit.until_ready().await;
|
||||
let asset = app_config
|
||||
.alpaca_client
|
||||
.get(&format!("{}/{}", ALPACA_ASSET_API_URL, request.symbol))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
if e.status() == Some(reqwest::StatusCode::NOT_FOUND) {
|
||||
StatusCode::NOT_FOUND
|
||||
} else {
|
||||
panic!()
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
let asset = retry(ExponentialBackoff::default(), || async {
|
||||
app_config.alpaca_rate_limit.until_ready().await;
|
||||
app_config
|
||||
.alpaca_client
|
||||
.get(&format!("{}/{}", ALPACA_ASSET_API_URL, request.symbol))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()
|
||||
.map_err(|e| match e.status() {
|
||||
Some(reqwest::StatusCode::NOT_FOUND) => backoff::Error::Permanent(e),
|
||||
_ => e.into(),
|
||||
})?
|
||||
.json::<incoming::asset::Asset>()
|
||||
.await
|
||||
.map_err(backoff::Error::Permanent)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| match e.status() {
|
||||
Some(reqwest::StatusCode::NOT_FOUND) => StatusCode::NOT_FOUND,
|
||||
_ => panic!("Unexpected error: {}.", e),
|
||||
})?;
|
||||
|
||||
let asset = asset.json::<incoming::asset::Asset>().await.unwrap();
|
||||
if asset.status != Status::Active || !asset.tradable || !asset.fractionable {
|
||||
return Err(StatusCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
let asset = Asset::from(asset);
|
||||
|
||||
broadcast_bus_sender
|
||||
.send(BroadcastMessage::Asset((
|
||||
state::asset::BroadcastMessage::Add,
|
||||
@@ -85,8 +94,7 @@ pub async fn delete(
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
let asset = database::assets::select_where_symbol(&app_config.clickhouse_client, &symbol)
|
||||
.await
|
||||
.ok_or(StatusCode::NOT_FOUND)
|
||||
.unwrap();
|
||||
.ok_or(StatusCode::NOT_FOUND)?;
|
||||
|
||||
broadcast_bus_sender
|
||||
.send(BroadcastMessage::Asset((
|
||||
|
Reference in New Issue
Block a user