Skip to content

Commit

Permalink
fix: defer Error::source methods to inner kind's (#486)
Browse files Browse the repository at this point in the history
and kill ReportableError's redundant inheritance of fmt::Display

SYNC-3977
  • Loading branch information
pjenvey authored Oct 30, 2023
1 parent 09db55f commit a9a1796
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
18 changes: 9 additions & 9 deletions autoconnect/autoconnect-ws/autoconnect-ws-sm/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
use std::fmt;
use std::{error::Error, fmt};

use actix_ws::CloseCode;
use backtrace::Backtrace;

use autopush_common::{db::error::DbError, errors::ReportableError};

pub type NonStdBacktrace = backtrace::Backtrace;

/// WebSocket state machine errors
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub struct SMError {
pub kind: SMErrorKind,
/// Avoid thiserror's automatic `std::backtrace::Backtrace` integration by
/// not using the type name "Backtrace". The older `backtrace::Backtrace`
/// is still preferred for Sentry integration:
/// https://github.com/getsentry/sentry-rust/issues/600
backtrace: NonStdBacktrace,
backtrace: Backtrace,
}

impl fmt::Display for SMError {
Expand All @@ -24,6 +18,12 @@ impl fmt::Display for SMError {
}
}

impl Error for SMError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.kind.source()
}
}

// Forward From impls to SMError from SMErrorKind. Because From is reflexive,
// this impl also takes care of From<SMErrorKind>.
impl<T> From<T> for SMError
Expand Down
10 changes: 8 additions & 2 deletions autoconnect/autoconnect-ws/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{error::Error, fmt};

use actix_ws::CloseCode;
use backtrace::Backtrace;
Expand All @@ -7,7 +7,7 @@ use autoconnect_ws_sm::{SMError, WebPushClient};
use autopush_common::{errors::ReportableError, sentry::event_from_error};

/// WebPush WebSocket Handler Errors
#[derive(Debug, thiserror::Error)]
#[derive(Debug)]
pub struct WSError {
pub kind: WSErrorKind,
backtrace: Option<Backtrace>,
Expand All @@ -19,6 +19,12 @@ impl fmt::Display for WSError {
}
}

impl Error for WSError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.kind.source()
}
}

// Forward From impls to WSError from WSErrorKind. Because From is reflexive,
// this impl also takes care of From<WSErrorKind>.
impl<T> From<T> for WSError
Expand Down
2 changes: 1 addition & 1 deletion autopush-common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl ApcErrorKind {
}

/// Interface for reporting our Error types to Sentry or as metrics
pub trait ReportableError: std::error::Error + fmt::Display {
pub trait ReportableError: std::error::Error {
/// Return a `Backtrace` for this Error if one was captured
fn backtrace(&self) -> Option<&Backtrace>;

Expand Down

0 comments on commit a9a1796

Please sign in to comment.