use micro errors on handlers

This commit is contained in:
A.Unger
2020-08-20 11:25:39 +02:00
parent e510229bc1
commit c50399fb1d
3 changed files with 48 additions and 52 deletions
+3 -8
View File
@@ -5,21 +5,18 @@ import (
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
merrors "github.com/micro/go-micro/v2/errors"
)
// Unmarshal file into record
func (s Store) parseRecordFromFile(record proto.Message, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
s.Logger.Err(err).Msgf("error reading file %v: file not found", filePath)
return merrors.FromError(err)
return err
}
defer file.Close()
decoder := jsonpb.Unmarshaler{}
if err = decoder.Unmarshal(file, record); err != nil {
s.Logger.Err(err).Msgf("error reading file %v: unmarshalling failed", filePath)
return err
}
return nil
@@ -29,15 +26,13 @@ func (s Store) parseRecordFromFile(record proto.Message, filePath string) error
func (s Store) writeRecordToFile(record proto.Message, filePath string) error {
file, err := os.Create(filePath)
if err != nil {
s.Logger.Err(err).Msgf("error writing file %v: opening failed", filePath)
return merrors.FromError(err)
return err
}
defer file.Close()
encoder := jsonpb.Marshaler{}
if err = encoder.Marshal(file, record); err != nil {
s.Logger.Err(err).Msgf("error writing file %v: marshalling failed", filePath)
return merrors.FromError(err)
return err
}
return nil