func ExampleFlag() { // we define our flags var ( BadInput = utils.New("BadInput", 1) NotAuthorized = utils.New("NotAuthorized", 2) InternalError = utils.New("InternalError", 3) // aggregat of flags UserError = utils.Join("UserError", BadInput, NotAuthorized) ) // error flagged type MyError struct { flag utils.Flag Msg string } // MyError with flag BadInput match := MyError{flag: BadInput, Msg: "json wanted"} // MyError with flag InternalError dontmatch := MyError{flag: InternalError, Msg: "server error"} // will print if utils.Intersect(match.flag, UserError) { fmt.Printf("user error happened '[%s] %s'\n", match.flag.String(), match.Msg) } // will not print if utils.Intersect(dontmatch.flag, UserError) { fmt.Printf("user error happened '[%s] %s'\n", dontmatch.flag.String(), dontmatch.Msg) } // Output: // user error happened '[BadInput] json wanted' }
func (u *ErrorChecker) EnableErrorChecking(element string, flags ...utils.Flag) { if element == AllError { flags1 := append(flags, u.defaultErrorFlag) u.defaultErrorFlag = utils.Join("", flags1...) for i, _ := range u.elementErrors { flags2 := append(flags, u.elementErrors[i].flag) u.elementErrors[i].flag = utils.Join("", flags2...) } return } i := u.findElement(element) if i == -1 { i = u.createNewElement(element) } flags = append(flags, u.elementErrors[i].flag) u.elementErrors[i].flag = utils.Join("", flags...) }
package utils import ( "fmt" "github.com/jloup/utils" ) var ( ErrorFlagCounter utils.Counter = 0 XMLTokenError = utils.InitFlag(&ErrorFlagCounter, "XMLTokenError") XMLSyntaxError = utils.InitFlag(&ErrorFlagCounter, "XMLSyntaxError") XMLError = utils.Join("XMLError", XMLSyntaxError, XMLTokenError) IOError = utils.InitFlag(&ErrorFlagCounter, "IOError") ) type ParserError interface { utils.ErrorFlagged } type Error struct { flag utils.Flag msg string } func NewError(f utils.Flag, msg string) ParserError { return Error{flag: f, msg: msg} } func (s Error) Error() string { return fmt.Sprintf("[%s] %s", s.FlagString(), s.msg)