-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notify): use logrus to capture errors (#365)
Signed-off-by: Simon Delberghe <open-source@orandin.fr>
- Loading branch information
Showing
5 changed files
with
50 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,40 @@ | ||
package notify | ||
|
||
import "fmt" | ||
import ( | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/ovh/utask" | ||
) | ||
|
||
const ( | ||
errSendCommon string = "Error while sending notification on" | ||
) | ||
|
||
// WrappedSendError print a formatted string from Send Notify in case of issue | ||
func WrappedSendError(etype string, err string) { | ||
fmt.Printf("%s %s: %s", errSendCommon, etype, err) | ||
// WrappedSendError captures an error from Send Notify | ||
func WrappedSendError(err error, m *Message, backend, name string) { | ||
newLogger(err, m, backend, name). | ||
Errorf("%s %s", errSendCommon, backend) | ||
} | ||
|
||
// WrappedSendErrorWithBody captures an error with a response body from Send Notify. | ||
func WrappedSendErrorWithBody(err error, m *Message, backend, name, body string) { | ||
newLogger(err, m, backend, name). | ||
WithField("response_body", body). | ||
Errorf("%s %s", errSendCommon, backend) | ||
} | ||
|
||
// newLogger creates a logger instance with pre-filled fields. | ||
func newLogger(err error, m *Message, backend, name string) *log.Entry { | ||
var taskID string | ||
if m != nil { // avoid panic if `m` is nil | ||
taskID = m.Fields["task_id"] | ||
} | ||
|
||
return log.WithFields(log.Fields{ | ||
"notify_backend": backend, | ||
"notifier_name": name, | ||
"task_id": taskID, | ||
"notification_type": m.NotificationType, | ||
"instance_id": utask.InstanceID, | ||
}).WithError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters