Remove custom err type

err
v2
Nikita Tokarchuk 5 years ago
parent deac951551
commit db6c11f455
  1. 9
      mongox/base/getid.go
  2. 4
      mongox/common/common.go
  3. 7
      mongox/common/count.go
  4. 14
      mongox/common/deletearray.go
  5. 8
      mongox/common/deleteone.go
  6. 10
      mongox/common/loadarray.go
  7. 10
      mongox/common/loadone.go
  8. 17
      mongox/common/loadstream.go
  9. 3
      mongox/common/saveone.go
  10. 4
      mongox/database/database.go
  11. 16
      mongox/errors/internalerror.go
  12. 16
      mongox/errors/malformed.go
  13. 16
      mongox/errors/notfound.go
  14. 5
      mongox/query/compose.go

@ -1,10 +1,11 @@
package base package base
import ( import (
"fmt"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
) )
// GetID returns source document id // GetID returns source document id
@ -18,7 +19,7 @@ func GetID(source interface{}) (id interface{}) {
case mongox.BaseObject: case mongox.BaseObject:
return getObjectOrPanic(doc) return getObjectOrPanic(doc)
default: default:
panic(errors.Malformedf("source contains malformed document, %v", source)) panic(fmt.Errorf("source contains malformed document, %v", source))
} }
} }
@ -42,7 +43,7 @@ func getStringIDOrPanic(source mongox.BaseString) (id string) {
return id return id
} }
panic(errors.Malformedf("victim contains malformed document, %v", source)) panic(fmt.Errorf("source contains malformed document, %v", source))
} }
func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) { func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) {
@ -52,5 +53,5 @@ func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) {
return id return id
} }
panic(errors.Malformedf("victim contains malformed document, %v", source)) panic(fmt.Errorf("source contains malformed document, %v", source))
} }

@ -1,6 +1,7 @@
package common package common
import ( import (
"fmt"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -10,7 +11,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -62,7 +62,7 @@ func createAggregateLoad(db mongox.Database, target interface{}, composed *query
} }
jsonTag, ok := tag.Lookup("json") jsonTag, ok := tag.Lookup("json")
if jsonTag == "-" { if jsonTag == "-" {
return nil, errors.Malformedf("preload private field is impossible") return nil, fmt.Errorf("preload private field is impossible")
} }
jsonData := strings.SplitN(jsonTag, ",", 2) jsonData := strings.SplitN(jsonTag, ",", 2)

@ -1,11 +1,12 @@
package common package common
import ( import (
"fmt"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -22,10 +23,10 @@ func Count(db mongox.Database, target interface{}, filters ...interface{}) (int6
result, err := collection.CountDocuments(db.Context(), composed.M(), opts) result, err := collection.CountDocuments(db.Context(), composed.M(), opts)
if err == mongo.ErrNoDocuments { if err == mongo.ErrNoDocuments {
return 0, errors.NotFoundErrorf("%s", err) return 0, err
} }
if err != nil { if err != nil {
return 0, errors.InternalErrorf("can't decode desult: %s", err) return 0, fmt.Errorf("can't decode desult: %w", err)
} }
return result, nil return result, nil

@ -1,6 +1,7 @@
package common package common
import ( import (
"fmt"
"reflect" "reflect"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -8,7 +9,6 @@ import (
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
) )
// DeleteArray removes documents list from a database by their ids // DeleteArray removes documents list from a database by their ids
@ -19,18 +19,18 @@ func DeleteArray(db mongox.Database, target interface{}) error {
targetK := targetV.Kind() targetK := targetV.Kind()
if targetK != reflect.Ptr { if targetK != reflect.Ptr {
panic(errors.Malformedf("target is not a ptr")) panic(fmt.Errorf("target is not a ptr"))
} }
targetSliceV := targetV.Elem() targetSliceV := targetV.Elem()
targetSliceT := targetT.Elem() targetSliceT := targetT.Elem()
if targetSliceT.Kind() != reflect.Slice { if targetSliceT.Kind() != reflect.Slice {
panic(errors.Malformedf("target should be a ptr to a slice")) panic(fmt.Errorf("target should be a ptr to a slice"))
} }
targetSliceElemT := targetSliceT.Elem() targetSliceElemT := targetSliceT.Elem()
if targetSliceElemT.Kind() != reflect.Ptr { if targetSliceElemT.Kind() != reflect.Ptr {
panic(errors.Malformedf("target slice should contain ptrs")) panic(fmt.Errorf("target slice should contain ptrs"))
} }
zeroElem := reflect.Zero(targetSliceElemT) zeroElem := reflect.Zero(targetSliceElemT)
@ -45,15 +45,15 @@ func DeleteArray(db mongox.Database, target interface{}) error {
} }
if len(ids) == 0 { if len(ids) == 0 {
return errors.Malformedf("can't delete zero elements") return fmt.Errorf("can't delete zero elements")
} }
result, err := collection.DeleteMany(db.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts) result, err := collection.DeleteMany(db.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts)
if err != nil { if err != nil {
return errors.NotFoundErrorf("can't create find and delete result: %s", err) return fmt.Errorf("can't create find and delete result: %w", err)
} }
if result.DeletedCount != int64(targetLen) { if result.DeletedCount != int64(targetLen) {
return errors.InternalErrorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen) return fmt.Errorf("can't verify delete result: removed count mismatch %d != %d", result.DeletedCount, targetLen)
} }
return nil return nil

@ -1,6 +1,7 @@
package common package common
import ( import (
"fmt"
"time" "time"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -9,7 +10,6 @@ import (
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -35,15 +35,15 @@ func DeleteOne(db mongox.Database, target interface{}, filters ...interface{}) e
result := collection.FindOneAndDelete(db.Context(), composed.M(), opts) result := collection.FindOneAndDelete(db.Context(), composed.M(), opts)
if result.Err() != nil { if result.Err() != nil {
return errors.InternalErrorf("can't create find one and delete result: %s", result.Err()) return fmt.Errorf("can't create find one and delete result: %w", result.Err())
} }
err := result.Decode(target) err := result.Decode(target)
if err == mongo.ErrNoDocuments { if err == mongo.ErrNoDocuments {
return errors.NotFoundErrorf("%s", err) return err
} }
if err != nil { if err != nil {
return errors.InternalErrorf("can't decode result: %s", err) return fmt.Errorf("can't decode result: %w", err)
} }
return nil return nil

@ -1,13 +1,13 @@
package common package common
import ( import (
"fmt"
"reflect" "reflect"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -19,18 +19,18 @@ func LoadArray(db mongox.Database, target interface{}, filters ...interface{}) e
targetK := targetV.Kind() targetK := targetV.Kind()
if targetK != reflect.Ptr { if targetK != reflect.Ptr {
panic(errors.InternalErrorf("target is not a ptr")) panic(fmt.Errorf("target is not a ptr"))
} }
targetSliceV := targetV.Elem() targetSliceV := targetV.Elem()
targetSliceT := targetT.Elem() targetSliceT := targetT.Elem()
if targetSliceT.Kind() != reflect.Slice { if targetSliceT.Kind() != reflect.Slice {
panic(errors.InternalErrorf("target should be a ptr to a slice")) panic(fmt.Errorf("target should be a ptr to a slice"))
} }
targetSliceElemT := targetSliceT.Elem() targetSliceElemT := targetSliceT.Elem()
if targetSliceElemT.Kind() != reflect.Ptr { if targetSliceElemT.Kind() != reflect.Ptr {
panic(errors.InternalErrorf("target slice should contain ptrs")) panic(fmt.Errorf("target slice should contain ptrs"))
} }
composed := query.Compose(filters...) composed := query.Compose(filters...)
@ -46,7 +46,7 @@ func LoadArray(db mongox.Database, target interface{}, filters ...interface{}) e
result, err = createSimpleLoad(db, zeroElem.Interface(), composed) result, err = createSimpleLoad(db, zeroElem.Interface(), composed)
} }
if err != nil { if err != nil {
return errors.InternalErrorf("can't create find result: %s", err) return fmt.Errorf("can't create find result: %w", err)
} }
defer result.Close(db.Context()) defer result.Close(db.Context())

@ -1,11 +1,12 @@
package common package common
import ( import (
"fmt"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -24,12 +25,15 @@ func LoadOne(db mongox.Database, target interface{}, filters ...interface{}) err
result, err = createSimpleLoad(db, target, composed) result, err = createSimpleLoad(db, target, composed)
} }
if err != nil { if err != nil {
return errors.InternalErrorf("can't create find result: %s", err) return fmt.Errorf("can't create find result: %w", err)
} }
hasNext := result.Next(db.Context()) hasNext := result.Next(db.Context())
if result.Err() != nil {
return err
}
if !hasNext { if !hasNext {
return errors.NotFoundErrorf("can't find result: %s", result.Err()) return mongo.ErrNoDocuments
} }
base.Reset(target) base.Reset(target)

@ -2,12 +2,12 @@ package common
import ( import (
"context" "context"
"fmt"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -23,15 +23,18 @@ func (l *StreamLoader) DecodeNext() error {
hasNext := l.Cursor.Next(l.ctx) hasNext := l.Cursor.Next(l.ctx)
if l.Cursor.Err() != nil {
return l.Cursor.Err()
}
if !hasNext { if !hasNext {
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments) return mongo.ErrNoDocuments
} }
base.Reset(l.target) base.Reset(l.target)
err := l.Decode(l.target) err := l.Decode(l.target)
if err != nil { if err != nil {
return errors.InternalErrorf("can't decode desult: %s", err) return fmt.Errorf("can't decode desult: %w", err)
} }
return nil return nil
@ -41,8 +44,12 @@ func (l *StreamLoader) DecodeNext() error {
func (l *StreamLoader) Next() error { func (l *StreamLoader) Next() error {
hasNext := l.Cursor.Next(l.ctx) hasNext := l.Cursor.Next(l.ctx)
if l.Cursor.Err() != nil {
return l.Cursor.Err()
}
if !hasNext { if !hasNext {
return errors.NotFoundErrorf("%s", mongo.ErrNoDocuments) return mongo.ErrNoDocuments
} }
return nil return nil
@ -69,7 +76,7 @@ func LoadStream(db mongox.Database, target interface{}, filters ...interface{})
cursor, err = createSimpleLoad(db, target, composed) cursor, err = createSimpleLoad(db, target, composed)
} }
if err != nil { if err != nil {
return nil, errors.InternalErrorf("can't create find result: %s", err) return nil, fmt.Errorf("can't create find result: %w", err)
} }
l := &StreamLoader{Cursor: cursor, ctx: db.Context(), target: target} l := &StreamLoader{Cursor: cursor, ctx: db.Context(), target: target}

@ -9,7 +9,6 @@ import (
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
"github.com/mainnika/mongox-go-driver/v2/mongox/query" "github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
@ -33,7 +32,7 @@ func SaveOne(db mongox.Database, source interface{}) error {
result := collection.FindOneAndReplace(db.Context(), composed.M(), source, opts) result := collection.FindOneAndReplace(db.Context(), composed.M(), source, opts)
if result.Err() != nil { if result.Err() != nil {
return errors.NotFoundErrorf("%s", result.Err()) return result.Err()
} }
return result.Decode(source) return result.Decode(source)

@ -2,12 +2,12 @@ package database
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"github.com/mainnika/mongox-go-driver/v2/mongox" "github.com/mainnika/mongox-go-driver/v2/mongox"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
) )
// Database handler // Database handler
@ -78,5 +78,5 @@ func (d *Database) GetCollectionOf(document interface{}) mongox.MongoCollection
return d.client.Database(d.dbname).Collection(found) return d.client.Database(d.dbname).Collection(found)
} }
panic(errors.InternalErrorf("document %v does not have a collection tag", document)) panic(fmt.Errorf("document %v does not have a collection tag", document))
} }

@ -1,16 +0,0 @@
package errors
import "fmt"
// InternalError error
type InternalError string
// Error message
func (ie InternalError) Error() string {
return fmt.Sprintf("internal error, %s", string(ie))
}
// InternalErrorf function creates an instance of InternalError
func InternalErrorf(format string, params ...interface{}) error {
return InternalError(fmt.Sprintf(format, params...))
}

@ -1,16 +0,0 @@
package errors
import "fmt"
// Malformed error
type Malformed string
// Error message
func (m Malformed) Error() string {
return fmt.Sprintf("Malformed, %s", string(m))
}
// Malformedf creates an instance of Malformed
func Malformedf(format string, params ...interface{}) error {
return Malformed(fmt.Sprintf(format, params...))
}

@ -1,16 +0,0 @@
package errors
import "fmt"
// NotFound error
type NotFound string
// Error message
func (nf NotFound) Error() string {
return fmt.Sprintf("can not find, %s", string(nf))
}
// NotFoundErrorf function creates an instance of BadRequestError
func NotFoundErrorf(format string, params ...interface{}) error {
return NotFound(fmt.Sprintf(format, params...))
}

@ -1,11 +1,12 @@
package query package query
import ( import (
"fmt"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/errors"
) )
// Compose is a function to compose filters into a single query // Compose is a function to compose filters into a single query
@ -15,7 +16,7 @@ func Compose(filters ...interface{}) *Query {
for _, f := range filters { for _, f := range filters {
if !Push(q, f) { if !Push(q, f) {
panic(errors.InternalErrorf("unknown filter %v", f)) panic(fmt.Errorf("unknown filter %v", f))
} }
} }

Loading…
Cancel
Save