go - How check error codes with couchbase gocb? -
when handling errors returned gocb (the official couchbase go client) check specific status codes (e.g. statuskeynotfound or statuskeyexists).
like so
_, err := bucket.get(key, entity) if err != nil { if err == gocb.errkeynotfound { ... } } can done?
can done?
in gocbcore, error.go have following definition:
type memderror struct { code statuscode } // ... func (e memderror) keynotfound() bool { return e.code == statuskeynotfound } func (e memderror) keyexists() bool { return e.code == statuskeyexists } func (e memderror) temporary() bool { return e.code == statusoutofmemory || e.code == statustmpfail } func (e memderror) autherror() bool { return e.code == statusautherror } func (e memderror) valuetoobig() bool { return e.code == statustoobig } func (e memderror) notstored() bool { return e.code == statusnotstored } func (e memderror) baddelta() bool { return e.code == statusbaddelta } note memderror unexported, can't use in type assertion.
so, take different approach: define own interface:
type myerrorinterface interface { keynotfound() bool keyexists() bool } and assert whatever error gocb interface:
_, err := bucket.get(key, entity) if err != nil { if se, ok = err.(myerrorinterface); ok { if se.keynotfound() { // handle keynotfound } if se.keyexists() { // handle keyexists } } else { // no information states } }
Comments
Post a Comment