Skip to content

ing-bank/ginerr

Repository files navigation

🦁 Gin Error Registry

Go package GitHub GitHub go.mod Go version

❗ 🚨 Click here for version 2 🚨 ❗

Sending any error back to the user can pose a big security risk. 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

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/ing-bank/ginerr"
	"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(myError *MyError) (int, Response) {
		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(err))
}

🚀 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
  5. Run make lint to lint your code

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

🔭 Future Plans

Nothing here yet!