Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SubgraphServiceError to return JSON-formatted error responses #652

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions crates/service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ pub enum IndexerServiceError {
EscrowAccount(#[from] EscrowAccountsError),
}

// Helper struct to properly format
// error messages
#[derive(Serialize)]
struct ErrorResponse {
message: String,
}

impl ErrorResponse {
fn new(message: impl ToString) -> Self {
Self {
message: message.to_string(),
}
}

fn into_response(self, status_code: StatusCode) -> Response {
(status_code, Json(self)).into_response()
}
}

impl StatusCodeExt for IndexerServiceError {
fn status_code(&self) -> StatusCode {
use IndexerServiceError as E;
Expand All @@ -55,19 +74,9 @@ impl StatusCodeExt for IndexerServiceError {

impl IntoResponse for IndexerServiceError {
fn into_response(self) -> Response {
#[derive(Serialize)]
struct ErrorResponse {
message: String,
}

tracing::error!(%self, "An IndexerServiceError occoured.");
(
self.status_code(),
Json(ErrorResponse {
message: self.to_string(),
}),
)
.into_response()
tracing::error!(%self, "An IndexerServiceError occurred.");
let status_code = self.status_code();
ErrorResponse::new(self).into_response(status_code)
}
}

Expand Down Expand Up @@ -100,7 +109,9 @@ impl StatusCodeExt for SubgraphServiceError {
// Tell axum how to convert `SubgraphServiceError` into a response.
impl IntoResponse for SubgraphServiceError {
fn into_response(self) -> Response {
(self.status_code(), self.to_string()).into_response()
tracing::error!(%self, "An SubgraphServiceError occurred.");
let status_code = self.status_code();
ErrorResponse::new(self).into_response(status_code)
}
}

Expand Down
Loading