mgo icon indicating copy to clipboard operation
mgo copied to clipboard

Panic With ObjectIdHex(s string)

Open zjzjzjzj1874 opened this issue 2 years ago • 0 comments

It is very danger with mgo/bson/bson.go.212,see code; if anyone else who wanted to change request args to mongoDB objectID, the code maybe like this:

objID := bson.ID
if IsObjectIdHex(req.ObjectID) {
    objID = ObjectIdHex(req.ObjectID)
}else {
    // TODO do else logic
}

And Why not add like this:

if objID,err := MaybeObjectIdHex(req.ObjectID);err != nil {
    // TODO add return logic
    return
}

    // TODO do correct logic

Any way, panic in runtime is very unfriendly for every programmer.

zjzjzjzj1874 avatar May 11 '22 10:05 zjzjzjzj1874