-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an autoconnect-web Error type
and don't report NoWebsocketUpgrades to sentry SYNC-3889
- Loading branch information
Showing
9 changed files
with
111 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use actix_http::ws::HandshakeError; | ||
use actix_web::{error::ResponseError, http::StatusCode, HttpResponse}; | ||
use backtrace::Backtrace; | ||
use serde_json::json; | ||
|
||
use autopush_common::errors::ReportableError; | ||
|
||
/// The main error type | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum ApiError { | ||
#[error("Actix Web error: {0}")] | ||
Actix(#[from] actix_web::error::Error), | ||
|
||
#[error("LogCheck")] | ||
LogCheck, | ||
} | ||
|
||
impl ResponseError for ApiError { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
ApiError::Actix(e) => e.as_response_error().status_code(), | ||
ApiError::LogCheck => StatusCode::IM_A_TEAPOT, | ||
} | ||
} | ||
|
||
fn error_response(&self) -> HttpResponse { | ||
HttpResponse::build(self.status_code()).json(json!({ | ||
"code": self.status_code().as_u16(), | ||
"errno": self.errno(), | ||
"error": self.to_string(), | ||
})) | ||
} | ||
} | ||
|
||
impl ReportableError for ApiError { | ||
fn backtrace(&self) -> Option<&Backtrace> { | ||
None | ||
} | ||
|
||
fn is_sentry_event(&self) -> bool { | ||
match self { | ||
// Ignore failing upgrade to WebSocket | ||
ApiError::Actix(e) => e.as_error() != Some(&HandshakeError::NoWebsocketUpgrade), | ||
_ => true, | ||
} | ||
} | ||
|
||
fn metric_label(&self) -> Option<&'static str> { | ||
None | ||
} | ||
} | ||
|
||
impl ApiError { | ||
/// Return a unique errno code per variant | ||
pub fn errno(&self) -> i32 { | ||
match self { | ||
ApiError::Actix(_) => 500, | ||
ApiError::LogCheck => 999, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters