Skip to content

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

License

Notifications You must be signed in to change notification settings

abdelrhman-basyoni/gin-error-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ginErrorHandler

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.

Installation

To install this package, use the standard go get command:

go get -u github.com/abdelrhman-basyoni/ginErrorHandler

Usage

Middleware Setup

To use ginErrorHandler as a middleware in your Gin application, follow these steps:

  1. Import the package:

    import "github.com/yourusername/ginErrorHandler"
    
  2. Create a Gin router instance:

    r := gin.Default()
  3. 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"))
  4. Define your Gin routes and handlers as usual.

  5. Run your Gin application:

    r.Run(":8080")
  6. 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
	}

Customization

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"))

Contributing

Contributions to ginErrorHandler are welcome. If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

Contact

For questions or feedback, please contact:

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Special thanks to the Gin web framework community and contributors.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages