Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling for bulk endpoints & error checking in tests #43

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::info;
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

use crate::models::bulk::BulkResponse;
use crate::models::bulk::{BulkResponse, ListResponse};
use crate::models::error::ErrorResponse;
use crate::models::profile::ENSProfile;
use crate::routes;
Expand All @@ -19,7 +19,7 @@ use crate::state::AppState;
#[derive(OpenApi)]
#[openapi(
paths(routes::address::get, routes::name::get, routes::universal::get),
components(schemas(ENSProfile, BulkResponse<ENSProfile>, ErrorResponse))
components(schemas(ENSProfile, ListResponse<BulkResponse<ENSProfile>>, ErrorResponse))
)]
pub struct ApiDoc;

Expand Down
51 changes: 47 additions & 4 deletions server/src/models/bulk.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
use enstate_shared::models::profile::error::ProfileError;
use enstate_shared::models::profile::Profile;
use utoipa::ToSchema;

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct BulkResponse<T> {
use crate::models::error::ErrorResponse;
use crate::routes::profile_http_error_mapper;

#[derive(serde::Serialize)]
#[serde(tag = "type")]
pub enum BulkResponse<Ok> {
#[serde(rename = "success")]
Ok(Ok),
#[serde(rename = "error")]
Err(ErrorResponse),
}

impl<T> From<BulkResponse<T>> for Result<T, ErrorResponse> {
fn from(value: BulkResponse<T>) -> Self {
match value {
BulkResponse::Ok(value) => Ok(value),
BulkResponse::Err(err) => Err(err),
}
}
}

impl<T> From<Result<T, ErrorResponse>> for BulkResponse<T> {
fn from(value: Result<T, ErrorResponse>) -> Self {
match value {
Ok(value) => Self::Ok(value),
Err(err) => Self::Err(err),
}
}
}

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, ToSchema)]
pub struct ListResponse<T> {
pub(crate) response_length: usize,
pub(crate) response: Vec<T>,
}

impl<T> From<Vec<T>> for BulkResponse<T> {
impl<T> From<Vec<T>> for ListResponse<T> {
fn from(value: Vec<T>) -> Self {
BulkResponse {
Self {
response_length: value.len(),
response: value,
}
}
}

impl From<Vec<Result<Profile, ProfileError>>> for ListResponse<BulkResponse<Profile>> {
fn from(value: Vec<Result<Profile, ProfileError>>) -> Self {
value
.into_iter()
.map(|it| it.map_err(profile_http_error_mapper))
.map(BulkResponse::from)
.collect::<Vec<_>>()
.into()
}
}
23 changes: 11 additions & 12 deletions server/src/routes/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ use axum::{
};
use enstate_shared::models::profile::Profile;
use ethers_core::types::Address;
use futures::future::try_join_all;
use futures::future::join_all;
use serde::Deserialize;

use crate::models::bulk::BulkResponse;
use crate::routes::{
http_simple_status_error, profile_http_error_mapper, validate_bulk_input, FreshQuery, Qs,
RouteError,
};
use crate::models::bulk::{BulkResponse, ListResponse};
use crate::routes::{http_simple_status_error, validate_bulk_input, FreshQuery, Qs, RouteError};

#[utoipa::path(
get,
Expand Down Expand Up @@ -42,7 +39,11 @@ pub async fn get(
State(state),
)
.await
.map(|res| Json(res.0.response.get(0).expect("index 0 should exist").clone()))
.map(|mut res| {
Result::<_, _>::from(res.0.response.remove(0))
.map(Json)
.map_err(RouteError::from)
})?
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -71,7 +72,7 @@ pub struct AddressGetBulkQuery {
pub async fn get_bulk(
Qs(query): Qs<AddressGetBulkQuery>,
State(state): State<Arc<crate::AppState>>,
) -> Result<Json<BulkResponse<Profile>>, RouteError> {
) -> Result<Json<ListResponse<BulkResponse<Profile>>>, RouteError> {
let addresses = validate_bulk_input(&query.addresses, 10)?;

let addresses = addresses
Expand All @@ -89,9 +90,7 @@ pub async fn get_bulk(
})
.collect::<Vec<_>>();

let joined = try_join_all(profiles)
.await
.map_err(profile_http_error_mapper)?;
let joined = join_all(profiles).await.into();

Ok(Json(joined.into()))
Ok(Json(joined))
}
22 changes: 14 additions & 8 deletions server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ where

pub type RouteError = (StatusCode, Json<ErrorResponse>);

pub fn profile_http_error_mapper<T: AsRef<ProfileError>>(err: T) -> RouteError {
impl From<ErrorResponse> for RouteError {
fn from(value: ErrorResponse) -> Self {
(
StatusCode::from_u16(value.status).expect("status should be valid"),
Json(value),
)
}
}

pub fn profile_http_error_mapper<T: AsRef<ProfileError>>(err: T) -> ErrorResponse {
let err = err.as_ref();
let status = match err {
ProfileError::NotFound => StatusCode::NOT_FOUND,
Expand All @@ -46,13 +55,10 @@ pub fn profile_http_error_mapper<T: AsRef<ProfileError>>(err: T) -> RouteError {
_ => StatusCode::INTERNAL_SERVER_ERROR,
};

(
status,
Json(ErrorResponse {
status: status.as_u16(),
error: err.to_string(),
}),
)
ErrorResponse {
status: status.as_u16(),
error: err.to_string(),
}
}

pub fn http_simple_status_error(status: StatusCode) -> RouteError {
Expand Down
22 changes: 12 additions & 10 deletions server/src/routes/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use axum::{
Json,
};
use enstate_shared::models::profile::Profile;
use futures::future::try_join_all;
use futures::future::join_all;
use serde::Deserialize;

use crate::models::bulk::BulkResponse;
use crate::routes::{profile_http_error_mapper, validate_bulk_input, FreshQuery, Qs, RouteError};
use crate::models::bulk::{BulkResponse, ListResponse};
use crate::routes::{validate_bulk_input, FreshQuery, Qs, RouteError};

#[utoipa::path(
get,
Expand All @@ -35,7 +35,11 @@ pub async fn get(
State(state),
)
.await
.map(|res| Json(res.0.response.get(0).expect("index 0 should exist").clone()))
.map(|mut res| {
Result::<_, _>::from(res.0.response.remove(0))
.map(Json)
.map_err(RouteError::from)
})?
}

#[derive(Deserialize)]
Expand All @@ -52,7 +56,7 @@ pub struct NameGetBulkQuery {
get,
path = "/bulk/n/",
responses(
(status = 200, description = "Successfully found name.", body = BulkResponse<ENSProfile>),
(status = 200, description = "Successfully found name.", body = ListButWithLength<BulkResponse<Profile>>),
(status = NOT_FOUND, description = "No name could be found.", body = ErrorResponse),
),
params(
Expand All @@ -62,17 +66,15 @@ pub struct NameGetBulkQuery {
pub async fn get_bulk(
Qs(query): Qs<NameGetBulkQuery>,
State(state): State<Arc<crate::AppState>>,
) -> Result<Json<BulkResponse<Profile>>, RouteError> {
) -> Result<Json<ListResponse<BulkResponse<Profile>>>, RouteError> {
let names = validate_bulk_input(&query.names, 10)?;

let profiles = names
.iter()
.map(|name| state.service.resolve_from_name(name, query.fresh.fresh))
.collect::<Vec<_>>();

let joined = try_join_all(profiles)
.await
.map_err(profile_http_error_mapper)?;
let joined = join_all(profiles).await.into();

Ok(Json(joined.into()))
Ok(Json(joined))
}
20 changes: 11 additions & 9 deletions server/src/routes/universal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use axum::{
Json,
};
use enstate_shared::models::profile::Profile;
use futures::future::try_join_all;
use futures::future::join_all;
use serde::Deserialize;

use crate::models::bulk::BulkResponse;
use crate::routes::{profile_http_error_mapper, validate_bulk_input, FreshQuery, Qs, RouteError};
use crate::models::bulk::{BulkResponse, ListResponse};
use crate::routes::{validate_bulk_input, FreshQuery, Qs, RouteError};

#[utoipa::path(
get,
Expand All @@ -36,7 +36,11 @@ pub async fn get(
State(state),
)
.await
.map(|res| Json(res.0.response.get(0).expect("index 0 should exist").clone()))
.map(|mut res| {
Result::<_, _>::from(res.0.response.remove(0))
.map(Json)
.map_err(RouteError::from)
})?
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -64,7 +68,7 @@ pub struct UniversalGetBulkQuery {
pub async fn get_bulk(
Qs(query): Qs<UniversalGetBulkQuery>,
State(state): State<Arc<crate::AppState>>,
) -> Result<Json<BulkResponse<Profile>>, RouteError> {
) -> Result<Json<ListResponse<BulkResponse<Profile>>>, RouteError> {
let queries = validate_bulk_input(&query.queries, 10)?;

let profiles = queries
Expand All @@ -76,9 +80,7 @@ pub async fn get_bulk(
})
.collect::<Vec<_>>();

let joined = try_join_all(profiles)
.await
.map_err(profile_http_error_mapper)?;
let joined = join_all(profiles).await.into();

Ok(Json(joined.into()))
Ok(Json(joined))
}
2 changes: 1 addition & 1 deletion shared/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ serde_json = "1.0.96"
crc32fast = "1.3.2"
hex = "0.4.3"
reqwest = "0.11.22"
ethers-ccip-read = { git = "https://github.com/ensdomains/ethers-ccip-read" }
ethers-ccip-read = { git = "https://github.com/v3xlabs/rust-ethers-ccip-read/", branch = "patch/provider-error-handling" }
#ethers-ccip-read = { git = "https://github.com/ensdomains/ethers-ccip-read" }
build-info = "0.0.34"

# needed to enable the "js" feature for compatibility with wasm,
Expand Down
2 changes: 1 addition & 1 deletion shared/src/models/universal_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn resolve_universal(
})?;

// Abi Decode
let result = ethers_core::abi::decode(
let result = abi::decode(
&[
ParamType::Array(Box::new(ParamType::Bytes)),
ParamType::Address,
Expand Down
Loading