Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ing-bank/ginerr
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.2
Choose a base ref
...
head repository: ing-bank/ginerr
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 9 commits
  • 17 files changed
  • 2 contributors

Commits on Jun 5, 2024

  1. Delete v1

    survivorbat committed Jun 5, 2024
    Copy the full SHA
    2df0291 View commit details
  2. Merge pull request #6 from ing-bank/feature/delete-v1

    Delete v1
    survivorbat authored Jun 5, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    bdeb163 View commit details

Commits on Nov 25, 2024

  1. Initial version of v3

    survivorbat committed Nov 25, 2024
    Copy the full SHA
    2fe9e66 View commit details
  2. Simplify version matrices

    survivorbat committed Nov 25, 2024
    Copy the full SHA
    db8fb93 View commit details
  3. Fix comment

    survivorbat committed Nov 25, 2024
    Copy the full SHA
    1592352 View commit details

Commits on Nov 28, 2024

  1. Add test to verify whether an error from a function is handled properly

    survivorbat committed Nov 28, 2024
    Copy the full SHA
    242ebd5 View commit details
  2. Merge pull request #8 from ing-bank/v3

    Initial version of v3
    survivorbat authored Nov 28, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    a773aa4 View commit details

Commits on Jan 8, 2025

  1. Update linting config to be opt-out

    survivorbat committed Jan 8, 2025
    Copy the full SHA
    0eef28e View commit details
  2. Fix issue in example test

    survivorbat committed Jan 8, 2025
    Copy the full SHA
    76665a7 View commit details
Showing with 334 additions and 1,358 deletions.
  1. +1 −1 .github/workflows/test.yaml
  2. +30 −85 .golangci.yaml
  3. +3 −2 Makefile
  4. +18 −49 README.md
  5. +74 −115 errors.go
  6. +148 −200 errors_test.go
  7. +58 −43 examples_test.go
  8. +2 −2 go.mod
  9. +0 −15 v2/.gitignore
  10. +0 −21 v2/LICENSE
  11. +0 −22 v2/Makefile
  12. +0 −86 v2/README.md
  13. +0 −159 v2/errors.go
  14. +0 −443 v2/errors_test.go
  15. +0 −68 v2/examples_test.go
  16. +0 −14 v2/go.mod
  17. +0 −33 v2/go.sum
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.18', '1.19', '1.20' ]
go-version: [ '1.23.0' ]
steps:
- uses: actions/checkout@v3

115 changes: 30 additions & 85 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,96 +1,41 @@
issues:
exclude-rules:
- path: (.+)_test.go
linters:
- goconst # Test data doesn't need to be in constants
- err113 # Necessary for tests
- fatcontext # We save contexts for validation

linters-settings:
nlreturn:
block-size: 3

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

issues:
exclude-rules:
- path: (.+)_test.go
linters:
- funlen
- goconst
- nilnil
- goerr113
govet:
enable-all: true
disable:
- fieldalignment

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- dogsled
- dupword
- durationcheck
- errcheck
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forbidigo
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- goerr113
- gofmt
- gofumpt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- ifshort
- importas
- ineffassign
- interfacebloat
- interfacer
- ireturn
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- nonamedreturns
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- rowserrcheck
- sqlclosecheck
- staticcheck
- tenv
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- zerologlint
enable-all: true
disable:
# Disabled because they're too strict
- gochecknoglobals # We sometimes use global variables for ease of use
- depguard # Unnecessary check
- exhaustruct # We don't always have to fill all fields
- lll # We don't enforce line lengths
- wsl # Too strict about statement placement
- wrapcheck # We don't enforce wrapping
- varnamelen # We don't enforce var name length
- testpackage # We don't use test packages
- funlen # We don't enforce the length of a function
- exportloopref # Deprecated and no longer required in 1.22

5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
67 changes: 18 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,70 +4,39 @@
![GitHub](https://img.shields.io/github/license/ing-bank/ginerr)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/ing-bank/ginerr)

**[❗ 🚨 Click here for version 2 🚨 ❗](./v2)**

Sending any error back to the user can pose a [big security risk](https://owasp.org/www-community/Improper_Error_Handling).
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

## ⬇️ Installation

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

## 📋 Usage

```go
package main

import (
"github.com/gin-gonic/gin"
"github.com/ing-bank/ginerr"
"net/http"
)
## 👷 V3 migration guide

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

func (m *MyError) Error() string {
return "Something went wrong!"
}
- `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

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

func main() {
handler := func(myError *MyError) (int, Response) {
return http.StatusInternalServerError, Response{
Errors: map[string]any{
"error": myError.Error(),
},
}
}
`go get github.com/ing-bank/ginerr/v3`

ginerr.RegisterErrorHandler(handler)

// [...]
}
## 📋 Usage

func handleGet(c *gin.Context) {
err := &MyError{}
c.JSON(ginerr.NewErrorResponse(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.
Loading