made it so that the Route() methods, of the various routers, will return an error if the receiver is nil. (except for the discard router.)

master
Charles Iliya Krempeaux 2016-09-20 11:03:35 -07:00
parent 34b5f0d3fe
commit 4afa358cf7
7 changed files with 34 additions and 0 deletions

View File

@ -27,6 +27,10 @@ type CopyingRouter struct {
func (router *CopyingRouter) Route(message string, context map[string]interface{}) error { func (router *CopyingRouter) Route(message string, context map[string]interface{}) error {
if nil == router {
return errNilReceiver
}
copy := struct{Message string ; Context map[string]interface{}}{ copy := struct{Message string ; Context map[string]interface{}}{
Message: message, Message: message,
Context: context, Context: context,

11
errors.go 100644
View File

@ -0,0 +1,11 @@
package flog
import (
"errors"
)
var (
errNilReceiver = errors.New("Nil Receiver")
)

View File

@ -24,6 +24,10 @@ type FanoutRouter struct {
func (router *FanoutRouter) Route(message string, context map[string]interface{}) error { func (router *FanoutRouter) Route(message string, context map[string]interface{}) error {
if nil == router {
return errNilReceiver
}
errors := []error{} errors := []error{}
for _, subrouter := range router.subrouters { for _, subrouter := range router.subrouters {

View File

@ -74,6 +74,10 @@ type FilteringRouter struct {
func (router *FilteringRouter) Route(message string, context map[string]interface{}) error { func (router *FilteringRouter) Route(message string, context map[string]interface{}) error {
if nil == router {
return errNilReceiver
}
if router.filterFn(message, context) { if router.filterFn(message, context) {
return router.subrouter.Route(message, context) return router.subrouter.Route(message, context)
} }

View File

@ -24,5 +24,9 @@ type MappingRouter struct {
func (router *MappingRouter) Route(message string, context map[string]interface{}) error { func (router *MappingRouter) Route(message string, context map[string]interface{}) error {
if nil == router {
return errNilReceiver
}
return router.subrouter.Route(router.fn(message, context)) return router.subrouter.Route(router.fn(message, context))
} }

View File

@ -22,6 +22,10 @@ type NonBlockingRouter struct {
func (router *NonBlockingRouter) Route(message string, context map[string]interface{}) error { func (router *NonBlockingRouter) Route(message string, context map[string]interface{}) error {
if nil == router {
return errNilReceiver
}
go func() { go func() {
router.subrouter.Route(message, context) router.subrouter.Route(message, context)
}() }()

View File

@ -34,6 +34,9 @@ type PrettyWritingRouter struct {
func (router *PrettyWritingRouter) Route(message string, context map[string]interface{}) error { func (router *PrettyWritingRouter) Route(message string, context map[string]interface{}) error {
if nil == router {
return errNilReceiver
}
const STYLE_FATAL = "\x1b[40;33;1m" // BG BLACK, FG YELLOW, BOLD const STYLE_FATAL = "\x1b[40;33;1m" // BG BLACK, FG YELLOW, BOLD
const STYLE_PANIC = "\x1b[40;31;1m" // BG BLACK, FG RED, BOLD const STYLE_PANIC = "\x1b[40;31;1m" // BG BLACK, FG RED, BOLD