Skip to content

Commit

Permalink
feat: 增加IsNotFound()方法简化错误判断
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyi committed Nov 22, 2024
1 parent 65d4c26 commit d3d0bca
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package entity
import (
"context"
"database/sql"
"errors"
"fmt"
"math"

Expand Down Expand Up @@ -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)
}

0 comments on commit d3d0bca

Please sign in to comment.