Prevent runtime panic in bson.ObjectIdHex

前端 未结 2 1890
囚心锁ツ
囚心锁ツ 2021-01-23 09:47

i\'m trying to convert string of objectid to bson ObjectId format with mgo,

errCheck := d.C(\"col\").FindId(bson.ObjectIdHex(obid[0])).One(&Result)

2条回答
  •  粉色の甜心
    2021-01-23 10:34

    As @icza said in the last answer. you should check validity if ObjectId. And you can use panic recover defer to handle any kind of error in future

    package main
    
    import (
        "fmt"
        "gopkg.in/mgo.v2/bson"
        "path/filepath"
        "runtime"
        "strings"
    )
    
    func main() {
        r := Result{}
        getData(&r)
    }
    
    func IdentifyPanic() string {
        var name, file string
        var line int
        var pc [16]uintptr
    
        n := runtime.Callers(3, pc[:])
        for _, pc := range pc[:n] {
            fn := runtime.FuncForPC(pc)
            if fn == nil {
                continue
            }
            file, line = fn.FileLine(pc)
            name = fn.Name()
    
            if !strings.HasPrefix(name, "runtime.") {
                break
            }
        }
        file = filepath.Base(file)
    
        switch {
        case name != "":
            return fmt.Sprintf("%v:%v", file, line)
        case file != "":
            return fmt.Sprintf("%v:%v", file, line)
        }
    
        return fmt.Sprintf("pc:%x", pc)
    }
    
    type Result struct {
        success int
        data string
    }
    func getData(result *Result){
        defer func() {
            if err := recover(); err != nil {
                ip := IdentifyPanic()
                errorMessage := fmt.Sprintf("%s Error: %s", ip, err)
                fmt.Println(errorMessage)
                result.success = 0
            }
        }()
        if bson.IsObjectIdHex(obid[0]) {                                 // this line copied from @icza answer
            // It's valid, calling bson.ObjectIdHex() will not panic...  // this line copied from @icza answer
            errCheck := d.C("col").FindId(bson.ObjectIdHex(obid[0])).One(&res)
            result.success = 1
            result.data = "your result (res). this is just the exam"
        }else{
            result.success = 0  
        }
    }
    

提交回复
热议问题