Skip to content

Commit

Permalink
ErrorHandlerFunc: add error log fields based on function reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua T Corbin committed Jan 14, 2016
1 parent ac0103a commit 9615a6d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package tchannel

import (
"reflect"
"runtime"
"sync"

"golang.org/x/net/context"
Expand Down Expand Up @@ -50,14 +52,25 @@ type ErrorHandlerFunc func(ctx context.Context, call *InboundCall) error
// Handle calls f(ctx, call)
func (f ErrorHandlerFunc) Handle(ctx context.Context, call *InboundCall) {
if err := f(ctx, call); err != nil {
call.log.WithFields(ErrField(err)).Error("Handler error.")
resp := call.Response()
if err := resp.SendSystemError(err); err != nil {
call.log.WithFields(ErrField(err)).Error("Failed to send system error.")
log := call.log.WithFields(f.getLogFields()...)
log.WithFields(ErrField(err)).Error("Handler error.")
if err := call.Response().SendSystemError(err); err != nil {
log.WithFields(ErrField(err)).Error("Failed to send system error.")
}
}
}

func (f ErrorHandlerFunc) getLogFields() LogFields {
ptr := reflect.ValueOf(f).Pointer()
handlerFunc := runtime.FuncForPC(ptr) // can't be nil
fileName, fileLine := handlerFunc.FileLine(ptr)
return LogFields{
{"handlerFuncName", handlerFunc.Name()},
{"handlerFuncFileName", fileName},
{"handlerFuncFileLine", fileLine},
}
}

// Manages handlers
type handlerMap struct {
sync.RWMutex
Expand Down

0 comments on commit 9615a6d

Please sign in to comment.