Skip to content

Commit 40c696c

Browse files
Enhance middleware error reporting and logging without breaking changes (#660)
* fix(middleware): improve error reporting and logging - Add detailed error logging in attestation middleware - Enhance signer middleware with better error reporting - Improve error responses with structured JSON format - Maintain backward compatibility with existing behavior - Add tests for error handling scenarios This change addresses debugging challenges by adding proper tracing while preserving the original middleware behavior. * fix(service): Update attestation middleware implementation Co-authored-by: consoli <[email protected]> * fix(service): Update attestation signer middleware Co-authored-by: consoli <[email protected]> * fix(service): Use display impl for logging values * fix(service): Use allocation_id as variable in warning log --------- Co-authored-by: consoli <[email protected]>
1 parent 0103269 commit 40c696c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

crates/service/src/error.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ pub enum IndexerServiceError {
4040
// Helper struct to properly format
4141
// error messages
4242
#[derive(Serialize)]
43-
struct ErrorResponse {
44-
message: String,
43+
#[cfg_attr(test, derive(serde::Deserialize))]
44+
pub struct ErrorResponse {
45+
pub(crate) message: String,
4546
}
4647

4748
impl ErrorResponse {
48-
fn new(message: impl ToString) -> Self {
49+
pub fn new(message: impl ToString) -> Self {
4950
Self {
5051
message: message.to_string(),
5152
}
5253
}
5354

54-
fn into_response(self, status_code: StatusCode) -> Response {
55+
pub fn into_response(self, status_code: StatusCode) -> Response {
5556
(status_code, Json(self)).into_response()
5657
}
5758
}

crates/service/src/middleware/attestation.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use reqwest::StatusCode;
1414
use serde::Serialize;
1515
use thegraph_core::attestation::Attestation;
1616

17-
use crate::error::StatusCodeExt;
17+
use crate::error::{ErrorResponse, StatusCodeExt};
1818

1919
#[derive(Clone)]
2020
pub enum AttestationInput {
@@ -56,6 +56,11 @@ pub async fn attestation_middleware(
5656
(Some(signer), Some(AttestationInput::Attestable { req })) => {
5757
Some(signer.create_attestation(req, &res))
5858
}
59+
(None, Some(AttestationInput::Attestable { .. })) => {
60+
// keep this branch separated just to log the missing signer condition
61+
tracing::warn!("Attestation requested but no signer found");
62+
None
63+
}
5964
_ => None,
6065
};
6166

@@ -93,12 +98,14 @@ impl StatusCodeExt for AttestationError {
9398

9499
impl IntoResponse for AttestationError {
95100
fn into_response(self) -> Response {
96-
self.status_code().into_response()
101+
tracing::error!(error=%self, "Attestation error");
102+
let status_code = self.status_code();
103+
ErrorResponse::new(self).into_response(status_code)
97104
}
98105
}
99106

100107
#[cfg(test)]
101-
mod tests {
108+
mod attestation_tests {
102109
use axum::{
103110
body::{to_bytes, Body},
104111
http::{Request, Response},

crates/service/src/middleware/attestation_signer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub async fn signer_middleware(
3030
if let Some(Allocation(allocation_id)) = request.extensions().get::<Allocation>() {
3131
if let Some(signer) = state.attestation_signers.borrow().get(allocation_id) {
3232
request.extensions_mut().insert(signer.clone());
33+
} else {
34+
// Just log this case which is silently passed through next middleware
35+
tracing::warn!(%allocation_id, "No attestation signer found for allocation",);
3336
}
3437
}
3538

0 commit comments

Comments
 (0)