From d3d0bca0d66df6d56a0e5a6591dd43fb31bc4c55 Mon Sep 17 00:00:00 2001 From: yangyi Date: Fri, 22 Nov 2024 17:24:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0IsNotFound()=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=AE=80=E5=8C=96=E9=94=99=E8=AF=AF=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/helper.go b/helper.go index e4e2d16..10cef86 100644 --- a/helper.go +++ b/helper.go @@ -3,6 +3,7 @@ package entity import ( "context" "database/sql" + "errors" "fmt" "math" @@ -221,3 +222,13 @@ func (p Pagination) Offset() int { func (p Pagination) UOffset() uint { return uint(p.Offset()) } + +// IsNotFound 判断是否是未找到错误 +// +// repository在没有找到记录时返回ErrNotFound错误 +// GetRecord()在没有找到记录时返回sql.ErrNoRows错误 +// 使用这个方法来统一处理错误判断 +func IsNotFound(err error) bool { + return errors.Is(err, sql.ErrNoRows) || + errors.Is(err, ErrNotFound) +}