Code review changes

This commit is contained in:
Burke Libbey
2015-03-19 11:17:22 -04:00
parent 83752ed3c5
commit d96cee72fa
3 changed files with 47 additions and 26 deletions
+40 -25
View File
@@ -1,42 +1,57 @@
package logrus_bugsnag
import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/bugsnag/bugsnag-go"
)
// BugsnagHook sends exceptions to an exception-tracking service compatible
// with the Bugsnag API. Before using this hook, you must call
// bugsnag.Configure().
type bugsnagHook struct{}
// ErrBugsnagUnconfigured is returned if NewBugsnagHook is called before
// bugsnag.Configure. Bugsnag must be configured before the hook.
var ErrBugsnagUnconfigured = errors.New("bugsnag must be configured before installing this logrus hook")
// ErrBugsnagSendFailed indicates that the hook failed to submit an error to
// bugsnag. The error was successfully generated, but `bugsnag.Notify()`
// failed.
type ErrBugsnagSendFailed struct {
err error
}
func (e ErrBugsnagSendFailed) Error() string {
return "failed to send error to Bugsnag: " + e.err.Error()
}
// NewBugsnagHook initializes a logrus hook which sends exceptions to an
// exception-tracking service compatible with the Bugsnag API. Before using
// this hook, you must call bugsnag.Configure(). The returned object should be
// registered with a log via `AddHook()`
//
// Entries that trigger an Error, Fatal or Panic should now include an "error"
// field to send to Bugsnag
type BugsnagHook struct{}
// field to send to Bugsnag.
func NewBugsnagHook() (*bugsnagHook, error) {
if bugsnag.Config.APIKey == "" {
return nil, ErrBugsnagUnconfigured
}
return &bugsnagHook{}, nil
}
// Fire forwards an error to Bugsnag. Given a logrus.Entry, it extracts the
// implicitly-required "error" field and sends it off.
func (hook *BugsnagHook) Fire(entry *logrus.Entry) error {
if entry.Data["error"] == nil {
entry.Logger.WithFields(logrus.Fields{
"source": "bugsnag",
}).Warn("Exceptions sent to Bugsnag must have an 'error' key with the error")
return nil
}
// "error" field (or the Message if the error isn't present) and sends it off.
func (hook *bugsnagHook) Fire(entry *logrus.Entry) error {
var notifyErr error
err, ok := entry.Data["error"].(error)
if !ok {
entry.Logger.WithFields(logrus.Fields{
"source": "bugsnag",
}).Warn("Exceptions sent to Bugsnag must have an `error` key of type `error`")
return nil
if ok {
notifyErr = err
} else {
notifyErr = errors.New(entry.Message)
}
bugsnagErr := bugsnag.Notify(err)
bugsnagErr := bugsnag.Notify(notifyErr)
if bugsnagErr != nil {
entry.Logger.WithFields(logrus.Fields{
"source": "bugsnag",
"error": bugsnagErr,
}).Warn("Failed to send error to Bugsnag")
return ErrBugsnagSendFailed{bugsnagErr}
}
return nil
@@ -44,7 +59,7 @@ func (hook *BugsnagHook) Fire(entry *logrus.Entry) error {
// Levels enumerates the log levels on which the error should be forwarded to
// bugsnag: everything at or above the "Error" level.
func (hook *BugsnagHook) Levels() []logrus.Level {
func (hook *bugsnagHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
logrus.FatalLevel,
+1 -1
View File
@@ -37,7 +37,7 @@ func TestNoticeReceived(t *testing.T) {
}))
defer ts.Close()
hook := &BugsnagHook{}
hook := &bugsnagHook{}
bugsnag.Configure(bugsnag.Configuration{
Endpoint: ts.URL,