2023-05-31 07:26:11 +03:00
|
|
|
//! Utlities for logging errors for admins and displaying errors to users.
|
|
|
|
|
|
|
|
use crate::frontend::authorization::Authorization;
|
|
|
|
use crate::jsonrpc::{JsonRpcErrorData, JsonRpcForwardedResponse};
|
|
|
|
use crate::response_cache::JsonRpcResponseEnum;
|
2023-05-31 21:20:17 +03:00
|
|
|
use crate::rpcs::provider::EthersHttpProvider;
|
2023-06-15 19:50:21 +03:00
|
|
|
use axum::extract::ws::Message;
|
2023-05-31 07:26:11 +03:00
|
|
|
use axum::{
|
|
|
|
headers,
|
|
|
|
http::StatusCode,
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
Json,
|
|
|
|
};
|
|
|
|
use derive_more::{Display, Error, From};
|
2023-05-31 21:20:17 +03:00
|
|
|
use ethers::prelude::ContractError;
|
2023-05-31 07:26:11 +03:00
|
|
|
use http::header::InvalidHeaderValue;
|
|
|
|
use ipnet::AddrParseError;
|
2023-06-16 10:46:27 +03:00
|
|
|
use log::{debug, error, trace, warn};
|
2023-05-31 07:26:11 +03:00
|
|
|
use migration::sea_orm::DbErr;
|
|
|
|
use redis_rate_limiter::redis::RedisError;
|
|
|
|
use reqwest::header::ToStrError;
|
2023-05-31 21:20:17 +03:00
|
|
|
use rust_decimal::Error as DecimalError;
|
2023-05-31 07:26:11 +03:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::value::RawValue;
|
2023-05-31 21:20:17 +03:00
|
|
|
use std::error::Error;
|
2023-06-08 03:26:38 +03:00
|
|
|
use std::sync::Arc;
|
2023-05-31 21:20:17 +03:00
|
|
|
use std::{borrow::Cow, net::IpAddr};
|
2023-05-31 07:26:11 +03:00
|
|
|
use tokio::{sync::AcquireError, task::JoinError, time::Instant};
|
|
|
|
|
|
|
|
pub type Web3ProxyResult<T> = Result<T, Web3ProxyError>;
|
|
|
|
// TODO: take "IntoResponse" instead of Response?
|
|
|
|
pub type Web3ProxyResponse = Web3ProxyResult<Response>;
|
|
|
|
|
|
|
|
impl From<Web3ProxyError> for Web3ProxyResult<()> {
|
|
|
|
fn from(value: Web3ProxyError) -> Self {
|
|
|
|
Err(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:20:17 +03:00
|
|
|
// TODO: replace all String with `Cow<'static, str>`
|
2023-05-31 07:26:11 +03:00
|
|
|
#[derive(Debug, Display, Error, From)]
|
|
|
|
pub enum Web3ProxyError {
|
2023-05-31 22:44:02 +03:00
|
|
|
Abi(ethers::abi::Error),
|
2023-05-31 07:26:11 +03:00
|
|
|
AccessDenied,
|
|
|
|
#[error(ignore)]
|
|
|
|
Anyhow(anyhow::Error),
|
2023-06-08 03:26:38 +03:00
|
|
|
Arc(Arc<Self>),
|
2023-05-31 07:26:11 +03:00
|
|
|
#[error(ignore)]
|
|
|
|
#[from(ignore)]
|
2023-05-31 09:17:05 +03:00
|
|
|
BadRequest(Cow<'static, str>),
|
2023-05-31 07:26:11 +03:00
|
|
|
#[error(ignore)]
|
|
|
|
#[from(ignore)]
|
2023-06-10 04:31:47 +03:00
|
|
|
BadResponse(Cow<'static, str>),
|
2023-05-31 07:26:11 +03:00
|
|
|
BadRouting,
|
2023-05-31 21:20:17 +03:00
|
|
|
Contract(ContractError<EthersHttpProvider>),
|
2023-05-31 07:26:11 +03:00
|
|
|
Database(DbErr),
|
2023-05-31 21:20:17 +03:00
|
|
|
Decimal(DecimalError),
|
2023-05-31 07:26:11 +03:00
|
|
|
#[display(fmt = "{:#?}, {:#?}", _0, _1)]
|
|
|
|
EipVerificationFailed(Box<Web3ProxyError>, Box<Web3ProxyError>),
|
|
|
|
EthersHttpClient(ethers::prelude::HttpClientError),
|
|
|
|
EthersProvider(ethers::prelude::ProviderError),
|
|
|
|
EthersWsClient(ethers::prelude::WsClientError),
|
|
|
|
FlumeRecv(flume::RecvError),
|
|
|
|
GasEstimateNotU256,
|
2023-06-09 22:21:50 +03:00
|
|
|
HdrRecord(hdrhistogram::errors::RecordError),
|
2023-05-31 07:26:11 +03:00
|
|
|
Headers(headers::Error),
|
|
|
|
HeaderToString(ToStrError),
|
|
|
|
Hyper(hyper::Error),
|
|
|
|
InfluxDb2Request(influxdb2::RequestError),
|
|
|
|
#[display(fmt = "{} > {}", min, max)]
|
|
|
|
#[from(ignore)]
|
|
|
|
InvalidBlockBounds {
|
|
|
|
min: u64,
|
|
|
|
max: u64,
|
|
|
|
},
|
|
|
|
InvalidHeaderValue(InvalidHeaderValue),
|
|
|
|
InvalidEip,
|
|
|
|
InvalidInviteCode,
|
|
|
|
Io(std::io::Error),
|
|
|
|
UnknownReferralCode,
|
|
|
|
InvalidReferer,
|
|
|
|
InvalidSignatureLength,
|
2023-06-07 19:39:30 +03:00
|
|
|
InvalidUserTier,
|
2023-05-31 07:26:11 +03:00
|
|
|
InvalidUserAgent,
|
|
|
|
InvalidUserKey,
|
|
|
|
IpAddrParse(AddrParseError),
|
|
|
|
#[error(ignore)]
|
|
|
|
#[from(ignore)]
|
|
|
|
IpNotAllowed(IpAddr),
|
|
|
|
JoinError(JoinError),
|
|
|
|
#[display(fmt = "{:?}", _0)]
|
|
|
|
#[error(ignore)]
|
|
|
|
JsonRpcErrorData(JsonRpcErrorData),
|
|
|
|
#[display(fmt = "{:?}", _0)]
|
|
|
|
#[error(ignore)]
|
|
|
|
MsgPackEncode(rmp_serde::encode::Error),
|
|
|
|
NoBlockNumberOrHash,
|
|
|
|
NoBlocksKnown,
|
|
|
|
NoConsensusHeadBlock,
|
2023-06-16 10:46:27 +03:00
|
|
|
NoDatabase,
|
2023-05-31 07:26:11 +03:00
|
|
|
NoHandleReady,
|
|
|
|
NoServersSynced,
|
|
|
|
#[display(fmt = "{}/{}", num_known, min_head_rpcs)]
|
|
|
|
#[from(ignore)]
|
|
|
|
NotEnoughRpcs {
|
|
|
|
num_known: usize,
|
|
|
|
min_head_rpcs: usize,
|
|
|
|
},
|
|
|
|
#[display(fmt = "{}/{}", available, needed)]
|
|
|
|
#[from(ignore)]
|
|
|
|
NotEnoughSoftLimit {
|
|
|
|
available: u32,
|
|
|
|
needed: u32,
|
|
|
|
},
|
|
|
|
NotFound,
|
|
|
|
NotImplemented,
|
|
|
|
OriginRequired,
|
|
|
|
#[error(ignore)]
|
|
|
|
#[from(ignore)]
|
|
|
|
OriginNotAllowed(headers::Origin),
|
|
|
|
#[display(fmt = "{:?}", _0)]
|
|
|
|
#[error(ignore)]
|
|
|
|
ParseBytesError(Option<ethers::types::ParseBytesError>),
|
|
|
|
ParseMsgError(siwe::ParseError),
|
|
|
|
ParseAddressError,
|
|
|
|
#[display(fmt = "{:?}, {:?}", _0, _1)]
|
|
|
|
RateLimited(Authorization, Option<Instant>),
|
|
|
|
Redis(RedisError),
|
|
|
|
RefererRequired,
|
|
|
|
#[display(fmt = "{:?}", _0)]
|
|
|
|
#[error(ignore)]
|
|
|
|
#[from(ignore)]
|
|
|
|
RefererNotAllowed(headers::Referer),
|
|
|
|
SemaphoreAcquireError(AcquireError),
|
|
|
|
SendAppStatError(flume::SendError<crate::stats::AppStat>),
|
|
|
|
SerdeJson(serde_json::Error),
|
|
|
|
/// simple way to return an error message to the user and an anyhow to our logs
|
|
|
|
#[display(fmt = "{}, {}, {:?}", _0, _1, _2)]
|
2023-06-10 04:31:47 +03:00
|
|
|
StatusCode(StatusCode, Cow<'static, str>, Option<anyhow::Error>),
|
2023-05-31 07:26:11 +03:00
|
|
|
/// TODO: what should be attached to the timout?
|
|
|
|
#[display(fmt = "{:?}", _0)]
|
|
|
|
#[error(ignore)]
|
|
|
|
Timeout(Option<tokio::time::error::Elapsed>),
|
|
|
|
UlidDecode(ulid::DecodeError),
|
|
|
|
UnknownBlockNumber,
|
|
|
|
UnknownKey,
|
|
|
|
UserAgentRequired,
|
|
|
|
#[error(ignore)]
|
|
|
|
UserAgentNotAllowed(headers::UserAgent),
|
|
|
|
UserIdZero,
|
|
|
|
PaymentRequired,
|
|
|
|
VerificationError(siwe::VerificationError),
|
|
|
|
WatchRecvError(tokio::sync::watch::error::RecvError),
|
|
|
|
WatchSendError,
|
|
|
|
WebsocketOnly,
|
|
|
|
#[display(fmt = "{:?}, {}", _0, _1)]
|
|
|
|
#[error(ignore)]
|
2023-06-10 04:31:47 +03:00
|
|
|
WithContext(Option<Box<Web3ProxyError>>, Cow<'static, str>),
|
2023-05-31 07:26:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Web3ProxyError {
|
2023-06-08 03:26:38 +03:00
|
|
|
pub fn as_response_parts<R: Serialize>(&self) -> (StatusCode, JsonRpcResponseEnum<R>) {
|
2023-05-31 07:26:11 +03:00
|
|
|
// TODO: include a unique request id in the data
|
|
|
|
let (code, err): (StatusCode, JsonRpcErrorData) = match self {
|
2023-05-31 22:44:02 +03:00
|
|
|
Self::Abi(err) => {
|
|
|
|
warn!("abi error={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 22:44:02 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-05-31 07:26:11 +03:00
|
|
|
Self::AccessDenied => {
|
|
|
|
// TODO: attach something to this trace. probably don't include much in the message though. don't want to leak creds by accident
|
|
|
|
trace!("access denied");
|
|
|
|
(
|
|
|
|
StatusCode::FORBIDDEN,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "FORBIDDEN".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::FORBIDDEN.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Anyhow(err) => {
|
|
|
|
warn!("anyhow. err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
|
|
|
// TODO: is it safe to expose all of our anyhow strings?
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-06-08 03:26:38 +03:00
|
|
|
Self::Arc(err) => {
|
|
|
|
// recurse
|
|
|
|
return err.as_response_parts::<R>();
|
|
|
|
}
|
2023-05-31 07:26:11 +03:00
|
|
|
Self::BadRequest(err) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("BAD_REQUEST: {}", err);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("bad request: {}", err).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::BadResponse(err) => {
|
|
|
|
// TODO: think about this one more. ankr gives us this because ethers fails to parse responses without an id
|
2023-06-17 01:49:10 +03:00
|
|
|
debug!("BAD_RESPONSE: {:?}", err);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("bad response: {}", err).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::BadRouting => {
|
|
|
|
error!("BadRouting");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "bad routing".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Database(err) => {
|
|
|
|
error!("database err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "database error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-05-31 21:20:17 +03:00
|
|
|
Self::Contract(err) => {
|
|
|
|
warn!("Contract Error: {}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("contract error: {}", err).into(),
|
2023-05-31 21:20:17 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Decimal(err) => {
|
|
|
|
debug!("Decimal Error: {}", err);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("decimal error: {}", err).into(),
|
2023-05-31 21:20:17 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-05-31 07:26:11 +03:00
|
|
|
Self::EipVerificationFailed(err_1, err_191) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!(
|
2023-05-31 07:26:11 +03:00
|
|
|
"EipVerificationFailed err_1={:#?} err2={:#?}",
|
2023-06-16 10:46:27 +03:00
|
|
|
err_1,
|
|
|
|
err_191
|
2023-05-31 07:26:11 +03:00
|
|
|
);
|
|
|
|
(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!(
|
2023-05-31 07:26:11 +03:00
|
|
|
"both the primary and eip191 verification failed: {:#?}; {:#?}",
|
|
|
|
err_1, err_191
|
2023-06-10 04:31:47 +03:00
|
|
|
)
|
|
|
|
.into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::UNAUTHORIZED.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::EthersHttpClient(err) => {
|
|
|
|
warn!("EthersHttpClientError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "ether http client error".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::EthersProvider(err) => {
|
|
|
|
warn!("EthersProviderError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "ether provider error".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::EthersWsClient(err) => {
|
|
|
|
warn!("EthersWsClientError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "ether ws client error".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::FlumeRecv(err) => {
|
|
|
|
warn!("FlumeRecvError err={:#?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "flume recv error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// Self::JsonRpcForwardedError(x) => (StatusCode::OK, x),
|
|
|
|
Self::GasEstimateNotU256 => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("GasEstimateNotU256");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "gas estimate result is not an U256".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-06-09 22:21:50 +03:00
|
|
|
Self::HdrRecord(err) => {
|
|
|
|
warn!("HdrRecord {:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-06-09 22:21:50 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-05-31 07:26:11 +03:00
|
|
|
Self::Headers(err) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("HeadersError {:?}", err);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Hyper(err) => {
|
|
|
|
warn!("hyper err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
|
|
|
// TODO: is it safe to expose these error strings?
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InfluxDb2Request(err) => {
|
|
|
|
// TODO: attach a request id to the message and to this error so that if people report problems, we can dig in sentry to find out more
|
|
|
|
error!("influxdb2 err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "influxdb2 error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidBlockBounds { min, max } => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidBlockBounds min={} max={}", min, max);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!(
|
2023-05-31 07:26:11 +03:00
|
|
|
"Invalid blocks bounds requested. min ({}) > max ({})",
|
|
|
|
min, max
|
2023-06-10 04:31:47 +03:00
|
|
|
)
|
|
|
|
.into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::IpAddrParse(err) => {
|
|
|
|
debug!("IpAddrParse err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::IpNotAllowed(ip) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("IpNotAllowed ip={})", ip);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::FORBIDDEN,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("IP ({}) is not allowed!", ip).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::FORBIDDEN.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidHeaderValue(err) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidHeaderValue err={:?}", err);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidEip => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidEip");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "invalid message eip given".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidInviteCode => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidInviteCode");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "invalid invite code".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::UNAUTHORIZED.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Io(err) => {
|
|
|
|
warn!("std io err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
|
|
|
// TODO: is it safe to expose our io error strings?
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::UnknownReferralCode => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("UnknownReferralCode");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "invalid referral code".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::UNAUTHORIZED.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidReferer => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidReferer");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "invalid referer!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidSignatureLength => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidSignatureLength");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "invalid signature length".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidUserAgent => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidUserAgent");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::FORBIDDEN,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "invalid user agent!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::FORBIDDEN.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::InvalidUserKey => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("InvalidUserKey");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "UserKey was not a ULID or UUID".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
2023-06-07 19:39:30 +03:00
|
|
|
}
|
|
|
|
Self::InvalidUserTier => {
|
|
|
|
warn!("InvalidUserTier");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "UserTier is not valid!".into(),
|
2023-06-07 19:39:30 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
2023-05-31 07:26:11 +03:00
|
|
|
}
|
|
|
|
Self::JoinError(err) => {
|
|
|
|
let code = if err.is_cancelled() {
|
|
|
|
trace!("JoinError. likely shutting down. err={:?}", err);
|
|
|
|
StatusCode::BAD_GATEWAY
|
|
|
|
} else {
|
|
|
|
warn!("JoinError. err={:?}", err);
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
(
|
|
|
|
code,
|
|
|
|
JsonRpcErrorData {
|
|
|
|
// TODO: different messages of cancelled or not?
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "Unable to complete request".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: code.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-06-08 03:26:38 +03:00
|
|
|
Self::JsonRpcErrorData(jsonrpc_error_data) => {
|
|
|
|
// TODO: do this without clone? the Arc needed it though
|
|
|
|
(StatusCode::OK, jsonrpc_error_data.clone())
|
|
|
|
}
|
2023-05-31 07:26:11 +03:00
|
|
|
Self::MsgPackEncode(err) => {
|
|
|
|
warn!("MsgPackEncode Error: {}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("msgpack encode error: {}", err).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NoBlockNumberOrHash => {
|
|
|
|
warn!("NoBlockNumberOrHash");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "Blocks here must have a number or hash".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NoBlocksKnown => {
|
|
|
|
error!("NoBlocksKnown");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "no blocks known".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NoConsensusHeadBlock => {
|
|
|
|
error!("NoConsensusHeadBlock");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "no consensus head block".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-06-16 10:46:27 +03:00
|
|
|
Self::NoDatabase => {
|
|
|
|
error!("no database configured");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
|
|
|
message: "no database configured!".into(),
|
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2023-05-31 07:26:11 +03:00
|
|
|
Self::NoHandleReady => {
|
|
|
|
error!("NoHandleReady");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "unable to retry for request handle".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NoServersSynced => {
|
|
|
|
warn!("NoServersSynced");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "no servers synced".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NotEnoughRpcs {
|
|
|
|
num_known,
|
|
|
|
min_head_rpcs,
|
|
|
|
} => {
|
|
|
|
error!("NotEnoughRpcs {}/{}", num_known, min_head_rpcs);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!(
|
2023-05-31 07:26:11 +03:00
|
|
|
"not enough rpcs connected {}/{}",
|
|
|
|
num_known, min_head_rpcs
|
2023-06-10 04:31:47 +03:00
|
|
|
)
|
|
|
|
.into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NotEnoughSoftLimit { available, needed } => {
|
|
|
|
error!("NotEnoughSoftLimit {}/{}", available, needed);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!(
|
2023-05-31 07:26:11 +03:00
|
|
|
"not enough soft limit available {}/{}",
|
|
|
|
available, needed
|
2023-06-10 04:31:47 +03:00
|
|
|
)
|
|
|
|
.into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NotFound => {
|
|
|
|
// TODO: emit a stat?
|
|
|
|
// TODO: instead of an error, show a normal html page for 404?
|
|
|
|
(
|
|
|
|
StatusCode::NOT_FOUND,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "not found!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::NOT_FOUND.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::NotImplemented => {
|
|
|
|
trace!("NotImplemented");
|
|
|
|
(
|
|
|
|
StatusCode::NOT_IMPLEMENTED,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "work in progress".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::NOT_IMPLEMENTED.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::OriginRequired => {
|
|
|
|
trace!("OriginRequired");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "Origin required".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::OriginNotAllowed(origin) => {
|
|
|
|
trace!("OriginNotAllowed origin={}", origin);
|
|
|
|
(
|
|
|
|
StatusCode::FORBIDDEN,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("Origin ({}) is not allowed!", origin).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::FORBIDDEN.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::ParseBytesError(err) => {
|
|
|
|
trace!("ParseBytesError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "parse bytes error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::ParseMsgError(err) => {
|
|
|
|
trace!("ParseMsgError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "parse message error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::ParseAddressError => {
|
|
|
|
trace!("ParseAddressError");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "unable to parse address".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::PaymentRequired => {
|
|
|
|
trace!("PaymentRequiredError");
|
|
|
|
(
|
|
|
|
StatusCode::PAYMENT_REQUIRED,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "Payment is required and user is not premium".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::PAYMENT_REQUIRED.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// TODO: this should actually by the id of the key. multiple users might control one key
|
|
|
|
Self::RateLimited(authorization, retry_at) => {
|
|
|
|
// TODO: emit a stat
|
|
|
|
|
|
|
|
let retry_msg = if let Some(retry_at) = retry_at {
|
|
|
|
let retry_in = retry_at.duration_since(Instant::now()).as_secs();
|
|
|
|
|
|
|
|
format!(" Retry in {} seconds", retry_in)
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
// create a string with either the IP or the rpc_key_id
|
|
|
|
let msg = if authorization.checks.rpc_secret_key_id.is_none() {
|
|
|
|
format!("too many requests from {}.{}", authorization.ip, retry_msg)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"too many requests from rpc key #{}.{}",
|
|
|
|
authorization.checks.rpc_secret_key_id.unwrap(),
|
|
|
|
retry_msg,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
(
|
|
|
|
StatusCode::TOO_MANY_REQUESTS,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: msg.into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::TOO_MANY_REQUESTS.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Redis(err) => {
|
|
|
|
warn!("redis err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "redis error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::RefererRequired => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("referer required");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "Referer required".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::RefererNotAllowed(referer) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("referer not allowed referer={:?}", referer);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::FORBIDDEN,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("Referer ({:?}) is not allowed", referer).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::FORBIDDEN.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::SemaphoreAcquireError(err) => {
|
|
|
|
warn!("semaphore acquire err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
|
|
|
// TODO: is it safe to expose all of our anyhow strings?
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "semaphore acquire error".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::SendAppStatError(err) => {
|
|
|
|
error!("SendAppStatError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "error stat_sender sending response_stat".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::SerdeJson(err) => {
|
2023-06-01 02:07:13 +03:00
|
|
|
trace!("serde json err={:?} source={:?}", err, err.source());
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("de/serialization error! {}", err).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::StatusCode(status_code, err_msg, err) => {
|
|
|
|
// different status codes should get different error levels. 500s should warn. 400s should stat
|
|
|
|
let code = status_code.as_u16();
|
|
|
|
if (500..600).contains(&code) {
|
|
|
|
warn!("server error {} {:?}: {:?}", code, err_msg, err);
|
|
|
|
} else {
|
|
|
|
trace!("user error {} {:?}: {:?}", code, err_msg, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
(
|
2023-06-08 03:26:38 +03:00
|
|
|
*status_code,
|
2023-05-31 07:26:11 +03:00
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: err_msg.clone(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: code.into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::Timeout(x) => (
|
|
|
|
StatusCode::REQUEST_TIMEOUT,
|
|
|
|
JsonRpcErrorData {
|
2023-06-08 03:26:38 +03:00
|
|
|
message: format!("request timed out: {:?}", x).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::REQUEST_TIMEOUT.as_u16().into(),
|
|
|
|
// TODO: include the actual id!
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Self::HeaderToString(err) => {
|
|
|
|
// trace!(?err, "HeaderToString");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-08 03:26:38 +03:00
|
|
|
message: err.to_string().into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::UlidDecode(err) => {
|
|
|
|
// trace!(?err, "UlidDecodeError");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-08 03:26:38 +03:00
|
|
|
message: format!("{}", err).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::UnknownBlockNumber => {
|
|
|
|
error!("UnknownBlockNumber");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_GATEWAY,
|
|
|
|
JsonRpcErrorData {
|
2023-06-08 03:26:38 +03:00
|
|
|
message: "no servers synced. unknown eth_blockNumber".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_GATEWAY.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// TODO: stat?
|
|
|
|
Self::UnknownKey => (
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "unknown api key!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::UNAUTHORIZED.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Self::UserAgentRequired => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("UserAgentRequired");
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "User agent required".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::UserAgentNotAllowed(ua) => {
|
2023-06-16 10:46:27 +03:00
|
|
|
trace!("UserAgentNotAllowed ua={}", ua);
|
2023-05-31 07:26:11 +03:00
|
|
|
(
|
|
|
|
StatusCode::FORBIDDEN,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: format!("User agent ({}) is not allowed!", ua).into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::FORBIDDEN.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::UserIdZero => {
|
|
|
|
warn!("UserIdZero");
|
|
|
|
// TODO: this might actually be an application error and not a BAD_REQUEST
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "user ids should always be non-zero".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::VerificationError(err) => {
|
|
|
|
trace!("VerificationError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "verification error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::WatchRecvError(err) => {
|
|
|
|
error!("WatchRecvError err={:?}", err);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "watch recv error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::WatchSendError => {
|
|
|
|
error!("WatchSendError");
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: "watch send error!".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::WebsocketOnly => {
|
|
|
|
trace!("WebsocketOnly");
|
|
|
|
(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
JsonRpcErrorData {
|
2023-06-08 03:26:38 +03:00
|
|
|
message: "redirect_public_url not set. only websockets work here".into(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::BAD_REQUEST.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Self::WithContext(err, msg) => match err {
|
|
|
|
Some(err) => {
|
|
|
|
warn!("{:#?} w/ context {}", err, msg);
|
2023-06-08 03:26:38 +03:00
|
|
|
return err.as_response_parts();
|
2023-05-31 07:26:11 +03:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
warn!("error w/ context {}", msg);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
JsonRpcErrorData {
|
2023-06-10 04:31:47 +03:00
|
|
|
message: msg.clone(),
|
2023-05-31 07:26:11 +03:00
|
|
|
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16().into(),
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
(code, JsonRpcResponseEnum::from(err))
|
|
|
|
}
|
|
|
|
|
2023-06-13 07:51:08 +03:00
|
|
|
#[inline]
|
|
|
|
pub fn into_response_with_id(self, id: Option<Box<RawValue>>) -> Response {
|
2023-06-08 03:26:38 +03:00
|
|
|
let (status_code, response_data) = self.as_response_parts();
|
2023-05-31 07:26:11 +03:00
|
|
|
|
2023-06-13 07:51:08 +03:00
|
|
|
let id = id.unwrap_or_default();
|
|
|
|
|
2023-05-31 07:26:11 +03:00
|
|
|
let response = JsonRpcForwardedResponse::from_response_data(response_data, id);
|
|
|
|
|
|
|
|
(status_code, Json(response)).into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ethers::types::ParseBytesError> for Web3ProxyError {
|
|
|
|
fn from(err: ethers::types::ParseBytesError) -> Self {
|
|
|
|
Self::ParseBytesError(Some(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<tokio::time::error::Elapsed> for Web3ProxyError {
|
|
|
|
fn from(err: tokio::time::error::Elapsed) -> Self {
|
|
|
|
Self::Timeout(Some(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for Web3ProxyError {
|
|
|
|
#[inline]
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
self.into_response_with_id(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Web3ProxyErrorContext<T> {
|
2023-06-10 04:31:47 +03:00
|
|
|
fn web3_context<S: Into<Cow<'static, str>>>(self, msg: S) -> Result<T, Web3ProxyError>;
|
2023-05-31 07:26:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Web3ProxyErrorContext<T> for Option<T> {
|
2023-06-10 04:31:47 +03:00
|
|
|
fn web3_context<S: Into<Cow<'static, str>>>(self, msg: S) -> Result<T, Web3ProxyError> {
|
2023-05-31 07:26:11 +03:00
|
|
|
self.ok_or(Web3ProxyError::WithContext(None, msg.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> Web3ProxyErrorContext<T> for Result<T, E>
|
|
|
|
where
|
|
|
|
E: Into<Web3ProxyError>,
|
|
|
|
{
|
2023-06-10 04:31:47 +03:00
|
|
|
fn web3_context<S: Into<Cow<'static, str>>>(self, msg: S) -> Result<T, Web3ProxyError> {
|
2023-05-31 07:26:11 +03:00
|
|
|
self.map_err(|err| Web3ProxyError::WithContext(Some(Box::new(err.into())), msg.into()))
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 19:50:21 +03:00
|
|
|
|
|
|
|
impl Web3ProxyError {
|
|
|
|
pub fn into_message(self, id: Option<Box<RawValue>>) -> Message {
|
|
|
|
let (_, err) = self.as_response_parts();
|
|
|
|
|
|
|
|
let id = id.unwrap_or_default();
|
|
|
|
|
|
|
|
let err = JsonRpcForwardedResponse::from_response_data(err, id);
|
|
|
|
|
|
|
|
let msg = serde_json::to_string(&err).expect("errors should always serialize to json");
|
|
|
|
|
|
|
|
// TODO: what about a binary message?
|
|
|
|
Message::Text(msg)
|
|
|
|
}
|
|
|
|
}
|