encapsulate ErrNotFound

This commit is contained in:
Sebastian Frank 2022-02-09 15:53:37 +01:00
parent 115c288a0b
commit 7dd238342c
Signed by: apairon
GPG Key ID: A0E05A8199CE3F57

View File

@ -6,6 +6,10 @@ import (
mgo "gopkg.in/mgo.v2"
)
var (
ErrNotFound = errors.New("not found")
)
type Session struct {
session *mgo.Session
}
@ -111,7 +115,7 @@ func (q *Query) Select(selector interface{}) *Query {
func (q *Query) One(result interface{}) error {
err := q.query.One(result)
if err == mgo.ErrNotFound {
err = &ErrNotFound{msg: err.Error()}
err = ErrNotFound
}
return err
}
@ -134,7 +138,7 @@ func (q *Query) Limit(n int) *Query {
func (q *Query) All(result interface{}) error {
err := q.query.All(result)
if err == mgo.ErrNotFound {
err = &ErrNotFound{msg: err.Error()}
err = ErrNotFound
}
return err
}
@ -142,7 +146,7 @@ func (q *Query) All(result interface{}) error {
func (q *Query) Count() (int, error) {
c, err := q.query.Count()
if err == mgo.ErrNotFound {
err = &ErrNotFound{msg: err.Error()}
err = ErrNotFound
}
return c, err
}
@ -169,7 +173,7 @@ type Pipe struct {
func (p *Pipe) All(result interface{}) error {
err := p.pipe.All(result)
if err == mgo.ErrNotFound {
err = &ErrNotFound{msg: err.Error()}
err = ErrNotFound
}
return err
}
@ -177,14 +181,3 @@ func (p *Pipe) All(result interface{}) error {
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
return &Pipe{pipe: c.collection.Pipe(pipeline).AllowDiskUse()}
}
type ErrNotFound struct {
msg string
}
func (e *ErrNotFound) Error() string {
if e.msg == "" {
return "not found"
}
return e.msg
}