gRPC的Go实现,其返回的error为什么不用fmt.Errorf()?
比如你要返回一个gorm.ErrRecordNotFound,那么gRPC的调用者最终收到的error的信息会是这样:
rpc error: code = Unknown desc =
调用者想这么判断error:
if errors.Is(err, gorm.ErrRecordNotFound)
会返回false
用errors.As也不行
errors.As(err, &gorm.ErrRecordNotFound)
在运行测试的时候会报错:second argument to errors.As should not be *error
GPT的解释:The error message you're seeing is because errors.As expects a pointer to an interface as its second argument. However, gorm.ErrRecordNotFound is a value of type error, not an interface type.
最后更新于