diff --git a/crud.go b/crud.go index 1151e1c..66a8c63 100644 --- a/crud.go +++ b/crud.go @@ -3,6 +3,7 @@ package mgocrud import ( "fmt" "reflect" + "runtime/debug" "strings" "time" @@ -11,6 +12,19 @@ import ( "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 func CreateDocument(db *mgo.Database, m ModelInterface) error { m.PrepareInsert() @@ -116,10 +130,10 @@ func idToObjectID(filter interface{}) { } // 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() { 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)) + var _err error + if pipelineModifier != nil { // search via pipeline @@ -190,7 +206,7 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select } q := c.Pipe(pipeline).AllowDiskUse().Iter() - err = q.All(results) + _err = q.All(results) } else { // search without pipe is faster @@ -212,9 +228,12 @@ func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, select 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 }