error_handler

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 11 Imported by: 0

README

Error Handler Library

Introduction

The Error Handler Library provides a centralized solution for managing errors in Go applications. Its primary purpose is to streamline error handling across various frameworks, ensuring that developers can focus on building features rather than managing error states.

🚀 Features

  • Centralized error handling
  • Automatic database error detection
  • Panic recovery

📦 Installation

go get github.com/emreisler/error-handler

🛠 Usage Examples

Gin Middleware
import "github.com/emreisler/error-handler"

func main() {
    r := gin.Default()
    r.Use(error_handler.GinMiddleware())
    // Your routes here
}
net/http Middleware
import "github.com/emreisler/error-handler"

func main() {
    http.Handle("/", error_handler.NetHTTPMiddleware(http.HandlerFunc(yourHandler)))
    http.ListenAndServe(":8080", nil)
}
Chi Middleware
import "github.com/emreisler/error-handler"

func main() {
    r := chi.NewRouter()
    r.Use(error_handler.ChiMiddleware())
    // Your routes here
}
Fiber Middleware
import "github.com/emreisler/error-handler"

func main() {
    app := fiber.New()
    app.Use(error_handler.FiberMiddleware())
    // Your routes here
}

🔗 Database Error Handling

The mapDBError function automatically maps database errors to a standardized error type, allowing for consistent error responses.

⚠️ Panic Recovery

The library includes built-in panic recovery to prevent application crashes due to unexpected errors.

📌 Error Type Mapping

The library provides a mechanism for mapping different error types to a unified structure, making it easier to handle errors across various components.

🤝 Contribution Guidelines

We welcome contributions! Please submit a pull request or open an issue for any enhancements or bug fixes.

📜 License Information

This library is licensed under the MIT License. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChiMiddleware

func ChiMiddleware(next http.Handler) http.Handler

Chi Middleware for centralized error handling

func FiberMiddleware

func FiberMiddleware() fiber.Handler

FiberMiddleware for centralized error handling

func GinMiddleware

func GinMiddleware() gin.HandlerFunc

GinMiddleware handles all errors centrally

func IsUniqueConstraintViolation

func IsUniqueConstraintViolation(err error) bool

IsUniqueConstraintViolation checks for unique constraint violations

func NetHTTPMiddleware

func NetHTTPMiddleware(next http.Handler) http.Handler

Net/HTTP Middleware

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func BadRequestError

func BadRequestError(msg string) Error

func ConflictError

func ConflictError(msg string) Error

func ForbiddenError

func ForbiddenError(msg string) Error

func InternalServerError

func InternalServerError(msg string) Error

func New

func New(message string, httpStatus int) Error

func NotFoundError

func NotFoundError(msg string) Error

func ServiceUnavailableError

func ServiceUnavailableError(msg string) Error

func TooManyRequestsError

func TooManyRequestsError(msg string) Error

func UnauthorizedError

func UnauthorizedError(msg string) Error

func UnprocessableEntityError

func UnprocessableEntityError(msg string) Error

func (Error) Error

func (e Error) Error() string

func (Error) HttpStatus

func (e Error) HttpStatus() int

Directories

Path Synopsis
examples
gin

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL