Skip to content

Commit 2fe9e66

Browse files
committed
Initial version of v3
1 parent bdeb163 commit 2fe9e66

File tree

7 files changed

+357
-502
lines changed

7 files changed

+357
-502
lines changed

.golangci.yaml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
issues:
2+
exclude-rules:
3+
- path: (.+)_test.go
4+
linters:
5+
- goconst # Test data doesn't need to be in constants
6+
- err113 # Necessary for tests
7+
8+
linters-settings:
9+
nlreturn:
10+
block-size: 3
11+
12+
gocritic:
13+
disabled-checks:
14+
- "paramTypeCombine"
15+
- "unnamedResult"
16+
enabled-tags:
17+
- "performance"
18+
- "style"
19+
- "diagnostic"
20+
21+
govet:
22+
enable-all: true
23+
disable:
24+
- fieldalignment
25+
26+
linters:
27+
enable:
28+
- asasalint
29+
- asciicheck
30+
- bidichk
31+
- bodyclose
32+
- canonicalheader
33+
- containedctx
34+
- contextcheck
35+
- copyloopvar
36+
- cyclop
37+
- decorder
38+
- dogsled
39+
- durationcheck
40+
- errcheck
41+
- errchkjson
42+
- errname
43+
- errorlint
44+
- exhaustive
45+
- copyloopvar
46+
- forbidigo
47+
- ginkgolinter
48+
- gocheckcompilerdirectives
49+
- gochecknoinits
50+
- gocognit
51+
- goconst
52+
- gocritic
53+
- err113
54+
- gofmt
55+
- gofumpt
56+
- goheader
57+
- goimports
58+
- gomoddirectives
59+
- gomodguard
60+
- goprintffuncname
61+
- gosec
62+
- gosimple
63+
- gosmopolitan
64+
- govet
65+
- grouper
66+
- importas
67+
- ineffassign
68+
- interfacebloat
69+
- intrange
70+
- ireturn
71+
- loggercheck
72+
- maintidx
73+
- makezero
74+
- mirror
75+
- misspell
76+
- mnd
77+
- musttag
78+
- nakedret
79+
- nestif
80+
- nilerr
81+
- nilnil
82+
- nlreturn
83+
- nolintlint
84+
- nonamedreturns
85+
- nosprintfhostport
86+
- paralleltest
87+
- perfsprint
88+
- prealloc
89+
- predeclared
90+
- promlinter
91+
- reassign
92+
- revive
93+
- rowserrcheck
94+
- spancheck
95+
- sqlclosecheck
96+
- staticcheck
97+
- tagalign
98+
- tagliatelle
99+
- tenv
100+
- testifylint
101+
- thelper
102+
- tparallel
103+
- typecheck
104+
- unconvert
105+
- unparam
106+
- unused
107+
- usestdlibvars
108+
- wastedassign
109+
- whitespace
110+
- zerologlint

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ test: fmt ## Run unit tests, alias: t
1111
go test ./... -timeout=30s -parallel=8
1212

1313
fmt: ## Format go code
14-
@go mod tidy
15-
@gofumpt -l -w .
14+
go mod tidy
15+
gofumpt -l -w .
16+
golangci-lint run --fix ./...
1617

1718
tools: ## Install extra tools for development
1819
go install mvdan.cc/gofumpt@latest

README.md

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,35 @@ Sending any error back to the user can pose a [big security risk](https://owasp.
88
For this reason we developed an error registry that allows you to register specific error handlers
99
for your application. This way you can control what information is sent back to the user.
1010

11-
You can register errors in 3 ways:
12-
- By error type
13-
- By value of string errors
14-
- By defining the error name yourself
15-
16-
## 👷 V2 migration guide
17-
18-
V2 of this library changes the interface of all the methods to allow contexts to be passed to handlers. This
19-
allows you to add additional data to the final response.
20-
21-
The interface changes are as follows.
22-
23-
- `RegisterErrorHandler` and all its variants take a context as a first parameter in the handler, allowing you to pass more data to the response
24-
- `RegisterErrorHandler` and all its variants require the callback function to return `(int, any)` instead of `(int, R)`, removing the unnecessary generic
25-
- Both `NewErrorResponse` and `NewErrorResponseFrom` take a context as a first parameter, this could be the request context but that's up to you
11+
## 👷 V3 migration guide
12+
13+
V3 completely revamps the `ErrorRegistry` and now utilises the `errors` package to match errors.
14+
The following changes have been made:
15+
16+
- `RegisterErrorHandler` now requires a concrete instance of the error as its first argument
17+
- `RegisterErrorHandlerOn` now requires a concrete instance of the error as its second argument
18+
- `RegisterStringErrorHandler` has been removed, use static `errors.New` in `RegisterErrorHandler` to get this to work
19+
- `RegisterStringErrorHandlerOn` has been removed, use static `errors.New` in `RegisterErrorHandlerOn` to get this to work
20+
- `RegisterCustomErrorTypeHandler` has been removed, wrap unexported errors from libraries to create handlers for these
21+
- `RegisterCustomErrorTypeHandlerOn` has been removed, wrap unexported errors from libraries to create handlers for these
22+
- `ErrorRegistry` changes:
23+
- `DefaultCode` has been removed, use `RegisterDefaultHandler` instead
24+
- `DefaultResponse` has been removed, use `RegisterDefaultHandler` instead
25+
- `SetDefaultResponse` has been removed, use `RegisterDefaultHandler` instead
2626

2727
## ⬇️ Installation
2828

29-
`go get github.com/ing-bank/ginerr/v2`
29+
`go get github.com/ing-bank/ginerr/v3`
3030

3131
## 📋 Usage
3232

33-
```go
34-
package main
35-
36-
import (
37-
"github.com/gin-gonic/gin"
38-
"github.com/ing-bank/ginerr/v2"
39-
"net/http"
40-
)
41-
42-
type MyError struct {
43-
}
44-
45-
func (m *MyError) Error() string {
46-
return "Something went wrong!"
47-
}
48-
49-
// Response is an example response object, you can return anything you like
50-
type Response struct {
51-
Errors map[string]any `json:"errors,omitempty"`
52-
}
53-
54-
func main() {
55-
handler := func(ctx context.Context, myError *MyError) (int, any) {
56-
return http.StatusInternalServerError, Response{
57-
Errors: map[string]any{
58-
"error": myError.Error(),
59-
},
60-
}
61-
}
62-
63-
ginerr.RegisterErrorHandler(handler)
64-
65-
// [...]
66-
}
67-
68-
func handleGet(c *gin.Context) {
69-
err := &MyError{}
70-
c.JSON(ginerr.NewErrorResponse(c.Request.Context(), err))
71-
}
72-
```
33+
Check out [the examples here](./examples_test.go).
7334

7435
## 🚀 Development
7536

7637
1. Clone the repository
7738
2. Run `make tools` to install necessary tools
78-
3. Run `make t` to run unit tests
79-
4. Run `make fmt` to format code
39+
3. Run `make fmt` to format code
8040
4. Run `make lint` to lint your code
8141

8242
You can run `make` to see a list of useful commands.

0 commit comments

Comments
 (0)