11extern crate actix_web;
22extern crate failure;
33extern crate sentry;
4+ extern crate uuid;
45
5- use std:: env;
6- use std:: io;
7-
6+ use std:: sync:: Arc ;
7+ use uuid:: Uuid ;
88use actix_web:: middleware:: { Middleware , Response , Started } ;
9- use actix_web:: { server , App , Error , HttpMessage , HttpRequest , HttpResponse } ;
9+ use actix_web:: { Error , HttpMessage , HttpRequest , HttpResponse } ;
1010use failure:: Fail ;
1111use sentry:: integrations:: failure:: exception_from_single_fail;
1212use sentry:: protocol:: { Event , Level } ;
@@ -17,7 +17,7 @@ pub struct CaptureSentryError;
1717
1818impl < S : ' static > Middleware < S > for CaptureSentryError {
1919 fn start ( & self , req : & mut HttpRequest < S > ) -> Result < Started , Error > {
20- let hub = Hub :: new_from_top ( Hub :: current ( ) ) ;
20+ let hub = Arc :: new ( Hub :: new_from_top ( Hub :: current ( ) ) ) ;
2121 let outer_req = req;
2222 let req = outer_req. clone ( ) ;
2323 hub. add_event_processor ( Box :: new ( move || {
@@ -51,52 +51,56 @@ impl<S: 'static> Middleware<S> for CaptureSentryError {
5151 fn response ( & self , req : & mut HttpRequest < S > , resp : HttpResponse ) -> Result < Response , Error > {
5252 if resp. status ( ) . is_server_error ( ) {
5353 if let Some ( error) = resp. error ( ) {
54- let hub = req. extensions ( ) . get ( ) . unwrap ( ) ;
55- report_actix_error_to_sentry ( error, hub) ;
54+ let hub = Hub :: from_request ( req) ;
55+ println ! ( "capturing error" ) ;
56+ hub. capture_actix_error ( error) ;
5657 }
5758 }
5859 Ok ( Response :: Done ( resp) )
5960 }
6061}
6162
62- pub fn report_actix_error_to_sentry ( err : & Error , hub : & Hub ) {
63- let mut exceptions = vec ! [ ] ;
64- let mut ptr: Option < & Fail > = Some ( err. as_fail ( ) ) ;
65- let mut idx = 0 ;
66- while let Some ( fail) = ptr {
67- exceptions. push ( exception_from_single_fail (
68- fail,
69- if idx == 0 {
70- Some ( err. backtrace ( ) )
71- } else {
72- fail. backtrace ( )
73- } ,
74- ) ) ;
75- ptr = fail. cause ( ) ;
76- idx += 1 ;
77- }
78- exceptions. reverse ( ) ;
79- hub. capture_event ( Event {
80- exceptions : exceptions,
81- level : Level :: Error ,
82- ..Default :: default ( )
83- } ) ;
63+ /// Utility function that takes an actix error and reports it to the default hub.
64+ pub fn capture_actix_error ( err : & Error ) -> Uuid {
65+ Hub :: with_active ( |hub| {
66+ hub. capture_actix_error ( err)
67+ } )
8468}
8569
86- fn failing ( _req : HttpRequest ) -> Result < String , Error > {
87- Err ( io:: Error :: new ( io:: ErrorKind :: Other , "Something went really wrong here" ) . into ( ) )
70+ /// Hub extensions for actix.
71+ pub trait HubExt {
72+ /// Returns the hub from a given http request.
73+ fn from_request < S > ( req : & HttpRequest < S > ) -> & Arc < Hub > ;
74+ /// Captures an actix error on the given hub.
75+ fn capture_actix_error ( & self , err : & Error ) -> Uuid ;
8876}
8977
90- fn main ( ) {
91- let _guard = sentry :: init ( "https://a94ae32be2584e0bbd7a4cbb95971fee@sentry.io/1041156" ) ;
92- env :: set_var ( "RUST_BACKTRACE" , "1" ) ;
93- sentry :: integrations :: panic :: register_panic_handler ( ) ;
78+ impl HubExt for Hub {
79+ fn from_request < S > ( req : & HttpRequest < S > ) -> & Arc < Hub > {
80+ req . extensions ( ) . get ( ) . expect ( "CaptureSentryError middleware was not registered" )
81+ }
9482
95- server:: new ( || {
96- App :: new ( )
97- . middleware ( CaptureSentryError )
98- . resource ( "/" , |r| r. f ( failing) )
99- } ) . bind ( "127.0.0.1:3001" )
100- . unwrap ( )
101- . run ( ) ;
83+ fn capture_actix_error ( & self , err : & Error ) -> Uuid {
84+ let mut exceptions = vec ! [ ] ;
85+ let mut ptr: Option < & Fail > = Some ( err. as_fail ( ) ) ;
86+ let mut idx = 0 ;
87+ while let Some ( fail) = ptr {
88+ exceptions. push ( exception_from_single_fail (
89+ fail,
90+ if idx == 0 {
91+ Some ( err. backtrace ( ) )
92+ } else {
93+ fail. backtrace ( )
94+ } ,
95+ ) ) ;
96+ ptr = fail. cause ( ) ;
97+ idx += 1 ;
98+ }
99+ exceptions. reverse ( ) ;
100+ self . capture_event ( Event {
101+ exceptions : exceptions,
102+ level : Level :: Error ,
103+ ..Default :: default ( )
104+ } )
105+ }
102106}
0 commit comments