Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .covignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.gen.go
dad_jokes.go
15 changes: 12 additions & 3 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ updates:
directory: /
schedule:
interval: monthly
- directory: github
package-ecosystem: github-actions
groups:
prod:
patterns:
- "*"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: monthly
interval: "monthly"
groups:
prod:
patterns:
- "*"
7 changes: 3 additions & 4 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ on:
types: [opened, synchronize, reopened]


env:
GOLANG_VERSION: 1.22

jobs:

Expand All @@ -36,9 +34,10 @@ jobs:
fetch-depth: 0

- name: Setup golang ${{ env.GOLANG_VERSION }}
uses: actions/setup-go@v4
uses: actions/setup-go@v6
with:
go-version: ${{ env.GOLANG_VERSION }}
go-version-file: go.mod
cache-dependency-path: go.sum

- name: install tools
run: make install-ci
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.DS_Store
coverage*
56 changes: 56 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
version: "2"

linters:
enable:
- cyclop
- nestif
- bodyclose
- iface
- gosec
- errcheck
- errchkjson
- errname
- errorlint
- exptostd
- fatcontext
- forbidigo
- forcetypeassert
- govet
- importas
- ireturn
- perfsprint
- recvcheck
- sloglint
- staticcheck
- unparam
- unused
- wastedassign
exclusions:
generated: lax

rules:
# Exclude some linters from running on tests files.
- path: _test\.go

linters:
- gocyclo
- cyclop
- dupl
- gosec
- forbidigo
- bodyclose
- exhaustruct
- fatcontext

- path: dad_jokes.go
linters:
- forbidigo
- errchkjson

settings:
depguard:
rules:
main:
list-mode: lax


22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ if err != nil {

```


### Cancelable HTTP calls
Use <method>Ctx if you want more granular control and the ability to cancel.

```go
var apiErr *fetch.APIError
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

resp, err := client.PutCtx(ctx,url, bytes.NewReader([]byte(`{"hello": "world"}`)), nil)
if err != nil {
if errors.is(err, context.Canceled) {
// Handle context cancelled
}
if errors.As(err, &apiErr) {
fmt.Println("API Response error", apiErr)
}
// Handle non-API Error
}
```


<br>
<br>

Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type APIError struct {
}

func (e *APIError) Error() string {
return fmt.Sprintf("%s [%d]: %s", e.StatusText, e.StatusCode, e.Message)
return fmt.Sprintf("%s: [%d]: %s", e.StatusText, e.StatusCode, e.Message)
}

func (e *APIError) Unwrap() error {
Expand Down
19 changes: 17 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAPIError_Error(t *testing.T) {
StatusText: http.StatusText(http.StatusBadRequest),
Message: "there was an issue with the request",
},
want: "Bad Request [400]: there was an issue with the request",
want: "Bad Request: [400]: there was an issue with the request",
},
{
name: "should return 5xx error",
Expand All @@ -35,7 +35,7 @@ func TestAPIError_Error(t *testing.T) {
StatusText: http.StatusText(http.StatusInternalServerError),
Message: "there was an issue with the request",
},
want: "Internal Server Error [500]: there was an issue with the request",
want: "Internal Server Error: [500]: there was an issue with the request",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -79,3 +79,18 @@ func TestAPIError_errors_is(t *testing.T) {
err := fn()
odize.AssertEqual(t, err.Error(), expected.Error())
}

func TestAPIError_Unwrap(t *testing.T) {
var err *APIError
expectedErr := APIError{
StatusCode: http.StatusBadRequest,
StatusText: http.StatusText(http.StatusBadRequest),
Message: "some issue",
}
var testErr error = &expectedErr
if errors.As(testErr, &err) {
odize.AssertEqual(t, err.Unwrap().Error(), expectedErr.Error())
} else {
t.Error("non matching error")
}
}
Loading
Loading