encapsulate ErrNotFound
This commit is contained in:
parent
115c288a0b
commit
7dd238342c
23
session.go
23
session.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user