-
-
Notifications
You must be signed in to change notification settings - Fork 167
fix(actix): capture only server errors #877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd0fba3
fix(actix): capture only server errors
lcian e6a03ad
changelog
lcian 039109d
changelog
lcian 8a288e6
Merge branch 'master' into lcian/fix/actix-server-errors
lcian 28d59cf
Merge branch 'master' into lcian/fix/actix-server-errors
lcian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -351,7 +351,9 @@ where | |
let mut res: Self::Response = match fut.await { | ||
Ok(res) => res, | ||
Err(e) => { | ||
if inner.capture_server_errors { | ||
// Errors returned by middleware, and possibly other lower level errors | ||
if inner.capture_server_errors && e.error_response().status().is_server_error() | ||
{ | ||
hub.capture_error(&e); | ||
} | ||
|
||
|
@@ -474,6 +476,7 @@ fn process_event(mut event: Event<'static>, request: &Request) -> Event<'static> | |
mod tests { | ||
use std::io; | ||
|
||
use actix_web::body::BoxBody; | ||
use actix_web::test::{call_service, init_service, TestRequest}; | ||
use actix_web::{get, web, App, HttpRequest, HttpResponse}; | ||
use futures::executor::block_on; | ||
|
@@ -622,9 +625,9 @@ mod tests { | |
} | ||
} | ||
|
||
/// Ensures client errors (4xx) are not captured. | ||
/// Ensures client errors (4xx) returned by service are not captured. | ||
lcian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[actix_web::test] | ||
async fn test_client_errors_discarded() { | ||
async fn test_service_client_errors_discarded() { | ||
let events = sentry::test::with_captured_events(|| { | ||
block_on(async { | ||
let service = HttpResponse::NotFound; | ||
|
@@ -645,6 +648,66 @@ mod tests { | |
assert!(events.is_empty()); | ||
} | ||
|
||
/// Ensures client errors (4xx) returned by middleware are not captured. | ||
#[actix_web::test] | ||
async fn test_middleware_client_errors_discarded() { | ||
let events = sentry::test::with_captured_events(|| { | ||
block_on(async { | ||
async fn hello_world() -> HttpResponse { | ||
HttpResponse::Ok().body("Hello, world!") | ||
} | ||
|
||
let app = init_service( | ||
App::new() | ||
.wrap_fn(|_, _| async { | ||
Err(actix_web::error::ErrorNotFound("Not found")) | ||
as Result<ServiceResponse<BoxBody>, _> | ||
}) | ||
.wrap(Sentry::builder().with_hub(Hub::current()).finish()) | ||
.service(web::resource("/test").to(hello_world)), | ||
) | ||
.await; | ||
|
||
let req = TestRequest::get().uri("/test").to_request(); | ||
let res = app.call(req).await; | ||
assert!(res.is_err()); | ||
assert!(res.unwrap_err().error_response().status().is_client_error()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] I would check the exact status code, not just that it is a client error here |
||
}) | ||
}); | ||
|
||
assert!(events.is_empty()); | ||
} | ||
|
||
/// Ensures server errors (5xx) returned by middleware are captured. | ||
#[actix_web::test] | ||
async fn test_middleware_server_errors_captured() { | ||
let events = sentry::test::with_captured_events(|| { | ||
block_on(async { | ||
async fn hello_world() -> HttpResponse { | ||
HttpResponse::Ok().body("Hello, world!") | ||
} | ||
|
||
let app = init_service( | ||
App::new() | ||
.wrap_fn(|_, _| async { | ||
Err(actix_web::error::ErrorInternalServerError("Server error")) | ||
as Result<ServiceResponse<BoxBody>, _> | ||
}) | ||
.wrap(Sentry::builder().with_hub(Hub::current()).finish()) | ||
.service(web::resource("/test").to(hello_world)), | ||
) | ||
.await; | ||
|
||
let req = TestRequest::get().uri("/test").to_request(); | ||
let res = app.call(req).await; | ||
assert!(res.is_err()); | ||
assert!(res.unwrap_err().error_response().status().is_server_error()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] also would check exact status code here |
||
}) | ||
}); | ||
|
||
assert_eq!(events.len(), 1); | ||
} | ||
|
||
/// Ensures transaction name can be overridden in handler scope. | ||
#[actix_web::test] | ||
async fn test_override_transaction_name() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.