From 9d791b1e3d7708d301d88825a7dd2d2792d64a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 14 Sep 2024 15:20:17 -0400 Subject: [PATCH] externalize more panic/error strings --- errors.go | 1 + idcounter.go | 4 ++-- model.go | 8 +++++--- registry.go | 2 +- util.go | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/errors.go b/errors.go index b9f67eb..6b6f86b 100644 --- a/errors.go +++ b/errors.go @@ -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 ( diff --git a/idcounter.go b/idcounter.go index ca2ab18..6cef9f1 100644 --- a/idcounter.go +++ b/idcounter.go @@ -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 diff --git a/model.go b/model.go index 0616b45..e72258b 100644 --- a/model.go +++ b/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 { diff --git a/registry.go b/registry.go index 73bdf47..b38210c 100644 --- a/registry.go +++ b/registry.go @@ -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, diff --git a/util.go b/util.go index b715b5f..c7ea36c 100644 --- a/util.go +++ b/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 }