-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kezi
committed
Dec 12, 2021
0 parents
commit 1056bfd
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
1056bfd
There was a problem hiding this comment.
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