-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy patherror.rs
145 lines (126 loc) · 4.16 KB
/
error.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0
use std::convert::Infallible;
use anyhow::Error;
use axum::{
response::{IntoResponse, Response},
Json,
};
use indexer_monitor::EscrowAccountsError;
use reqwest::StatusCode;
use serde::Serialize;
use tap_core::{receipt::ReceiptError, Error as TapError};
use thegraph_core::DeploymentId;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum IndexerServiceError {
#[error("No Tap receipt was found in the request")]
ReceiptNotFound,
#[error("Could not find deployment id")]
DeploymentIdNotFound,
#[error(transparent)]
AxumError(#[from] axum::Error),
#[error(transparent)]
SerializationError(#[from] serde_json::Error),
#[error("Issues with provided receipt: {0}")]
TapCoreError(#[from] tap_core::Error),
#[error("Issues with provided receipt: {0}")]
Eip712Error(#[from] tap_core::signed_message::Eip712Error),
#[error("There was an error while accessing escrow account: {0}")]
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;
match &self {
E::TapCoreError(ref error) => match error {
TapError::ReceiptError(ReceiptError::CheckFailure(_)) => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
},
E::EscrowAccount(_) | E::ReceiptNotFound => StatusCode::PAYMENT_REQUIRED,
E::DeploymentIdNotFound => StatusCode::INTERNAL_SERVER_ERROR,
E::AxumError(_) | E::SerializationError(_) => StatusCode::BAD_GATEWAY,
E::Eip712Error(_) => StatusCode::BAD_REQUEST,
}
}
}
impl IntoResponse for IndexerServiceError {
fn into_response(self) -> Response {
tracing::error!(%self, "An IndexerServiceError occurred.");
let status_code = self.status_code();
ErrorResponse::new(self).into_response(status_code)
}
}
#[derive(Debug, Error)]
pub enum SubgraphServiceError {
#[error("Invalid status query: {0}")]
InvalidStatusQuery(Error),
#[error("Unsupported status query fields: {0:?}")]
UnsupportedStatusQueryFields(Vec<String>),
#[error("Internal server error: {0}")]
StatusQueryError(Error),
#[error("Invalid deployment: {0}")]
InvalidDeployment(DeploymentId),
#[error("Failed to process query: {0}")]
QueryForwardingError(reqwest::Error),
}
impl StatusCodeExt for SubgraphServiceError {
fn status_code(&self) -> StatusCode {
use SubgraphServiceError::*;
match self {
InvalidStatusQuery(_) | UnsupportedStatusQueryFields(_) => StatusCode::BAD_REQUEST,
InvalidDeployment(_) => StatusCode::INTERNAL_SERVER_ERROR,
StatusQueryError(_) => StatusCode::BAD_GATEWAY,
QueryForwardingError(_) => StatusCode::SERVICE_UNAVAILABLE,
}
}
}
// Tell axum how to convert `SubgraphServiceError` into a response.
impl IntoResponse for SubgraphServiceError {
fn into_response(self) -> Response {
tracing::error!(%self, "An SubgraphServiceError occurred.");
let status_code = self.status_code();
ErrorResponse::new(self).into_response(status_code)
}
}
pub trait StatusCodeExt {
fn status_code(&self) -> StatusCode;
}
impl<T> StatusCodeExt for Response<T> {
fn status_code(&self) -> StatusCode {
self.status()
}
}
impl<T, E> StatusCodeExt for Result<T, E>
where
T: StatusCodeExt,
E: StatusCodeExt,
{
fn status_code(&self) -> StatusCode {
match self {
Ok(t) => t.status_code(),
Err(e) => e.status_code(),
}
}
}
impl StatusCodeExt for Infallible {
fn status_code(&self) -> StatusCode {
unreachable!()
}
}