Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
2 changes: 1 addition & 1 deletion 07-behaviour_error_handling/cmd/beers-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func main() {

csvData := flag.Bool("csv", false, "load data from csv")
csvData := flag.Bool("csv", true, "load data from csv")
flag.Parse()
var repo beerscli.BeerRepo
repo = csv.NewRepository()
Expand Down
3 changes: 2 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,7 @@ 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 @@ -66,7 +67,7 @@ func (t *BeerType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
return errors.WrapJsonProcessingDataError(err, "can't parser bytes to Json")
}
*t = toID[j]
return nil
Expand Down
5 changes: 4 additions & 1 deletion 07-behaviour_error_handling/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func InitBeersCmd(repository beerscli.BeerRepo) *cobra.Command {
func runBeersFn(repository beerscli.BeerRepo) CobraFn {
return func(cmd *cobra.Command, args []string) {
beers, err := repository.GetBeers()
if errors.IsDataUnreacheable(err) {
if errors.IsFileErrorType(err) {
log.Fatal(err)
}
if errors.IsFormatDataError(err) {
log.Fatal(err)
}

Expand Down
28 changes: 0 additions & 28 deletions 07-behaviour_error_handling/internal/errors/errortypes.go

This file was deleted.

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

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

type fileDataError struct {
error
}

func WrapFileDataUError(err error, format string, args ...interface{}) error {
return &fileDataError{errors.Wrapf(err, format, args...)}
}

func NewFileDataError(format string, args ...interface{}) error {
return &fileDataError{errors.Errorf(format, args...)}
}

func IsFileErrorType(err error) bool {
err = errors.Cause(err)
_, ok := err.(*fileDataError)
return ok
}
23 changes: 23 additions & 0 deletions 07-behaviour_error_handling/internal/errors/formaterrortype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

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

type formatDataError struct {
error
}

func WrapFormatDataError(err error, format string, args ...interface{}) error {
return &formatDataError{errors.Wrapf(err, format, args...)}
}

func NewFormatDataError(format string, args ...interface{}) error {
return &formatDataError{errors.Errorf(format, args...)}
}

func IsFormatDataError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*formatDataError)
return ok
}
23 changes: 23 additions & 0 deletions 07-behaviour_error_handling/internal/errors/httperrortype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

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

type httpDataError struct {
error
}

func WrapHttpDataError(err error, format string, args ...interface{}) error {
return &httpDataError{errors.Wrapf(err, format, args...)}
}

func NewHttpDataError(format string, args ...interface{}) error {
return &httpDataError{errors.Errorf(format, args...)}
}

func IsHttpDataError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*httpDataError)
return ok
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

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

type jsonProcessingDataError struct {
error
}

func WrapJsonProcessingDataError(err error, format string, args ...interface{}) error {
return &jsonProcessingDataError{errors.Wrapf(err, format, args...)}
}

func NewJsonProcessingDataError(format string, args ...interface{}) error {
return &jsonProcessingDataError{errors.Errorf(format, args...)}
}

func IsJsonProcessingDataError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*jsonProcessingDataError)
return ok
}
15 changes: 13 additions & 2 deletions 07-behaviour_error_handling/internal/storage/csv/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package csv

import (
"bufio"
"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/errors"
"os"
"strconv"
"strings"
Expand All @@ -19,15 +20,24 @@ 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")
f, err := os.Open("07-behaviour_error_handling/data/beers2.csv")

if err != nil {
return nil, errors.WrapFileDataUError(err, "error getting beers to %s", "csv data")
}

reader := bufio.NewReader(f)

var beers []beerscli.Beer

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

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

if err != nil {
return nil, errors.WrapFormatDataError(err, "can't format product Id with Atoi method")
}

beer := beerscli.NewBeer(
productID,
Expand All @@ -47,5 +57,6 @@ func (r *repository) GetBeers() ([]beerscli.Beer, error) {

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

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ func NewOntarioRepository() beerscli.BeerRepo {
func (b *beerRepo) GetBeers() (beers []beerscli.Beer, err error) {
response, err := http.Get(fmt.Sprintf("%v%v", b.url, productsEndpoint))
if err != nil {
return nil, errors.WrapDataUnreacheable(err, "error getting response to %s", productsEndpoint)
return nil, errors.WrapHttpDataError(err, "error getting response to %s", productsEndpoint)
}

contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, errors.WrapDataUnreacheable(err, "error reading the response from %s", productsEndpoint)
return nil, errors.WrapHttpDataError(err, "error reading the response from %s", productsEndpoint)
}

err = json.Unmarshal(contents, &beers)
if err != nil {
return nil, errors.WrapDataUnreacheable(err, "can't parsing response into beers")
return nil, errors.WrapJsonProcessingDataError(err, "can't parsing response into beers")
}
return
}