Skip to content

Commit

Permalink
task commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kezi committed Dec 12, 2021
0 parents commit 1056bfd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions FirstTask/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package FirstTask

import (
"database/sql"
"fmt"
"github.com/pkg/errors"
)

func main() {
err := Call()
if errors.Cause(err) == sql.ErrNoRows {
fmt.Printf("data not found,%v\n", err)
fmt.Printf("%+v\n", err)
return
}
}

func getSql() error {
return errors.Wrap(sql.ErrNoRows, "getSql failed")
}
func Call() error {
return errors.WithMessage(getSql(), "call failed")
}

1 comment on commit 1056bfd

@LeoLi-Goat
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

方案可以,只是设计上不是最好的,将sql.ErrNoRows直接返回给上层意味着上层也要使用sql.ErrNoRows来判空,这样后面低层升级换掉sql时,上层也被影响,这样设计增加了上层代码和第三方库的依赖,好的做法是上层和下层解耦,下层使用自定义错误来封装第三方错误,见推荐做法:https://github.com/flycash/geekbang-go-camp/blob/main/homework/second/not_found.go

Please sign in to comment.