Skip to content

Commit 916f9c8

Browse files
committed
refactor: move indexer error to error.rs
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 6202b0c commit 916f9c8

File tree

4 files changed

+66
-68
lines changed

4 files changed

+66
-68
lines changed

crates/service/src/error.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use alloy::primitives::Address;
45
use anyhow::Error;
5-
use axum::response::{IntoResponse, Response};
6+
use axum::{response::{IntoResponse, Response}, Json};
7+
use indexer_monitor::EscrowAccountsError;
68
use reqwest::StatusCode;
9+
use serde::Serialize;
710
use thegraph_core::DeploymentId;
811
use thiserror::Error;
912

13+
#[derive(Debug, Error)]
14+
pub enum IndexerServiceError {
15+
#[error("Issues with provided receipt: {0}")]
16+
ReceiptError(tap_core::Error),
17+
#[error("No attestation signer found for allocation `{0}`")]
18+
NoSignerForAllocation(Address),
19+
#[error("Invalid request body: {0}")]
20+
InvalidRequest(anyhow::Error),
21+
#[error("Error while processing the request: {0}")]
22+
ProcessingError(SubgraphServiceError),
23+
#[error("No valid receipt or free query auth token provided")]
24+
Unauthorized,
25+
#[error("Invalid free query auth token")]
26+
InvalidFreeQueryAuthToken,
27+
#[error("Failed to sign attestation")]
28+
FailedToSignAttestation,
29+
30+
#[error("Could not decode signer: {0}")]
31+
CouldNotDecodeSigner(tap_core::Error),
32+
33+
#[error("There was an error while accessing escrow account: {0}")]
34+
EscrowAccount(EscrowAccountsError),
35+
}
36+
37+
impl IntoResponse for IndexerServiceError {
38+
fn into_response(self) -> Response {
39+
use IndexerServiceError::*;
40+
41+
#[derive(Serialize)]
42+
struct ErrorResponse {
43+
message: String,
44+
}
45+
46+
let status = match self {
47+
Unauthorized => StatusCode::UNAUTHORIZED,
48+
49+
NoSignerForAllocation(_) | FailedToSignAttestation => StatusCode::INTERNAL_SERVER_ERROR,
50+
51+
ReceiptError(_)
52+
| InvalidRequest(_)
53+
| InvalidFreeQueryAuthToken
54+
| CouldNotDecodeSigner(_)
55+
| EscrowAccount(_)
56+
| ProcessingError(_) => StatusCode::BAD_REQUEST,
57+
};
58+
tracing::error!(%self, "An IndexerServiceError occoured.");
59+
(
60+
status,
61+
Json(ErrorResponse {
62+
message: self.to_string(),
63+
}),
64+
)
65+
.into_response()
66+
}
67+
}
68+
1069
#[derive(Debug, Error)]
1170
pub enum SubgraphServiceError {
1271
#[error("Invalid status query: {0}")]

crates/service/src/routes/request_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use std::sync::Arc;
55

6-
use crate::tap::AgoraQuery;
6+
use crate::{error::IndexerServiceError, tap::AgoraQuery};
77
use axum::{
88
extract::{Path, State},
99
http::HeaderMap,
@@ -19,7 +19,7 @@ use thegraph_core::DeploymentId;
1919
use tracing::trace;
2020

2121
use crate::service::{
22-
AttestationOutput, IndexerServiceError, IndexerServiceResponse, IndexerServiceState, TapReceipt,
22+
AttestationOutput, IndexerServiceResponse, IndexerServiceState, TapReceipt,
2323
};
2424

2525
lazy_static! {

crates/service/src/service.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ use tracing::error;
3535
mod indexer_service;
3636
mod tap_receipt_header;
3737

38-
pub use indexer_service::{
39-
AttestationOutput, IndexerServiceError, IndexerServiceResponse, IndexerServiceState,
40-
};
38+
pub use indexer_service::{AttestationOutput, IndexerServiceResponse, IndexerServiceState};
4139
pub use tap_receipt_header::TapReceipt;
4240

4341
#[derive(Debug)]

crates/service/src/service/indexer_service.rs

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use axum::extract::MatchedPath;
77
use axum::extract::Request as ExtractRequest;
88
use axum::http::{Method, Request};
99
use axum::{
10-
response::{IntoResponse, Response},
10+
response::IntoResponse,
1111
routing::{get, post},
1212
Json, Router,
1313
};
@@ -16,19 +16,17 @@ use build_info::BuildInfo;
1616
use indexer_attestation::AttestationSigner;
1717
use indexer_monitor::{
1818
attestation_signers, dispute_manager, escrow_accounts, indexer_allocations, DeploymentDetails,
19-
EscrowAccounts, EscrowAccountsError, SubgraphClient,
19+
EscrowAccounts, SubgraphClient,
2020
};
2121
use prometheus::TextEncoder;
2222
use reqwest::StatusCode;
2323
use serde::Serialize;
2424
use sqlx::postgres::PgPoolOptions;
2525
use std::{
26-
collections::HashMap, error::Error, fmt::Debug, net::SocketAddr, path::PathBuf, sync::Arc,
27-
time::Duration,
26+
collections::HashMap, error::Error, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration,
2827
};
2928
use tap_core::{manager::Manager, receipt::checks::CheckList, tap_eip712_domain};
3029
use thegraph_core::{Address, Attestation};
31-
use thiserror::Error;
3230
use tokio::net::TcpListener;
3331
use tokio::signal;
3432
use tokio::sync::watch::Receiver;
@@ -38,7 +36,6 @@ use tower_http::{cors, cors::CorsLayer, normalize_path::NormalizePath, trace::Tr
3836
use tracing::warn;
3937
use tracing::{error, info, info_span};
4038

41-
use crate::error::SubgraphServiceError;
4239
use crate::routes::health;
4340
use crate::routes::request_handler;
4441
use crate::routes::static_subgraph_request_handler;
@@ -62,62 +59,6 @@ pub enum AttestationOutput {
6259
Attestable,
6360
}
6461

65-
#[derive(Debug, Error)]
66-
pub enum IndexerServiceError {
67-
#[error("Issues with provided receipt: {0}")]
68-
ReceiptError(tap_core::Error),
69-
#[error("No attestation signer found for allocation `{0}`")]
70-
NoSignerForAllocation(Address),
71-
#[error("Invalid request body: {0}")]
72-
InvalidRequest(anyhow::Error),
73-
#[error("Error while processing the request: {0}")]
74-
ProcessingError(SubgraphServiceError),
75-
#[error("No valid receipt or free query auth token provided")]
76-
Unauthorized,
77-
#[error("Invalid free query auth token")]
78-
InvalidFreeQueryAuthToken,
79-
#[error("Failed to sign attestation")]
80-
FailedToSignAttestation,
81-
82-
#[error("Could not decode signer: {0}")]
83-
CouldNotDecodeSigner(tap_core::Error),
84-
85-
#[error("There was an error while accessing escrow account: {0}")]
86-
EscrowAccount(EscrowAccountsError),
87-
}
88-
89-
impl IntoResponse for IndexerServiceError {
90-
fn into_response(self) -> Response {
91-
use IndexerServiceError::*;
92-
93-
#[derive(Serialize)]
94-
struct ErrorResponse {
95-
message: String,
96-
}
97-
98-
let status = match self {
99-
Unauthorized => StatusCode::UNAUTHORIZED,
100-
101-
NoSignerForAllocation(_) | FailedToSignAttestation => StatusCode::INTERNAL_SERVER_ERROR,
102-
103-
ReceiptError(_)
104-
| InvalidRequest(_)
105-
| InvalidFreeQueryAuthToken
106-
| CouldNotDecodeSigner(_)
107-
| EscrowAccount(_)
108-
| ProcessingError(_) => StatusCode::BAD_REQUEST,
109-
};
110-
tracing::error!(%self, "An IndexerServiceError occoured.");
111-
(
112-
status,
113-
Json(ErrorResponse {
114-
message: self.to_string(),
115-
}),
116-
)
117-
.into_response()
118-
}
119-
}
120-
12162
#[derive(Clone, Serialize)]
12263
pub struct IndexerServiceRelease {
12364
version: String,

0 commit comments

Comments
 (0)