Skip to content

Commit bef1fa9

Browse files
committed
refactor: use custom error for static subgraph
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent dc3b3d6 commit bef1fa9

File tree

2 files changed

+46
-29
lines changed

2 files changed

+46
-29
lines changed
Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,73 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use axum::{body::Bytes, http::HeaderMap, response::IntoResponse, Extension};
4+
use axum::{body::Bytes, http::HeaderMap, response::IntoResponse, Extension, Json};
5+
use reqwest::StatusCode;
6+
use serde_json::json;
57
use tracing::warn;
68

79
use indexer_common::SubgraphClient;
810

9-
use crate::service::{IndexerServiceError, IndexerServiceImpl};
10-
1111
#[autometrics::autometrics]
12-
pub async fn static_subgraph_request_handler<I>(
12+
pub async fn static_subgraph_request_handler(
1313
Extension(subgraph_client): Extension<&'static SubgraphClient>,
1414
Extension(required_auth_token): Extension<Option<String>>,
1515
headers: HeaderMap,
1616
body: Bytes,
17-
) -> Result<impl IntoResponse, IndexerServiceError<I::Error>>
18-
where
19-
I: IndexerServiceImpl + Sync + Send + 'static,
20-
{
17+
) -> Result<impl IntoResponse, StaticSubgraphError> {
2118
if let Some(required_auth_token) = required_auth_token {
2219
let authorization = headers
2320
.get("authorization")
2421
.map(|value| value.to_str())
2522
.transpose()
26-
.map_err(|_| IndexerServiceError::Unauthorized)?
27-
.ok_or_else(|| IndexerServiceError::Unauthorized)?
23+
.map_err(|_| StaticSubgraphError::Unauthorized)?
24+
.ok_or_else(|| StaticSubgraphError::Unauthorized)?
2825
.trim_start_matches("Bearer ");
2926

3027
if authorization != required_auth_token {
31-
return Err(IndexerServiceError::Unauthorized);
28+
return Err(StaticSubgraphError::Unauthorized);
3229
}
3330
}
3431

35-
let response = subgraph_client
36-
.query_raw(body)
37-
.await
38-
.map_err(IndexerServiceError::FailedToQueryStaticSubgraph)?;
32+
let response = subgraph_client.query_raw(body).await?;
3933

4034
Ok((
4135
response.status(),
4236
response.headers().to_owned(),
43-
response
44-
.text()
45-
.await
46-
.map_err(|e| {
47-
warn!("Failed to read response body: {}", e);
48-
e
49-
})
50-
.map_err(|e| IndexerServiceError::FailedToQueryStaticSubgraph(e.into()))?,
37+
response.text().await.inspect_err(|e| {
38+
warn!("Failed to read response body: {}", e);
39+
})?,
5140
))
5241
}
42+
43+
#[derive(Debug, thiserror::Error)]
44+
pub enum StaticSubgraphError {
45+
#[error("No valid receipt or free query auth token provided")]
46+
Unauthorized,
47+
48+
#[error("Failed to query subgraph: {0}")]
49+
FailedToQuery(#[from] anyhow::Error),
50+
51+
#[error("Failed to parse subgraph response: {0}")]
52+
FailedToParse(#[from] reqwest::Error),
53+
}
54+
55+
impl IntoResponse for StaticSubgraphError {
56+
fn into_response(self) -> axum::response::Response {
57+
let status = match self {
58+
StaticSubgraphError::Unauthorized => StatusCode::UNAUTHORIZED,
59+
StaticSubgraphError::FailedToQuery(_) | StaticSubgraphError::FailedToParse(_) => {
60+
StatusCode::INTERNAL_SERVER_ERROR
61+
}
62+
};
63+
64+
tracing::error!(%self, "StaticSubgraphError occoured.");
65+
(
66+
status,
67+
Json(json! {{
68+
"message": self.to_string(),
69+
}}),
70+
)
71+
.into_response()
72+
}
73+
}

crates/service/src/service/indexer_service.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ where
9191
InvalidFreeQueryAuthToken,
9292
#[error("Failed to sign attestation")]
9393
FailedToSignAttestation,
94-
#[error("Failed to query subgraph: {0}")]
95-
FailedToQueryStaticSubgraph(anyhow::Error),
9694

9795
#[error("Could not decode signer: {0}")]
9896
CouldNotDecodeSigner(tap_core::Error),
@@ -124,8 +122,6 @@ where
124122
| CouldNotDecodeSigner(_)
125123
| EscrowAccount(_)
126124
| ProcessingError(_) => StatusCode::BAD_REQUEST,
127-
128-
FailedToQueryStaticSubgraph(_) => StatusCode::INTERNAL_SERVER_ERROR,
129125
};
130126
tracing::error!(%self, "An IndexerServiceError occoured.");
131127
(
@@ -409,7 +405,7 @@ impl IndexerService {
409405

410406
misc_routes = misc_routes.route(
411407
"/network",
412-
post(static_subgraph_request_handler::<I>)
408+
post(static_subgraph_request_handler)
413409
.route_layer(Extension(network_subgraph))
414410
.route_layer(Extension(options.config.service.serve_auth_token.clone()))
415411
.route_layer(static_subgraph_rate_limiter.clone()),
@@ -420,7 +416,7 @@ impl IndexerService {
420416
info!("Serving escrow subgraph at /escrow");
421417

422418
misc_routes = misc_routes
423-
.route("/escrow", post(static_subgraph_request_handler::<I>))
419+
.route("/escrow", post(static_subgraph_request_handler))
424420
.route_layer(Extension(escrow_subgraph))
425421
.route_layer(Extension(options.config.service.serve_auth_token.clone()))
426422
.route_layer(static_subgraph_rate_limiter);

0 commit comments

Comments
 (0)