Properly handle Gob panic

This commit is contained in:
Patrick Mylund Nielsen 2012-02-19 01:21:07 +01:00
parent 91bd4334f1
commit 8805e79189
1 changed files with 2 additions and 3 deletions

View File

@ -199,10 +199,9 @@ func (c *cache) DeleteExpired() {
}
// Writes the cache's items (using Gob) to an io.Writer.
func (c *cache) Save(w io.Writer) error {
func (c *cache) Save(w io.Writer) (err error) {
enc := gob.NewEncoder(w)
var err error
defer func() {
if x := recover(); x != nil {
fmt.Printf(`The Gob library paniced while registering the cache's item types!
@ -219,7 +218,7 @@ were stored in cache, at https://github.com/pmylund/go-cache/issues/new
gob.Register(v.Object)
}
err = enc.Encode(&c.Items)
return err
return
}
// Saves the cache's items to the given filename, creating the file if it