@@ -41,9 +41,10 @@ type SentryHook struct {
41
41
client * raven.Client
42
42
levels []logrus.Level
43
43
44
- serverName string
45
- ignoreFields map [string ]struct {}
46
- extraFilters map [string ]func (interface {}) interface {}
44
+ serverName string
45
+ ignoreFields map [string ]struct {}
46
+ extraFilters map [string ]func (interface {}) interface {}
47
+ errorHandlers []func (entry * logrus.Entry , err error )
47
48
48
49
asynchronous bool
49
50
@@ -244,23 +245,30 @@ func (hook *SentryHook) Fire(entry *logrus.Entry) error {
244
245
245
246
_ , errCh := hook .client .Capture (packet , nil )
246
247
247
- if hook .asynchronous {
248
+ switch {
249
+ case hook .asynchronous :
248
250
// Our use of hook.mu guarantees that we are following the WaitGroup rule of
249
251
// not calling Add in parallel with Wait.
250
252
hook .wg .Add (1 )
251
253
go func () {
252
254
if err := <- errCh ; err != nil {
253
- fmt .Println (err )
255
+ for _ , handlerFn := range hook .errorHandlers {
256
+ handlerFn (entry , err )
257
+ }
258
+ hook .wg .Done ()
254
259
}
255
- hook .wg .Done ()
256
260
}()
257
261
return nil
258
- } else if timeout := hook .Timeout ; timeout == 0 {
262
+ case hook .Timeout == 0 :
259
263
return nil
260
- } else {
264
+ default :
265
+ timeout := hook .Timeout
261
266
timeoutCh := time .After (timeout )
262
267
select {
263
268
case err := <- errCh :
269
+ for _ , handlerFn := range hook .errorHandlers {
270
+ handlerFn (entry , err )
271
+ }
264
272
return err
265
273
case <- timeoutCh :
266
274
return fmt .Errorf ("no response from sentry server in %s" , timeout )
@@ -342,6 +350,11 @@ func (hook *SentryHook) AddExtraFilter(name string, fn func(interface{}) interfa
342
350
hook .extraFilters [name ] = fn
343
351
}
344
352
353
+ // AddErrorHandler adds a error handler function used when Sentry returns error.
354
+ func (hook * SentryHook ) AddErrorHandler (fn func (entry * logrus.Entry , err error )) {
355
+ hook .errorHandlers = append (hook .errorHandlers , fn )
356
+ }
357
+
345
358
func (hook * SentryHook ) formatExtraData (df * dataField ) (result map [string ]interface {}) {
346
359
// create a map for passing to Sentry's extra data
347
360
result = make (map [string ]interface {}, df .len ())
0 commit comments