From dfd42e9e987a53d461a45f71db60495fa183c32e Mon Sep 17 00:00:00 2001 From: Helge Olav Helgesen Date: Sat, 30 Dec 2023 00:33:31 +0100 Subject: [PATCH] Improved error handling * contextifyError - changed to wrapping the error * cleaned up go.mod/go.sum * error handling * reformatting of some comments --- go.mod | 9 --------- go.sum | 38 -------------------------------------- jokeapi.go | 46 ++++++++++++++++++++++------------------------ 3 files changed, 22 insertions(+), 71 deletions(-) diff --git a/go.mod b/go.mod index 67bb768..5242895 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,3 @@ module github.com/icelain/jokeapi go 1.16 - -require ( - - github.com/yuin/goldmark v1.3.6 // indirect - golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect - golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect - golang.org/x/tools v0.1.1 // indirect - -) diff --git a/go.sum b/go.sum index f580ae6..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,38 +0,0 @@ -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/yuin/goldmark v1.3.6 h1:rvdBidUJAJM2O9VLcNTB4oRwxG33uIxY+zUq6yWUT8c= -github.com/yuin/goldmark v1.3.6/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I= -golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= -golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/jokeapi.go b/jokeapi.go index a662b78..b6a8e96 100755 --- a/jokeapi.go +++ b/jokeapi.go @@ -3,7 +3,8 @@ package jokeapi import ( "encoding/json" "errors" - "io/ioutil" + "fmt" + "io" "net/http" "net/url" "strings" @@ -25,7 +26,7 @@ type jokeConsumer struct { Delivery string `json:"delivery"` } -//Params is the config struct be used by JokeAPI{}.Fetch() +// Params is the config struct be used by JokeAPI{}.Fetch() type Params struct { Categories []string Blacklist []string @@ -33,7 +34,7 @@ type Params struct { Lang string } -//JokesResp is the response to be sent by JokeAPI{}.Fetch() +// JokesResp is the response to be sent by JokeAPI{}.Fetch() type JokesResp struct { Error bool Category string @@ -44,28 +45,25 @@ type JokesResp struct { Lang string } -//JokeAPI struct +// JokeAPI struct type JokeAPI struct { ExportedParams Params } func contextifyError(context string, err error) error { - - return errors.New(context + ": " + err.Error()) - + return fmt.Errorf("%s: %w", context, err) } -//Fetch gets the content with respect to the parameters +// Fetch gets the content with respect to the parameters func (j *JokeAPI) Fetch() (JokesResp, error) { var ( //response = map[string]interface{}{} jokeConsumer jokeConsumer mainURL string - reqUrl *url.URL - err error + reqUrl *url.URL + err error ) - //param handling begins here if len(j.ExportedParams.Categories) > 0 { @@ -73,22 +71,22 @@ func (j *JokeAPI) Fetch() (JokesResp, error) { } else { mainURL = baseURL + "Any" } - + reqUrl, err = url.Parse(mainURL) if err != nil { - + if err != nil { return JokesResp{}, contextifyError("Request failed as url could not be generated", err) } } - + query := reqUrl.Query() query.Set("blacklistFlags", strings.Join(j.ExportedParams.Blacklist, ",")) query.Set("type", j.ExportedParams.JokeType) query.Set("lang", j.ExportedParams.Lang) - + reqUrl.RawQuery = query.Encode() //param handling ends here @@ -97,14 +95,14 @@ func (j *JokeAPI) Fetch() (JokesResp, error) { return JokesResp{}, contextifyError("Request failed", err) } - info, err := ioutil.ReadAll(resp.Body) + info, err := io.ReadAll(resp.Body) if err != nil { return JokesResp{}, contextifyError("Failed to decode request response", err) } if err = json.Unmarshal(info, &jokeConsumer); err != nil { - return JokesResp{}, errors.New("no joke found for your configuration: ") + return JokesResp{}, contextifyError("no joke found for your configuration", err) } @@ -134,7 +132,7 @@ func (j *JokeAPI) Fetch() (JokesResp, error) { }, nil } -//SetParams sets parameters to JokeAPI struct instance. This method only exists because I don't want to make breaking changes to the existing api by removing it. I would recommend using Jokeapi{}.Set() or the singular methods instead +// SetParams sets parameters to JokeAPI struct instance. This method only exists because I don't want to make breaking changes to the existing api by removing it. I would recommend using Jokeapi{}.Set() or the singular methods instead func (j *JokeAPI) SetParams(ctgs []string, blacklist []string, joketype string, lang string) { j.ExportedParams.Categories = ctgs @@ -144,40 +142,40 @@ func (j *JokeAPI) SetParams(ctgs []string, blacklist []string, joketype string, } -//Set sets custom Params struct +// Set sets custom Params struct func (j *JokeAPI) Set(params Params) { j.ExportedParams = params } -//SetCategories sets joke categories. Common categories are Programming | Misc | Spooky | Dark | Fun +// SetCategories sets joke categories. Common categories are Programming | Misc | Spooky | Dark | Fun func (j *JokeAPI) SetCategories(ctgs []string) { j.ExportedParams.Categories = ctgs } -//SetBlacklist sets joke blacklist. Common blacklists are nsfw | religious | political | racist | sexist | explicit +// SetBlacklist sets joke blacklist. Common blacklists are nsfw | religious | political | racist | sexist | explicit func (j *JokeAPI) SetBlacklist(b []string) { j.ExportedParams.Blacklist = b } -//SetLang sets language. Go to https://v2.jokeapi.dev/languages?format=txt to select your preferable language format. By default its en (English). Note that (as of now) most jokes are available in en and de only and setting other languages will give a corresponding error +// SetLang sets language. Go to https://v2.jokeapi.dev/languages?format=txt to select your preferable language format. By default its en (English). Note that (as of now) most jokes are available in en and de only and setting other languages will give a corresponding error func (j *JokeAPI) SetLang(lang string) { j.ExportedParams.Lang = lang } -//SetJokeType sets joke type +// SetJokeType sets joke type func (j *JokeAPI) SetJokeType(s string) { j.ExportedParams.JokeType = s } -//New Generates instance of JokeAPI struct +// New Generates instance of JokeAPI struct func New() *JokeAPI { return &JokeAPI{Params{}} }