|
1 | 1 | use std::fmt; |
2 | 2 |
|
| 3 | +use tendermint_rpc::error::{ |
| 4 | + Error as TendermintRpcError, ErrorDetail as TendermintRpcErrorDetail, |
| 5 | +}; |
| 6 | +use tendermint_rpc::response_error::Code as TendermintRpcCode; |
| 7 | + |
| 8 | +/// Override the log level of errors. |
| 9 | +/// |
| 10 | +/// Changes logs from [`tracing::error`] to [`tracing::debug`]. |
| 11 | +pub trait SilenceLog { |
| 12 | + fn silence_log(&self) -> bool; |
| 13 | +} |
| 14 | + |
| 15 | +impl SilenceLog for CometWrapperError { |
| 16 | + fn silence_log(&self) -> bool { |
| 17 | + // Check if the error originates from a block that hasn't been committed |
| 18 | + // yet |
| 19 | + if let Some(err) = self |
| 20 | + .0 |
| 21 | + .chain() |
| 22 | + .find_map(|err| err.downcast_ref::<TendermintRpcError>()) |
| 23 | + { |
| 24 | + let TendermintRpcErrorDetail::Response(resp) = err.detail() else { |
| 25 | + return false; |
| 26 | + }; |
| 27 | + |
| 28 | + if resp.source.code() != TendermintRpcCode::InternalError { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + return resp.source.data().is_some_and(|msg| { |
| 33 | + msg.contains("could not find results for height") |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + // By default, never silence error logs |
| 38 | + false |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl SilenceLog for MainError { |
| 43 | + fn silence_log(&self) -> bool { |
| 44 | + false |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl SilenceLog for anyhow::Error { |
| 49 | + fn silence_log(&self) -> bool { |
| 50 | + false |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +pub struct CometWrapperError(pub anyhow::Error); |
| 55 | + |
| 56 | +impl fmt::Debug for CometWrapperError { |
| 57 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 58 | + <anyhow::Error as fmt::Debug>::fmt(&self.0, f) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl fmt::Display for CometWrapperError { |
| 63 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 64 | + <anyhow::Error as fmt::Display>::fmt(&self.0, f) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl std::error::Error for CometWrapperError {} |
| 69 | + |
3 | 70 | #[derive(PartialEq, Eq)] |
4 | 71 | pub struct MainError; |
5 | 72 |
|
|
0 commit comments