externalize more panic/error strings
This commit is contained in:
parent
8b9720b70a
commit
9d791b1e3d
@ -10,6 +10,7 @@ var (
|
||||
ErrOutOfBounds = errors.New("Index(es) out of bounds!")
|
||||
ErrAppendMultipleDocuments = errors.New("Cannot append to multiple documents!")
|
||||
ErrNotSliceOrStruct = errors.New("Current object or field is not a slice nor a struct!")
|
||||
ErrUnsupportedID = errors.New("Unknown or unsupported id type provided")
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -17,7 +17,7 @@ type Counter struct {
|
||||
}
|
||||
|
||||
func getLastInColl(cname string, id interface{}) interface{} {
|
||||
var opts *options.FindOneOptions = options.FindOne()
|
||||
var opts = options.FindOne()
|
||||
|
||||
switch id.(type) {
|
||||
case int, int64, int32, uint, uint32, uint64, primitive.ObjectID:
|
||||
@ -25,7 +25,7 @@ func getLastInColl(cname string, id interface{}) interface{} {
|
||||
case string:
|
||||
opts.SetSort(bson.M{"createdAt": -1})
|
||||
default:
|
||||
panic("unsupported id type provided")
|
||||
panic(ErrUnsupportedID)
|
||||
}
|
||||
|
||||
var cnt Counter
|
||||
|
8
model.go
8
model.go
@ -59,7 +59,7 @@ func (m *Model) setTypeName(str string) {
|
||||
func (m *Model) getColl() *mongo.Collection {
|
||||
_, ri, ok := ModelRegistry.HasByName(m.typeName)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("the model '%s' has not been registered", m.typeName))
|
||||
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
||||
}
|
||||
return DB.Collection(ri.Collection)
|
||||
}
|
||||
@ -80,7 +80,7 @@ func (m *Model) getIdxs() []*mongo.IndexModel {
|
||||
func (m *Model) getParsedIdxs() map[string][]InternalIndex {
|
||||
_, ri, ok := ModelRegistry.HasByName(m.typeName)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("model '%s' not registered", m.typeName))
|
||||
panic(fmt.Sprintf(errFmtModelNotRegistered, m.typeName))
|
||||
}
|
||||
return ri.Indexes
|
||||
}
|
||||
@ -154,7 +154,9 @@ func (m *Model) FindOne(query interface{}, options ...*options.FindOneOptions) (
|
||||
rip := coll.FindOne(context.TODO(), query, options...)
|
||||
raw := bson.M{}
|
||||
err := rip.Decode(&raw)
|
||||
panik(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qqn := ModelRegistry.new_(m.typeName)
|
||||
idoc, ok := qqn.(IDocument)
|
||||
if ok {
|
||||
|
@ -305,7 +305,7 @@ func (r TModelRegistry) Model(mdl ...any) {
|
||||
}
|
||||
inds, refs, gfs, coll := parseTags(t, v)
|
||||
if coll == "" {
|
||||
panic(fmt.Sprintf("a model needs to be given a collection name! (passed type: %s)", n))
|
||||
panic(fmt.Sprintf("a Document needs to be given a collection name! (passed type: %s)", n))
|
||||
}
|
||||
ModelRegistry[n] = &Model{
|
||||
Idx: idx,
|
||||
|
2
util.go
2
util.go
@ -135,7 +135,7 @@ func incrementInterface(t interface{}) interface{} {
|
||||
case primitive.ObjectID:
|
||||
t = primitive.NewObjectID()
|
||||
default:
|
||||
panic("unknown or unsupported id type")
|
||||
panic(ErrUnsupportedID)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user