Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/error management #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion 07-behaviour_error_handling/internal/beer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package beerscli

import (
"encoding/json"

"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/errors"
)

// Beer representation of beer into data struct
Expand Down Expand Up @@ -65,9 +67,11 @@ var toID = map[string]BeerType{
func (t *BeerType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)

if err != nil {
return err
return errors.WrapConversionError(err, "Could not convert retrieved bytes to JSON")
}

*t = toID[j]
return nil
}
Expand Down
12 changes: 11 additions & 1 deletion 07-behaviour_error_handling/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ func runBeersFn(repository beerscli.BeerRepo) CobraFn {
log.Fatal(err)
}

if errors.IsFileHandlingError(err) {
log.Fatal(err)
}

id, _ := cmd.Flags().GetString(idFlag)

if id != "" {
i, _ := strconv.Atoi(id)
var i int
i, err = strconv.Atoi(id)

if errors.IsConversionError(err) {
log.Fatal(err)
}

for _, beer := range beers {
if beer.ProductID == i {
fmt.Println(beer)
Expand Down
28 changes: 28 additions & 0 deletions 07-behaviour_error_handling/internal/errors/conversionerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package errors

import (
"github.com/pkg/errors"
)

type conversionError struct {
error
}

// WrapConversionError returns an error which wraps err that satisfies
// IsConversionError()
func WrapConversionError(err error, format string, args ...interface{}) error {
return &conversionError{errors.Wrapf(err, format, args...)}
}

// NewConversionError returns an error which satisfies IsConversionError()
func NewConversionError(format string, args ...interface{}) error {
return &conversionError{errors.Errorf(format, args...)}
}

// IsConversionError reports whether err was created with ConversionError() or
// NewConversionError()
func IsConversionError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*conversionError)
return ok
}
28 changes: 28 additions & 0 deletions 07-behaviour_error_handling/internal/errors/filehandlingerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package errors

import (
"github.com/pkg/errors"
)

type fileHandlingError struct {
error
}

// WrapFileHandlingError returns an error which wraps err that satisfies
// IsFileHandlingError()
func WrapFileHandlingError(err error, format string, args ...interface{}) error {
return &fileHandlingError{errors.Wrapf(err, format, args...)}
}

// NewFileHandlingError returns an error which satisfies IsFileHandlingError()
func NewFileHandlingError(format string, args ...interface{}) error {
return &fileHandlingError{errors.Errorf(format, args...)}
}

// IsFileHandlingError reports whether err was created with FileHandlingError() or
// NewFileHandlingError()
func IsFileHandlingError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*fileHandlingError)
return ok
}
31 changes: 26 additions & 5 deletions 07-behaviour_error_handling/internal/storage/csv/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

beerscli "github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal"
"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/errors"
)

type repository struct {
Expand All @@ -19,15 +20,31 @@ func NewRepository() beerscli.BeerRepo {

// GetBeers fetch beers data from csv
func (r *repository) GetBeers() ([]beerscli.Beer, error) {
f, _ := os.Open("07-behaviour_error_handling/data/beers.csv")
const filePath = "07-behaviour_error_handling/data/beers.csv"
f, err := os.Open(filePath)

if err != nil {
return nil, errors.WrapFileHandlingError(err, "error opening file %s", filePath)
}

reader := bufio.NewReader(f)

var beers []beerscli.Beer

for line := readLine(reader); line != nil; line = readLine(reader) {
var line []byte

for line, err = readLine(reader); line != nil; line, err = readLine(reader) {
if err != nil {
return nil, err
}
values := strings.Split(string(line), ",")

productID, _ := strconv.Atoi(values[0])
var productID int
productID, err = strconv.Atoi(values[0])

if err != nil {
return nil, errors.WrapConversionError(err, "error while converting 'productId' from ascii to integer: %s", values[0])
}

beer := beerscli.NewBeer(
productID,
Expand All @@ -45,7 +62,11 @@ func (r *repository) GetBeers() ([]beerscli.Beer, error) {
return beers, nil
}

func readLine(reader *bufio.Reader) (line []byte) {
line, _, _ = reader.ReadLine()
func readLine(reader *bufio.Reader) (line []byte, err error) {
line, _, err = reader.ReadLine()

if err != nil {
return nil, errors.WrapFileHandlingError(err, "could not read next line of buffer")
}
return
}