1
+ 'use strict' ;
2
+
3
+ /* -*- javascript -*- */
4
+ /* *******************************************************************
5
+ * @author Evangelos Pappas <[email protected] >
6
+ * @copyright (C) 2014, evalonlabs
7
+ * Copyright 2015, evalonlabs
8
+ *
9
+ * The MIT License (MIT)
10
+ *
11
+ * Copyright (c) 2015 Evangelos Pappas
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ * of this software and associated documentation files (the "Software"), to deal
15
+ * in the Software without restriction, including without limitation the rights
16
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ * copies of the Software, and to permit persons to whom the Software is
18
+ * furnished to do so, subject to the following conditions:
19
+ *
20
+ * The above copyright notice and this permission notice shall be included in all
21
+ * copies or substantial portions of the Software.
22
+ *
23
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ * SOFTWARE.
30
+ *
31
+ * @doc
32
+ *
33
+ * @end
34
+ * *******************************************************************/
35
+
36
+ // Load modules
37
+ var rollbar = require ( 'rollbar' ) ;
38
+
39
+ exports . register = function ( server , options , next ) {
40
+ var rollbarKey = options . accessToken ;
41
+
42
+ var rollbarOpts = options ;
43
+ rollbarOpts . environment = rollbarOpts . environment || process . env . NODE_ENV ;
44
+ rollbarOpts . exitOnUncaughtException = typeof options . exitOnUncaughtException !== 'undefined' ?
45
+ options . exitOnUncaughtException : true ;
46
+
47
+ rollbar . init ( rollbarKey , rollbarOpts ) ;
48
+ rollbar . handleUncaughtExceptions ( rollbarKey , rollbarOpts ) ;
49
+
50
+ server . on ( 'stop' , function stop ( ) {
51
+ rollbar . shutdown ( ) ;
52
+ } ) ;
53
+
54
+ server . ext ( 'onPreResponse' , function onPreResponse ( request , next ) {
55
+ var response = request . response ;
56
+
57
+ if ( response . isBoom ) {
58
+ rollbar . reportMessage ( response , 'info' , formatReq ( request ) ) ;
59
+ }
60
+
61
+ next . continue ( ) ;
62
+ } ) ;
63
+
64
+ server . on ( 'log' , function rollbarLog ( event , tags ) {
65
+ if ( tags . rollbarError ) {
66
+ return rollbar . handleError ( event . err , formatReq ( event . req ) ) ;
67
+ }
68
+ if ( tags . rollbarMessage ) {
69
+ return rollbar . reportMessage ( event . msg , event . level || 'info' , formatReq ( event . req ) ) ;
70
+ }
71
+ } ) ;
72
+
73
+ server . on ( 'request-internal' , function requestInternal ( request , event , tags ) {
74
+ if ( tags . error && tags . state ) {
75
+ return rollbar . reportMessage ( event , event . level || 'warning' , formatReq ( request ) ) ;
76
+ }
77
+ } ) ;
78
+
79
+ server . on ( 'internalerror' , function internalError ( req , error ) {
80
+ return rollbar . handleError ( error , formatReq ( req ) ) ;
81
+ } ) ;
82
+
83
+ server . on ( 'request-error' , function requestError ( request , err ) {
84
+ return rollbar . handleError ( err , formatReq ( request ) ) ;
85
+ } ) ;
86
+
87
+ server . expose ( 'rollbar' , rollbar ) ;
88
+
89
+ server . method ( 'handleError' , function handleError ( err , req , next ) {
90
+ rollbar . handleError ( err , formatReq ( req ) , next ) ;
91
+ } ) ;
92
+
93
+ server . method ( 'handleErrorWithPayloadData' , function handleErrorWithPayloadData ( err , opts , req , next ) {
94
+ rollbar . handleErrorWithPayloadData ( err , opts , formatReq ( req ) , next ) ;
95
+ } ) ;
96
+
97
+ server . method ( 'reportMessage' , function reportMessage ( msg , level , req , next ) {
98
+ rollbar . reportMessage ( msg , level , formatReq ( req ) , next ) ;
99
+ } ) ;
100
+
101
+ server . method ( 'reportMessageWithPayloadData' , function reportMessageWithPayloadData ( msg , opts , req , next ) {
102
+ rollbar . reportMessageWithPayloadData ( msg , opts , formatReq ( req ) , next ) ;
103
+ } ) ;
104
+
105
+ next ( ) ;
106
+ } ;
107
+
108
+ exports . register . attributes = {
109
+ pkg : require ( '../package.json' )
110
+ } ;
111
+
112
+ function formatReq ( request ) {
113
+ if ( ! request ) return ;
114
+
115
+ var req = request . raw . req ;
116
+
117
+ req . socket = {
118
+ encrypted : request . server . info . protocol === 'https'
119
+ } ;
120
+
121
+ req . connection = {
122
+ remoteAddress : request . info . remoteAddress
123
+ } ;
124
+
125
+ return req ;
126
+ }
0 commit comments