❗ 🚨 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
go get github.com/ing-bank/ginerr
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))
}
- Clone the repository
- Run
make tools
to install necessary tools - Run
make t
to run unit tests - Run
make fmt
to format code - Run
make lint
to lint your code
You can run make
to see a list of useful commands.
Nothing here yet!