@@ -38,27 +38,21 @@ exports.register = (server, options) => {
38
38
} ,
39
39
} ) ;
40
40
41
- let catchLogErrors = false ;
42
41
let errorTags = [ 'error' , 'fatal' , 'fail' ] ;
43
- if ( opts . catchLogErrors ) {
44
- catchLogErrors = true ;
45
- if ( Array . isArray ( opts . catchLogErrors ) ) {
46
- errorTags = opts . catchLogErrors ;
47
- }
42
+ if ( opts . catchLogErrors && Array . isArray ( opts . catchLogErrors ) ) {
43
+ errorTags = opts . catchLogErrors ;
48
44
}
49
45
50
- function shouldIgnoreEvent ( event ) {
51
- const match = catchLogErrors &&
52
- event . error &&
53
- errorTags . reduce ( ( cond , tag ) => cond || event . tags . includes ( tag ) , false ) ;
54
-
55
- return event . channel === 'app' && ! match ;
56
- }
46
+ const channels = [ 'error' ] ;
47
+ // also listen for app events to get log messages
48
+ if ( opts . catchLogErrors ) channels . push ( 'app' ) ;
57
49
58
50
// get request errors to capture them with sentry
59
- server . events . on ( { name : 'request' , channels : [ 'error' , 'app' ] } , ( request , event ) => {
60
- if ( shouldIgnoreEvent ( event ) ) {
61
- return ;
51
+ server . events . on ( { name : 'request' , channels } , ( request , event ) => {
52
+ // check for errors in request logs
53
+ if ( event . channel === 'app' ) {
54
+ if ( ! event . error ) return ; // no error, just a log message
55
+ if ( event . tags . some ( tag => errorTags . includes ( tag ) ) === false ) return ; // no matching tag
62
56
}
63
57
64
58
Sentry . withScope ( scope => { // thus use a temp scope and re-assign it
@@ -92,23 +86,24 @@ exports.register = (server, options) => {
92
86
} ) ;
93
87
} ) ;
94
88
95
- server . events . on ( 'log' , event => {
96
- if ( shouldIgnoreEvent ( event ) ) {
97
- return ;
98
- }
89
+ if ( opts . catchLogErrors ) {
90
+ server . events . on ( { name : 'log' , channels : [ 'app' ] } , event => {
91
+ if ( ! event . error ) return ; // no error, just a log message
92
+ if ( event . tags . some ( tag => errorTags . includes ( tag ) ) === false ) return ; // no matching tag
99
93
100
- Sentry . withScope ( scope => {
101
- scope . addEventProcessor ( sentryEvent => {
102
- sentryEvent . level = 'error' ;
94
+ Sentry . withScope ( scope => {
95
+ scope . addEventProcessor ( sentryEvent => {
96
+ sentryEvent . level = 'error' ;
103
97
104
- // some SDK identificator
105
- sentryEvent . sdk = { name : 'sentry.javascript.node.hapi' , version } ;
106
- return sentryEvent ;
107
- } ) ;
98
+ // some SDK identificator
99
+ sentryEvent . sdk = { name : 'sentry.javascript.node.hapi' , version } ;
100
+ return sentryEvent ;
101
+ } ) ;
108
102
109
- Sentry . captureException ( event . error ) ;
103
+ Sentry . captureException ( event . error ) ;
104
+ } ) ;
110
105
} ) ;
111
- } ) ;
106
+ }
112
107
} ;
113
108
114
109
exports . name = name ;
0 commit comments