added ErrorWithStack
This commit is contained in:
parent
1fee9fe349
commit
ef51f2446b
27
crud.go
27
crud.go
@ -3,6 +3,7 @@ package mgocrud
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -11,6 +12,19 @@ import (
|
|||||||
"gopkg.in/mgo.v2/bson"
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ErrorWithStack struct {
|
||||||
|
Message string
|
||||||
|
StackTrace string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ErrorWithStack) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ErrorWithStack) Stack() string {
|
||||||
|
return e.StackTrace
|
||||||
|
}
|
||||||
|
|
||||||
// CreateDocument creates a document from specified model
|
// CreateDocument creates a document from specified model
|
||||||
func CreateDocument(db *mgo.Database, m ModelInterface) error {
|
func CreateDocument(db *mgo.Database, m ModelInterface) error {
|
||||||
m.PrepareInsert()
|
m.PrepareInsert()
|
||||||
@ -116,10 +130,10 @@ func idToObjectID(filter interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadCollection gets the filtered collection of the model
|
// ReadCollection gets the filtered collection of the model
|
||||||
func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) {
|
func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err *ErrorWithStack) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
err = fmt.Errorf("%v", r)
|
err = &ErrorWithStack{Message: fmt.Sprintf("%v", r), StackTrace: string(debug.Stack())}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -142,6 +156,8 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select
|
|||||||
|
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
|
|
||||||
|
var _err error
|
||||||
|
|
||||||
if pipelineModifier != nil {
|
if pipelineModifier != nil {
|
||||||
// search via pipeline
|
// search via pipeline
|
||||||
|
|
||||||
@ -190,7 +206,7 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select
|
|||||||
}
|
}
|
||||||
|
|
||||||
q := c.Pipe(pipeline).AllowDiskUse().Iter()
|
q := c.Pipe(pipeline).AllowDiskUse().Iter()
|
||||||
err = q.All(results)
|
_err = q.All(results)
|
||||||
} else {
|
} else {
|
||||||
// search without pipe is faster
|
// search without pipe is faster
|
||||||
|
|
||||||
@ -212,9 +228,12 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select
|
|||||||
q = q.Limit(limit)
|
q = q.Limit(limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = q.All(results)
|
_err = q.All(results)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _err != nil {
|
||||||
|
return &ErrorWithStack{Message: _err.Error(), StackTrace: string(debug.Stack())}
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user