How to check if a file is a valid boltdb database without panicing
korjavin opened this issue · 2 comments
korjavin commented
d1 := []byte("hello\n")
ioutil.WriteFile(filename, d1, 0644)
dbconn, err := bolt.Open(filename, os.ModePerm, &bolt.Options{Timeout: 1 * time.Second})
defer dbconn.Close()
This one will panic, but I would like to have just an err like "wrong format"
xyproto commented
You can recover from the panic with recover()
, like this:
func canOpenDB(filename string) (retval bool) {
defer func() {
retval = nil == recover()
}()
dbconn, err := bolt.Open(filename, os.ModePerm, &bolt.Options{Timeout: 1 * time.Second})
defer dbconn.Close()
return nil == err
}
korjavin commented
It may work, thank you!