如何使用Couchbase Gocb检查错误代码?

处理 gocb (Couchbase Go官方客户端),我想检查特定的状态代码(例如 StatusKeyNotFound </ code>或 StatusKeyExists </ code>)。</ p>

像这样</ p>

  _,err:= bucket.Get(键,实体)
if err!= nil {
如果err == gocb .ErrKeyNotFound {
...
}
}
</ code> </ pre>

可以做到吗?</ p>
</ div>

展开原文

原文

When handling errors returned by gocb (the official Couchbase Go Client) I would like to check for specific status codes (e.g. StatusKeyNotFound or StatusKeyExists).

Like so

_, err := bucket.Get(key, entity)
if err != nil {
    if err == gocb.ErrKeyNotFound {
        ...
    }
}

Can this be done?

1个回答




可以这样做吗?</ p>
</ blockquote>

gocbcore </ code>,error.go,我们有 以下定义:</ p>

 类型memdError结构{
代码StatusCode
}

// ...

func(e memdError)KeyNotFound ()bool {
返回e.code == StatusKeyNotFound
}
func(e memdError)KeyExists()bool {
return e.code == StatusKeyExists
}
func(e memdError)Temporary()bool {

返回e.code == StatusOutOfMemory || e.code == StatusTmpFail
}
func(e memdError)AuthError()bool {
返回e.code == StatusAuthError
}
func(e memdError)ValueTooBig()bool {
返回e.code = = StatusTooBig
}
func(e memdError)NotStored()bool {
return e.code == StatusNotStored
}
func(e memdError)BadDelta()bool {
return e.code == StatusBadDelta
}
</ code> </ pre>

请注意,未导出 memdError </ code>,因此不能在类型断言中使用它。</ p>
\ n

因此,采用另一种方法:定义自己的接口:</ p>

  type MyErrorInterface接口{
KeyNotFound()bool
KeyExists()bool
} \ n </ code> </ pre>

并声明从 gocb </ code>返回到界面的任何错误:</ p>

   _,err:= bucket.Get(键,实体)

if err!= nil {
if se,ok = err。(MyErrorInterface); ok {
if se.KeyNotFound(){
//处理KeyNotFound
}
if se.KeyExists(){
//处理KeyExists
}
} else {
//没有相关信息 状态
}
}
</ code> </ pre>
</ div>

展开原文

原文

Can this be done?

In gocbcore, error.go we have the 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 that memdError is unexported, so you can't use it in a type assertion.

So, take a different approach: define your own interface:

type MyErrorInterface interface {
    KeyNotFound() bool
    KeyExists() bool
}

And assert whatever error you get back from gocb to your 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 about states
    }
}

doujiu6976
doujiu6976 好招! 非常感谢。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问