forked from helius-labs/photon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
93 lines (87 loc) · 3.04 KB
/
error.rs
File metadata and controls
93 lines (87 loc) · 3.04 KB
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
use crate::common::typedefs::hash::ParseHashError;
use crate::metric;
use cadence_macros::statsd_count;
use jsonrpsee::core::Error as RpcError;
use jsonrpsee::types::error::CallError;
use log::error;
use solana_pubkey::ParsePubkeyError;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum PhotonApiError {
#[error("Validation Error: {0}")]
ValidationError(String),
#[error("Invalid Public Key: field '{field}'")]
InvalidPubkey { field: String },
#[error("Database Error: {0}")]
DatabaseError(#[from] sea_orm::DbErr),
#[error("Record Not Found: {0}")]
RecordNotFound(String),
#[error("Unexpected Error: {0}")]
UnexpectedError(String),
#[error("Node is behind {0} slots")]
StaleSlot(u64),
}
// TODO: Simplify error conversions and ensure we adhere
// to the proper RPC and HTTP codes.
impl From<PhotonApiError> for RpcError {
fn from(val: PhotonApiError) -> Self {
match val {
PhotonApiError::ValidationError(_) => {
metric! {
statsd_count!("validation_api_error", 1);
}
invalid_request(val)
}
PhotonApiError::InvalidPubkey { .. } => {
metric! {
statsd_count!("invalid_pubkey_api_error", 1);
}
invalid_request(val)
}
PhotonApiError::RecordNotFound(_) => {
metric! {
statsd_count!("record_not_found_api_error", 1);
}
invalid_request(val)
}
PhotonApiError::StaleSlot(_) => {
metric! {
statsd_count!("stale_slot_api_error", 1);
}
invalid_request(val)
}
PhotonApiError::DatabaseError(e) => {
error!("Internal server database error: {}", e);
metric! {
statsd_count!("internal_database_api_error", 1);
}
internal_server_error()
}
PhotonApiError::UnexpectedError(e) => {
error!("Internal server error: {}", e);
metric! {
statsd_count!("unexpected_api_error", 1);
}
internal_server_error()
}
}
}
}
// The API contract receives parsed input from the user, so if we get a ParseHashError it means
// that the database itself returned an invalid hash.
impl From<ParseHashError> for PhotonApiError {
fn from(_error: ParseHashError) -> Self {
PhotonApiError::UnexpectedError("Invalid hash in database".to_string())
}
}
impl From<ParsePubkeyError> for PhotonApiError {
fn from(_error: ParsePubkeyError) -> Self {
PhotonApiError::UnexpectedError("Invalid public key in database".to_string())
}
}
fn invalid_request(e: PhotonApiError) -> RpcError {
RpcError::Call(CallError::from_std_error(e))
}
fn internal_server_error() -> RpcError {
RpcError::Call(CallError::Failed(anyhow::anyhow!("Internal server error")))
}