Skip to content

Commit 01a4932

Browse files
committed
fix: allow responses for free queries with no allocation open
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 32f30db commit 01a4932

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

crates/service/src/middleware/attestation.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@ pub async fn attestation_middleware(
4545
request: Request,
4646
next: Next,
4747
) -> Result<Response, AttestationError> {
48-
let signer = request
49-
.extensions()
50-
.get::<AttestationSigner>()
51-
.cloned()
52-
.ok_or(AttestationError::CouldNotFindSigner)?;
48+
let signer = request.extensions().get::<AttestationSigner>().cloned();
5349

54-
let (parts, graphql_response) = next.run(request).await.into_parts();
50+
let response = next.run(request).await;
51+
52+
// the only scenario where we don't have a signer is if it's a free query
53+
let (parts, graphql_response) = response.into_parts();
5554
let attestation_response = parts.extensions.get::<AttestationInput>();
5655
let bytes = to_bytes(graphql_response, usize::MAX).await?;
5756
let res = String::from_utf8(bytes.into())?;
5857

59-
let attestation = match attestation_response {
60-
Some(AttestationInput::Attestable { req }) => Some(signer.create_attestation(req, &res)),
58+
let attestation = match (signer, attestation_response) {
59+
(Some(signer), Some(AttestationInput::Attestable { req })) => {
60+
Some(signer.create_attestation(req, &res))
61+
}
6162
_ => None,
6263
};
6364

@@ -73,26 +74,22 @@ pub async fn attestation_middleware(
7374

7475
#[derive(thiserror::Error, Debug)]
7576
pub enum AttestationError {
76-
#[error("Could not find signer for allocation")]
77-
CouldNotFindSigner,
78-
7977
#[error("There was an AxumError: {0}")]
80-
AxumError(#[from] axum::Error),
78+
Axum(#[from] axum::Error),
8179

8280
#[error("There was an error converting the response to UTF-8 string: {0}")]
83-
FromUtf8Error(#[from] FromUtf8Error),
81+
FromUtf8(#[from] FromUtf8Error),
8482

8583
#[error("there was an error while serializing the response: {0}")]
86-
SerializationError(#[from] serde_json::Error),
84+
Serialization(#[from] serde_json::Error),
8785
}
8886

8987
impl StatusCodeExt for AttestationError {
9088
fn status_code(&self) -> StatusCode {
9189
match self {
92-
AttestationError::CouldNotFindSigner => StatusCode::INTERNAL_SERVER_ERROR,
93-
AttestationError::AxumError(_)
94-
| AttestationError::FromUtf8Error(_)
95-
| AttestationError::SerializationError(_) => StatusCode::BAD_GATEWAY,
90+
AttestationError::Axum(_)
91+
| AttestationError::FromUtf8(_)
92+
| AttestationError::Serialization(_) => StatusCode::BAD_GATEWAY,
9693
}
9794
}
9895
}
@@ -211,6 +208,6 @@ mod tests {
211208
let app = Router::new().route("/", get(handle)).layer(middleware);
212209

213210
let res = send_request(app, None).await;
214-
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
211+
assert_eq!(res.status(), StatusCode::OK);
215212
}
216213
}

0 commit comments

Comments
 (0)