Skip to content

Instantly share code, notes, and snippets.

@benbjohnson
Created July 7, 2014 19:28
Show Gist options
  • Select an option

  • Save benbjohnson/09062ce54be58ba9cc7f to your computer and use it in GitHub Desktop.

Select an option

Save benbjohnson/09062ce54be58ba9cc7f to your computer and use it in GitHub Desktop.
Bolt error handling

Bolt Error Handling

It's better to simply return the errors that occur from Tx.CreateBucket() and Bucket.Put() because they will rollback the transaction and the error will be returned from DB.Update(). Then you can just check the error from DB.Update().

The DB.Update() can also fail (e.g. the disk write may fail) so you should always check the return error from it.

this.DB.Update(func(tx *bolt.Tx) error {
sesionBucket, err := tx.CreateBucket([]byte(bucket))
if err != nil {
// TODO: Handle instead of panic
panic(err)
}
sesionBucket.Put([]byte(keyId), dataBytes)
return nil
})
err := this.DB.Update(func(tx *bolt.Tx) error {
sesionBucket, err := tx.CreateBucket([]byte(bucket))
if err != nil {
return err
}
return sesionBucket.Put([]byte(keyId), dataBytes)
})
if err != nil {
// TODO: Handle instead of panic
panic(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment