Skip to content

Commit

Permalink
Initial version of v3
Browse files Browse the repository at this point in the history
  • Loading branch information
survivorbat committed Nov 25, 2024
1 parent bdeb163 commit 2fe9e66
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 502 deletions.
110 changes: 110 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
issues:
exclude-rules:
- path: (.+)_test.go
linters:
- goconst # Test data doesn't need to be in constants
- err113 # Necessary for tests

linters-settings:
nlreturn:
block-size: 3

gocritic:
disabled-checks:
- "paramTypeCombine"
- "unnamedResult"
enabled-tags:
- "performance"
- "style"
- "diagnostic"

govet:
enable-all: true
disable:
- fieldalignment

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- copyloopvar
- cyclop
- decorder
- dogsled
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- copyloopvar
- forbidigo
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoinits
- gocognit
- goconst
- gocritic
- err113
- gofmt
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
- ineffassign
- interfacebloat
- intrange
- ireturn
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- mnd
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- nolintlint
- nonamedreturns
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- spancheck
- sqlclosecheck
- staticcheck
- tagalign
- tagliatelle
- tenv
- testifylint
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- zerologlint
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ test: fmt ## Run unit tests, alias: t
go test ./... -timeout=30s -parallel=8

fmt: ## Format go code
@go mod tidy
@gofumpt -l -w .
go mod tidy
gofumpt -l -w .
golangci-lint run --fix ./...

tools: ## Install extra tools for development
go install mvdan.cc/gofumpt@latest
Expand Down
76 changes: 18 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,35 @@ Sending any error back to the user can pose a [big security risk](https://owasp.
For this reason we developed an error registry that allows you to register specific error handlers
for your application. This way you can control what information is sent back to the user.

You can register errors in 3 ways:
- By error type
- By value of string errors
- By defining the error name yourself

## 👷 V2 migration guide

V2 of this library changes the interface of all the methods to allow contexts to be passed to handlers. This
allows you to add additional data to the final response.

The interface changes are as follows.

- `RegisterErrorHandler` and all its variants take a context as a first parameter in the handler, allowing you to pass more data to the response
- `RegisterErrorHandler` and all its variants require the callback function to return `(int, any)` instead of `(int, R)`, removing the unnecessary generic
- Both `NewErrorResponse` and `NewErrorResponseFrom` take a context as a first parameter, this could be the request context but that's up to you
## 👷 V3 migration guide

V3 completely revamps the `ErrorRegistry` and now utilises the `errors` package to match errors.
The following changes have been made:

- `RegisterErrorHandler` now requires a concrete instance of the error as its first argument
- `RegisterErrorHandlerOn` now requires a concrete instance of the error as its second argument
- `RegisterStringErrorHandler` has been removed, use static `errors.New` in `RegisterErrorHandler` to get this to work
- `RegisterStringErrorHandlerOn` has been removed, use static `errors.New` in `RegisterErrorHandlerOn` to get this to work
- `RegisterCustomErrorTypeHandler` has been removed, wrap unexported errors from libraries to create handlers for these
- `RegisterCustomErrorTypeHandlerOn` has been removed, wrap unexported errors from libraries to create handlers for these
- `ErrorRegistry` changes:
- `DefaultCode` has been removed, use `RegisterDefaultHandler` instead
- `DefaultResponse` has been removed, use `RegisterDefaultHandler` instead
- `SetDefaultResponse` has been removed, use `RegisterDefaultHandler` instead

## ⬇️ Installation

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

## 📋 Usage

```go
package main

import (
"github.com/gin-gonic/gin"
"github.com/ing-bank/ginerr/v2"
"net/http"
)

type MyError struct {
}

func (m *MyError) Error() string {
return "Something went wrong!"
}

// Response is an example response object, you can return anything you like
type Response struct {
Errors map[string]any `json:"errors,omitempty"`
}

func main() {
handler := func(ctx context.Context, myError *MyError) (int, any) {
return http.StatusInternalServerError, Response{
Errors: map[string]any{
"error": myError.Error(),
},
}
}

ginerr.RegisterErrorHandler(handler)

// [...]
}

func handleGet(c *gin.Context) {
err := &MyError{}
c.JSON(ginerr.NewErrorResponse(c.Request.Context(), err))
}
```
Check out [the examples here](./examples_test.go).

## 🚀 Development

1. Clone the repository
2. Run `make tools` to install necessary tools
3. Run `make t` to run unit tests
4. Run `make fmt` to format code
3. Run `make fmt` to format code
4. Run `make lint` to lint your code

You can run `make` to see a list of useful commands.
Expand Down
Loading

0 comments on commit 2fe9e66

Please sign in to comment.