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
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 responseRegisterErrorHandler
and all its variants require the callback function to return(int, any)
instead of(int, R)
, removing the unnecessary generic- Both
NewErrorResponse
andNewErrorResponseFrom
take a context as a first parameter, this could be the request context but that's up to you
go get github.com/ing-bank/ginerr/v2
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))
}
- 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!