防止 bson.ObjectIdHex 中的运行时恐慌

Prevent runtime panic in bson.ObjectIdHex

我正在尝试使用 mgo 将 objectid 字符串转换为 bson ObjectId 格式,

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

我不知道为什么,但如果我输入错误/无效的输入字符串,我的应用程序就会出现运行时恐慌

我该如何预防?谢谢

bson.ObjectIdHex() 记录如果你传递一个无效的对象 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.

如果您想避免这种情况,请首先使用 bson.IsObjectIdHex() 检查您的输入字符串,只有在您的输入有效时才继续调用 bson.ObjectIdHex()

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

正如@icza 在上一个回答中所说的那样。如果是 ObjectId,你应该检查有效性。 您可以使用 panic recover defer 来处理将来的任何类型的错误

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