Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Error handling #49

Merged
merged 10 commits into from
Aug 20, 2020
Merged

Error handling #49

merged 10 commits into from
Aug 20, 2020

Conversation

kulmann
Copy link
Member

@kulmann kulmann commented Aug 19, 2020

Adopt go-micro's style of handling errors. Cherry-picked from here

)

// 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 err
return merrors.FromError(err)
Copy link
Member Author

@kulmann kulmann Aug 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refs Thinking about it, I don't like that we're logging and building micro-errors here. Following the rule that errors should only be dealt with in one place, I'd vote for just returning the original error here without logging and then deal with logging and micro-errors in the places where the parsing and writing is used. Right now it's inconsistent and hard to follow, as we're e.g. in bundles.go sometimes returning an error an sometimes building a micro-error. Even if we have to write more code, I'd still like to remove building the errors in io.go and move it to bundles.go etc.
Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

essentially I agree with this, I thought of this while building this. Otherwise errors will be logged twice; which is good for root cause analysis, but then we could include more context to the message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should agree on a common practice. AFAICT it would make sense to wrap errors with the macro errors in the handlers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with wrapping an error's detail on handlers 👍 I'll update

@kulmann kulmann mentioned this pull request Aug 19, 2020
28 tasks
)

// 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 err
return merrors.FromError(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should agree on a common practice. AFAICT it would make sense to wrap errors with the macro errors in the handlers.

Copy link
Member Author

@kulmann kulmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't request changes, since I built the PR with cherry-picking 😅 there are two places where you forgot to change back to returning the plain error instead of a new micro error.
Moving the merrors to the handlers feels much better now. Thanks for the changes! 😍

@@ -5,6 +5,7 @@ import (

validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
merrors "github.com/micro/go-micro/v2/errors"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import should not be required in here anymore

}

// validateResource is an internal helper for validating the content of a resource.
func validateResource(resource *proto.Resource) error {
if err := validation.Validate(&resource, validation.Required); err != nil {
return merrors.FromError(err)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with return err

@@ -23,7 +23,7 @@ func (s Store) ListBundles(bundleType proto.Bundle_Type) ([]*proto.Bundle, error
bundlesFolder := s.buildFolderPathForBundles(false)
bundleFiles, err := ioutil.ReadDir(bundlesFolder)
if err != nil {
return []*proto.Bundle{}, nil
return []*proto.Bundle{}, merrors.FromError(err)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with return []*proto.Bundle{}, err

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd restrain from returning an error here, as callers would check for it. Either return an empty bundle or an error, not both. I'd leave it as it was.

@kulmann kulmann merged commit adcecd9 into master Aug 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants