ginErrorHandler
is a Go package that provides a global error handler middleware for the Gin web framework. It collects error messages from Gin errors and responds with a JSON payload containing the error messages.
To install this package, use the standard go get
command:
go get -u github.com/abdelrhman-basyoni/ginErrorHandler
To use ginErrorHandler
as a middleware in your Gin application, follow these steps:
-
Import the package:
import "github.com/yourusername/ginErrorHandler"
-
Create a Gin router instance:
r := gin.Default()
-
Add the
GlobalErrorHandler
middleware to your Gin router :// add it first thing under the r:= gin.Default() r := gin.Default() r.Use(ginErrorHandler.GlobalErrorHandler("errors"))
-
Define your Gin routes and handlers as usual.
-
Run your Gin application:
r.Run(":8080")
-
Inside your controller when you handle error handle it like this
token, err := useCases.Login(login.Email, login.Password)
if err != nil {
ginContext.Error(err)
return
}
You can customize the error key used in the JSON response by passing it as an argument to the GlobalErrorHandler
function.
r.Use(ginErrorHandler.GlobalErrorHandler("customErrorKey"))
Contributions to ginErrorHandler
are welcome. If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
For questions or feedback, please contact:
- Email: [email protected]
- GitHub: GitHub Profile
This project is licensed under the MIT License - see the LICENSE file for details.
Special thanks to the Gin web framework community and contributors.