Add error checking

This commit is contained in:
Alexander Neumann 2015-11-18 20:33:20 +01:00
parent 2c95772a6a
commit 5b601f00b1
4 changed files with 25 additions and 15 deletions

View File

@ -556,7 +556,7 @@ func DecodeOldIndex(rd io.Reader) (idx *Index, err error) {
} }
// LoadIndexWithDecoder loads the index and decodes it with fn. // LoadIndexWithDecoder loads the index and decodes it with fn.
func LoadIndexWithDecoder(repo *Repository, id string, fn func(io.Reader) (*Index, error)) (*Index, error) { func LoadIndexWithDecoder(repo *Repository, id string, fn func(io.Reader) (*Index, error)) (idx *Index, err error) {
debug.Log("LoadIndexWithDecoder", "Loading index %v", id[:8]) debug.Log("LoadIndexWithDecoder", "Loading index %v", id[:8])
idxID, err := backend.ParseID(id) idxID, err := backend.ParseID(id)
@ -568,9 +568,9 @@ func LoadIndexWithDecoder(repo *Repository, id string, fn func(io.Reader) (*Inde
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rd.Close() defer closeOrErr(rd, &err)
idx, err := fn(rd) idx, err = fn(rd)
if err != nil { if err != nil {
debug.Log("LoadIndexWithDecoder", "error while decoding index %v: %v", id, err) debug.Log("LoadIndexWithDecoder", "error while decoding index %v: %v", id, err)
return nil, err return nil, err

View File

@ -129,7 +129,7 @@ func TestIndexSerialize(t *testing.T) {
"index not final after encoding") "index not final after encoding")
id := randomID() id := randomID()
idx.SetID(id) OK(t, idx.SetID(id))
id2, err := idx.ID() id2, err := idx.ID()
Assert(t, id2.Equal(id), Assert(t, id2.Equal(id),
"wrong ID returned: want %v, got %v", id, id2) "wrong ID returned: want %v, got %v", id, id2)

View File

@ -112,23 +112,23 @@ func SearchKey(s *Repository, password string) (*Key, error) {
} }
// LoadKey loads a key from the backend. // LoadKey loads a key from the backend.
func LoadKey(s *Repository, name string) (*Key, error) { func LoadKey(s *Repository, name string) (k *Key, err error) {
// extract data from repo // extract data from repo
rd, err := s.be.Get(backend.Key, name) rd, err := s.be.Get(backend.Key, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rd.Close() defer closeOrErr(rd, &err)
// restore json // restore json
dec := json.NewDecoder(rd) dec := json.NewDecoder(rd)
k := Key{} k = new(Key)
err = dec.Decode(&k) err = dec.Decode(k)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &k, nil return k, nil
} }
// AddKey adds a new key to an already existing repository. // AddKey adds a new key to an already existing repository.

View File

@ -143,19 +143,29 @@ func (r *Repository) LoadBlob(t pack.BlobType, id backend.ID, plaintextBuf []byt
return plaintextBuf, nil return plaintextBuf, nil
} }
// closeOrErr calls cl.Close() and sets err to the returned error value if
// itself is not yet set.
func closeOrErr(cl io.Closer, err *error) {
e := cl.Close()
if *err != nil {
return
}
*err = e
}
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on // LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
// the item. // the item.
func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) error { func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interface{}) (err error) {
// load blob from backend // load blob from backend
rd, err := r.be.Get(t, id.String()) rd, err := r.be.Get(t, id.String())
if err != nil { if err != nil {
return err return err
} }
defer rd.Close() defer closeOrErr(rd, &err)
// decrypt // decrypt
decryptRd, err := crypto.DecryptFrom(r.key, rd) decryptRd, err := crypto.DecryptFrom(r.key, rd)
defer decryptRd.Close() defer closeOrErr(decryptRd, &err)
if err != nil { if err != nil {
return err return err
} }
@ -172,7 +182,7 @@ func (r *Repository) LoadJSONUnpacked(t backend.Type, id backend.ID, item interf
// LoadJSONPack calls LoadBlob() to load a blob from the backend, decrypt the // LoadJSONPack calls LoadBlob() to load a blob from the backend, decrypt the
// data and afterwards call json.Unmarshal on the item. // data and afterwards call json.Unmarshal on the item.
func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface{}) error { func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface{}) (err error) {
// lookup pack // lookup pack
blob, err := r.idx.Lookup(id) blob, err := r.idx.Lookup(id)
if err != nil { if err != nil {
@ -184,11 +194,11 @@ func (r *Repository) LoadJSONPack(t pack.BlobType, id backend.ID, item interface
if err != nil { if err != nil {
return err return err
} }
defer rd.Close() defer closeOrErr(rd, &err)
// decrypt // decrypt
decryptRd, err := crypto.DecryptFrom(r.key, rd) decryptRd, err := crypto.DecryptFrom(r.key, rd)
defer decryptRd.Close() defer closeOrErr(decryptRd, &err)
if err != nil { if err != nil {
return err return err
} }