From aef753fe48e359f2865dca888c3d1e014eb848bc Mon Sep 17 00:00:00 2001 From: Xisco Bibiloni Date: Mon, 12 Dec 2022 10:35:31 +0100 Subject: [PATCH 1/3] feat:New store command --- 02-refactor-to-cobra/cmd/beers-cli/main.go | 1 + 02-refactor-to-cobra/internal/cli/beers.go | 23 ++------------------- 02-refactor-to-cobra/internal/cli/common.go | 23 +++++++++++++++++++++ 02-refactor-to-cobra/internal/cli/stores.go | 23 +++++++++++++++++++++ go.sum | 15 +++++++------- 5 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 02-refactor-to-cobra/internal/cli/common.go create mode 100644 02-refactor-to-cobra/internal/cli/stores.go diff --git a/02-refactor-to-cobra/cmd/beers-cli/main.go b/02-refactor-to-cobra/cmd/beers-cli/main.go index 4b3ae21..e074938 100644 --- a/02-refactor-to-cobra/cmd/beers-cli/main.go +++ b/02-refactor-to-cobra/cmd/beers-cli/main.go @@ -8,5 +8,6 @@ import ( func main() { rootCmd := &cobra.Command{Use: "beers-cli"} rootCmd.AddCommand(cli.InitBeersCmd()) + rootCmd.AddCommand(cli.InitStoresCmd()) rootCmd.Execute() } diff --git a/02-refactor-to-cobra/internal/cli/beers.go b/02-refactor-to-cobra/internal/cli/beers.go index 26c141f..b76e2ca 100644 --- a/02-refactor-to-cobra/internal/cli/beers.go +++ b/02-refactor-to-cobra/internal/cli/beers.go @@ -1,43 +1,24 @@ package cli import ( - "fmt" - "github.com/spf13/cobra" ) -// CobraFn function definion of run cobra command -type CobraFn func(cmd *cobra.Command, args []string) - var beers = map[string]string{ "01D9X58E7NPXX5MVCR9QN794CH": "Mad Jack Mixer", "01D9X5BQ5X48XMMVZ2F2G3R5MS": "Keystone Ice", "01D9X5CVS1M9VR5ZD627XDF6ND": "Belgian Moon", } -const idFlag = "id" - // InitBeersCmd initialize beers command func InitBeersCmd() *cobra.Command { beersCmd := &cobra.Command{ Use: "beers", Short: "Print data about beers", - Run: runBeersFn(), + Run: runCommandFn(beers), } - beersCmd.Flags().StringP(idFlag, "i", "", "id of the beer") + beersCmd.Flags().StringP(idFlag, shortIdFlag, "", "id of the beer") return beersCmd } - -func runBeersFn() CobraFn { - return func(cmd *cobra.Command, args []string) { - id, _ := cmd.Flags().GetString(idFlag) - - if id != "" { - fmt.Println(beers[id]) - } else { - fmt.Println(beers) - } - } -} diff --git a/02-refactor-to-cobra/internal/cli/common.go b/02-refactor-to-cobra/internal/cli/common.go new file mode 100644 index 0000000..08291bf --- /dev/null +++ b/02-refactor-to-cobra/internal/cli/common.go @@ -0,0 +1,23 @@ +package cli + +import ( + "fmt" + "github.com/spf13/cobra" +) + +const idFlag = "id" +const shortIdFlag = "i" + +type CobraFn func(cmd *cobra.Command, args []string) + +func runCommandFn(values map[string]string) CobraFn { + return func(cmd *cobra.Command, args []string) { + id, _ := cmd.Flags().GetString(idFlag) + + if id != "" { + fmt.Println(values[id]) + } else { + fmt.Println(values) + } + } +} diff --git a/02-refactor-to-cobra/internal/cli/stores.go b/02-refactor-to-cobra/internal/cli/stores.go new file mode 100644 index 0000000..b4c95f7 --- /dev/null +++ b/02-refactor-to-cobra/internal/cli/stores.go @@ -0,0 +1,23 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +var stores = map[string]string{ + "01DC9ZAPGKEQJS4P4A48EG3P43": "Mercadona", + "01DC9ZB23EW0J0ARAER09SJDKC": "Carrefour", + "01DC9ZB89V1PQD977ZE6QXSQHH": "Alcampo", +} + +func InitStoresCmd() *cobra.Command { + storesCmd := &cobra.Command{ + Use: "stores", + Short: "Print store identify", + Run: runCommandFn(stores), + } + + storesCmd.Flags().StringP(idFlag, shortIdFlag, "", "id of the Store") + + return storesCmd +} diff --git a/go.sum b/go.sum index e0ec129..517e856 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -43,16 +43,17 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= -github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -68,8 +69,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -91,8 +90,6 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.7 h1:FfTH+vuMXOas8jmfb5/M7dzEYx7LpcLb7a0LPe34uOU= github.com/spf13/cobra v0.0.7/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= @@ -102,8 +99,8 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= @@ -142,10 +139,12 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 4d9412d5271eb6213de7ff469818fd2c3d2b1776 Mon Sep 17 00:00:00 2001 From: Xisco Bibiloni Date: Wed, 4 Jan 2023 09:15:20 +0100 Subject: [PATCH 2/3] =?UTF-8?q?feat:Ejercicio=20para=20a=C3=B1adir=20error?= =?UTF-8?q?es=20por=20comportamiento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmd/beers-cli/main.go | 2 +- 07-behaviour_error_handling/internal/beer.go | 3 +- .../internal/cli/beers.go | 5 +++- .../internal/errors/errortypes.go | 28 ------------------- .../internal/errors/fileerrortype.go | 23 +++++++++++++++ .../internal/errors/formaterrortype.go | 23 +++++++++++++++ .../internal/errors/httperrortype.go | 23 +++++++++++++++ .../errors/jsonprocessingerrortype.go | 23 +++++++++++++++ .../internal/storage/csv/repository.go | 15 ++++++++-- .../internal/storage/ontario/repository.go | 6 ++-- 10 files changed, 115 insertions(+), 36 deletions(-) delete mode 100644 07-behaviour_error_handling/internal/errors/errortypes.go create mode 100644 07-behaviour_error_handling/internal/errors/fileerrortype.go create mode 100644 07-behaviour_error_handling/internal/errors/formaterrortype.go create mode 100644 07-behaviour_error_handling/internal/errors/httperrortype.go create mode 100644 07-behaviour_error_handling/internal/errors/jsonprocessingerrortype.go diff --git a/07-behaviour_error_handling/cmd/beers-cli/main.go b/07-behaviour_error_handling/cmd/beers-cli/main.go index 8fedd45..aa99dfb 100644 --- a/07-behaviour_error_handling/cmd/beers-cli/main.go +++ b/07-behaviour_error_handling/cmd/beers-cli/main.go @@ -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() diff --git a/07-behaviour_error_handling/internal/beer.go b/07-behaviour_error_handling/internal/beer.go index 4ed7c21..d7bffcd 100644 --- a/07-behaviour_error_handling/internal/beer.go +++ b/07-behaviour_error_handling/internal/beer.go @@ -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 @@ -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 diff --git a/07-behaviour_error_handling/internal/cli/beers.go b/07-behaviour_error_handling/internal/cli/beers.go index 8f8e666..108c3d0 100644 --- a/07-behaviour_error_handling/internal/cli/beers.go +++ b/07-behaviour_error_handling/internal/cli/beers.go @@ -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) } diff --git a/07-behaviour_error_handling/internal/errors/errortypes.go b/07-behaviour_error_handling/internal/errors/errortypes.go deleted file mode 100644 index eb443cf..0000000 --- a/07-behaviour_error_handling/internal/errors/errortypes.go +++ /dev/null @@ -1,28 +0,0 @@ -package errors - -import ( - "github.com/pkg/errors" -) - -type dataUnreacheable struct { - error -} - -// WrapDataUnreacheable returns an error which wraps err that satisfies -// IsDataUnreacheable() -func WrapDataUnreacheable(err error, format string, args ...interface{}) error { - return &dataUnreacheable{errors.Wrapf(err, format, args...)} -} - -// NewDataUnreacheable returns an error which satisfies IsDataUnreacheable() -func NewDataUnreacheable(format string, args ...interface{}) error { - return &dataUnreacheable{errors.Errorf(format, args...)} -} - -// IsDataUnreacheable reports whether err was created with DataUnreacheablef() or -// NewDataUnreacheable() -func IsDataUnreacheable(err error) bool { - err = errors.Cause(err) - _, ok := err.(*dataUnreacheable) - return ok -} diff --git a/07-behaviour_error_handling/internal/errors/fileerrortype.go b/07-behaviour_error_handling/internal/errors/fileerrortype.go new file mode 100644 index 0000000..90eacee --- /dev/null +++ b/07-behaviour_error_handling/internal/errors/fileerrortype.go @@ -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 +} diff --git a/07-behaviour_error_handling/internal/errors/formaterrortype.go b/07-behaviour_error_handling/internal/errors/formaterrortype.go new file mode 100644 index 0000000..0c094a8 --- /dev/null +++ b/07-behaviour_error_handling/internal/errors/formaterrortype.go @@ -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 +} diff --git a/07-behaviour_error_handling/internal/errors/httperrortype.go b/07-behaviour_error_handling/internal/errors/httperrortype.go new file mode 100644 index 0000000..8908421 --- /dev/null +++ b/07-behaviour_error_handling/internal/errors/httperrortype.go @@ -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 +} diff --git a/07-behaviour_error_handling/internal/errors/jsonprocessingerrortype.go b/07-behaviour_error_handling/internal/errors/jsonprocessingerrortype.go new file mode 100644 index 0000000..1f61bc9 --- /dev/null +++ b/07-behaviour_error_handling/internal/errors/jsonprocessingerrortype.go @@ -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 +} diff --git a/07-behaviour_error_handling/internal/storage/csv/repository.go b/07-behaviour_error_handling/internal/storage/csv/repository.go index cb1e7cc..ae0bba9 100644 --- a/07-behaviour_error_handling/internal/storage/csv/repository.go +++ b/07-behaviour_error_handling/internal/storage/csv/repository.go @@ -2,6 +2,7 @@ package csv import ( "bufio" + "github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/errors" "os" "strconv" "strings" @@ -19,7 +20,12 @@ 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 @@ -27,7 +33,11 @@ func (r *repository) GetBeers() ([]beerscli.Beer, error) { 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, @@ -47,5 +57,6 @@ func (r *repository) GetBeers() ([]beerscli.Beer, error) { func readLine(reader *bufio.Reader) (line []byte) { line, _, _ = reader.ReadLine() + return } diff --git a/07-behaviour_error_handling/internal/storage/ontario/repository.go b/07-behaviour_error_handling/internal/storage/ontario/repository.go index ea6608c..4696c42 100644 --- a/07-behaviour_error_handling/internal/storage/ontario/repository.go +++ b/07-behaviour_error_handling/internal/storage/ontario/repository.go @@ -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 } From 4b88bde8c8367a1de9ba5c44b0045b6a11842850 Mon Sep 17 00:00:00 2001 From: Xisco Bibiloni Date: Wed, 4 Jan 2023 09:15:57 +0100 Subject: [PATCH 3/3] feat:Add .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea