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 #43

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
15 changes: 3 additions & 12 deletions 07-behaviour_error_handling/cmd/beers-cli/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package main

import (
"flag"

"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/storage/ontario"

beerscli "github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal"
"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/cli"
"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/storage/csv"
"github.com/spf13/cobra"
)

func main() {

csvData := flag.Bool("csv", false, "load data from csv")
flag.Parse()
var repo beerscli.BeerRepo
repo = csv.NewRepository()

if !*csvData {
repo = ontario.NewOntarioRepository()
}
repoAPI := csv.NewRepository()
repoCSV := ontario.NewOntarioRepository()

rootCmd := &cobra.Command{Use: "beers-cli"}
rootCmd.AddCommand(cli.InitBeersCmd(repo))
rootCmd.AddCommand(cli.InitBeersCmd(repoAPI, repoCSV))
rootCmd.Execute()
}
20 changes: 17 additions & 3 deletions 07-behaviour_error_handling/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,36 @@ type CobraFn func(cmd *cobra.Command, args []string)
const idFlag = "id"

// InitBeersCmd initialize beers command
func InitBeersCmd(repository beerscli.BeerRepo) *cobra.Command {
func InitBeersCmd(repoAPI beerscli.BeerRepo, repoCSV beerscli.BeerRepo) *cobra.Command {
beersCmd := &cobra.Command{
Use: "beers",
Short: "Print data about beers",
Run: runBeersFn(repository),
Run: runBeersFn(repoAPI, repoCSV),
}

beersCmd.Flags().StringP(idFlag, "i", "", "id of the beer")
beersCmd.Flags().Bool("csv", false, "load data from csv")

return beersCmd
}

func runBeersFn(repository beerscli.BeerRepo) CobraFn {
func runBeersFn(repoAPI beerscli.BeerRepo, repoCSV beerscli.BeerRepo) CobraFn {
return func(cmd *cobra.Command, args []string) {
var repository beerscli.BeerRepo
useCSV, _ := cmd.Flags().GetBool("csv")
if useCSV {
repository = repoAPI
} else {
repository = repoCSV
}
beers, err := repository.GetBeers()
if errors.IsDataUnreacheable(err) {
log.Printf("https://github.com/caleeli/golang-examples.git/issues/new?assignees=&labels=&template=dataunreachable.md&title=")
}
if errors.IsLoadCSVError(err) {
log.Printf("https://github.com/caleeli/golang-examples.git/issues/new?assignees=&labels=&template=loadcsv.md&title=")
}
if err != nil {
log.Fatal(err)
}

Expand Down
28 changes: 28 additions & 0 deletions 07-behaviour_error_handling/internal/errors/load_csv_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package errors

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

type loadCSVError struct {
error
}

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

// NewLoadCSVError returns an error which satisfies IsLoadCSVError()
func NewLoadCSVError(format string, args ...interface{}) error {
return &loadCSVError{errors.Errorf(format, args...)}
}

// IsLoadCSVError reports whether err was created with LoadCSVErrorf() or
// NewLoadCSVError()
func IsLoadCSVError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*loadCSVError)
return ok
}
23 changes: 18 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,27 @@ 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")
file := "07-behaviour_error_handling/data/beers.csv"
f, err := os.Open(file)
if err != nil {
return nil, errors.WrapLoadCSVError(err, "Unable to open %s", file)
}

reader := bufio.NewReader(f)

var beers []beerscli.Beer

for line := readLine(reader); line != nil; line = readLine(reader) {
for line, err := readLine(reader); line != nil; line, err = readLine(reader) {
if err != nil {
return nil, errors.WrapLoadCSVError(err, "Failed reading of line %d", line)
}

values := strings.Split(string(line), ",")

productID, _ := strconv.Atoi(values[0])
productID, err := strconv.Atoi(values[0])
if err != nil {
return nil, errors.WrapLoadCSVError(err, "Invalid product ID '%s'", values[0])
}

beer := beerscli.NewBeer(
productID,
Expand All @@ -45,7 +58,7 @@ 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()
return
}