|
1 | 1 | // Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
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; |
5 | 7 | use tracing::warn; |
6 | 8 |
|
7 | 9 | use indexer_common::SubgraphClient; |
8 | 10 |
|
9 | | -use crate::service::{IndexerServiceError, IndexerServiceImpl}; |
10 | | - |
11 | 11 | #[autometrics::autometrics] |
12 | | -pub async fn static_subgraph_request_handler<I>( |
| 12 | +pub async fn static_subgraph_request_handler( |
13 | 13 | Extension(subgraph_client): Extension<&'static SubgraphClient>, |
14 | 14 | Extension(required_auth_token): Extension<Option<String>>, |
15 | 15 | headers: HeaderMap, |
16 | 16 | body: Bytes, |
17 | | -) -> Result<impl IntoResponse, IndexerServiceError<I::Error>> |
18 | | -where |
19 | | - I: IndexerServiceImpl + Sync + Send + 'static, |
20 | | -{ |
| 17 | +) -> Result<impl IntoResponse, StaticSubgraphError> { |
21 | 18 | if let Some(required_auth_token) = required_auth_token { |
22 | 19 | let authorization = headers |
23 | 20 | .get("authorization") |
24 | 21 | .map(|value| value.to_str()) |
25 | 22 | .transpose() |
26 | | - .map_err(|_| IndexerServiceError::Unauthorized)? |
27 | | - .ok_or_else(|| IndexerServiceError::Unauthorized)? |
| 23 | + .map_err(|_| StaticSubgraphError::Unauthorized)? |
| 24 | + .ok_or_else(|| StaticSubgraphError::Unauthorized)? |
28 | 25 | .trim_start_matches("Bearer "); |
29 | 26 |
|
30 | 27 | if authorization != required_auth_token { |
31 | | - return Err(IndexerServiceError::Unauthorized); |
| 28 | + return Err(StaticSubgraphError::Unauthorized); |
32 | 29 | } |
33 | 30 | } |
34 | 31 |
|
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?; |
39 | 33 |
|
40 | 34 | Ok(( |
41 | 35 | response.status(), |
42 | 36 | 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 | + })?, |
51 | 40 | )) |
52 | 41 | } |
| 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 | +} |
0 commit comments