Prevent runtime panic in bson.ObjectIdHex

十年热恋 提交于 2019-12-04 05:23:26

问题


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)

idk why, but if i give a wrong / invalid input string, my application got runtime panic

how i can prevent that ? thank you


回答1:


bson.ObjectIdHex() documents that it will panic if you pass an invalid object id:

ObjectIdHex returns an ObjectId from the provided hex representation. Calling this function with an invalid hex representation will cause a runtime panic. See the IsObjectIdHex function.

If you want to avoid this, first check your input string using bson.IsObjectIdHex(), and only proceed to call bson.ObjectIdHex() if your input is valid:

if bson.IsObjectIdHex(obid[0]) {
    // It's valid, calling bson.ObjectIdHex() will not panic...
}



回答2:


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  
    }
}


来源:https://stackoverflow.com/questions/53701764/prevent-runtime-panic-in-bson-objectidhex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!