errorx
errorx copied to clipboard
[feature] Add service error support!
Hi, errorx now has a new convenient usage about servicer error.
In some cases, clients want to show 'balance not enough', 'password is wrong' this kind of error messages. Apparrently it shouldn't be regarded as an error which deserves logging, and don't want users to see your error stack.
Now errorx provides good handle style:
var PasswordWrongErr = errorx.NewServiceError("password is wrong", 1001)
func login() error {
return PasswordWrongErr
}
func main() {
// where to handle service error
if se, ok:=errorx.IsServiceErr(e, loginService.PasswordWrongErr);ok {
fmt.Println("reply to client: ", fmt.Printf("errcode: %d errmsg:%s", se.Errcode, se.Errmsg))
return
}
if e!=nil {
fmt.Println("handler error with its stacktrace:", errorx.Wrap(e).Error())
return
}
}