errorx icon indicating copy to clipboard operation
errorx copied to clipboard

[feature] Add service error support!

Open fwhezfwhez opened this issue 5 years ago • 0 comments

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
    }
}

fwhezfwhez avatar Oct 30 '20 09:10 fwhezfwhez