Elegant error response handling for Echo web framework with built-in support for standard HTTP errors and custom error types.
- π― Simple Integration - Easy to integrate with existing Echo applications
- π§ Custom Error Types - Define your own error types and response formats
- π Standard HTTP Errors - Built-in support for all standard HTTP status codes
- β‘ Lightweight - Minimal dependencies and overhead
- π¦ Type-Safe - Go type-safe error handling
- π Extensible - Flexible configuration for custom needs
go get github.com/prongbang/echoerror
package main
import (
"github.com/prongbang/goerror"
"github.com/prongbang/echoerror"
"github.com/labstack/echo/v4"
)
func main() {
app := echo.New()
response := echoerror.New()
app.GET("/", func(c echo.Context) error {
return response.With(c).Response(goerror.NewUnauthorized())
})
app.Logger.Fatal(app.Start(":1323"))
}
Create and handle custom error types with specific response formats:
package main
import (
"net/http"
"github.com/prongbang/goerror"
"github.com/prongbang/echoerror"
"github.com/labstack/echo/v4"
)
// Define custom error type
type CustomError struct {
goerror.Body
}
func (c *CustomError) Error() string {
return c.Message
}
func NewCustomError() error {
return &CustomError{
Body: goerror.Body{
Code: "CUS001",
Message: "Custom error occurred",
},
}
}
// Define custom response handler
type customResponse struct{}
func (c *customResponse) Response(ctx echo.Context, err error) error {
switch resp := err.(type) {
case *CustomError:
return ctx.JSON(http.StatusBadRequest, resp)
}
return nil
}
func NewCustomResponse() echoerror.Custom {
return &customResponse{}
}
func main() {
app := echo.New()
// Configure custom response
customResp := NewCustomResponse()
response := echoerror.New(&echoerror.Config{
Custom: &customResp,
})
app.GET("/", func(c echo.Context) error {
return response.With(c).Response(NewCustomError())
})
app.Logger.Fatal(app.Start(":1323"))
}
Work with all standard HTTP error types from the goerror
package:
// 4xx Client Errors
goerror.NewBadRequest()
goerror.NewUnauthorized()
goerror.NewForbidden()
goerror.NewNotFound()
goerror.NewMethodNotAllowed()
goerror.NewConflict()
goerror.NewUnprocessableEntity()
goerror.NewTooManyRequests()
// 5xx Server Errors
goerror.NewInternalServerError()
goerror.NewNotImplemented()
goerror.NewBadGateway()
goerror.NewServiceUnavailable()
goerror.NewGatewayTimeout()
Define custom errors with additional fields:
type ValidationError struct {
goerror.Body
Errors map[string]string `json:"errors"`
}
func NewValidationError(errors map[string]string) error {
return &ValidationError{
Body: goerror.Body{
Code: "VAL001",
Message: "Validation failed",
},
Errors: errors,
}
}
app.POST("/users", func(c echo.Context) error {
var user User
if err := c.Bind(&user); err != nil {
return response.With(c).Response(goerror.NewBadRequest("Invalid request body"))
}
if err := validator.Validate(user); err != nil {
return response.With(c).Response(NewValidationError(err))
}
// Process user...
return c.JSON(http.StatusCreated, user)
})
type customResponse struct{}
func (c *customResponse) Response(ctx echo.Context, err error) error {
switch resp := err.(type) {
case *ValidationError:
return ctx.JSON(http.StatusBadRequest, resp)
case *AuthenticationError:
return ctx.JSON(http.StatusUnauthorized, resp)
case *ForbiddenError:
return ctx.JSON(http.StatusForbidden, resp)
default:
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"code": "INTERNAL_ERROR",
"message": "An unexpected error occurred",
})
}
}
Option | Type | Description |
---|---|---|
Custom |
*Custom |
Custom error response handler |
Standard response structure:
{
"code": "CUS001",
"message": "Custom error message"
}
With additional fields:
{
"code": "VAL001",
"message": "Validation failed",
"errors": {
"email": "Invalid email format",
"password": "Password too short"
}
}
- Use Standard Errors - Prefer standard HTTP errors when possible
- Consistent Format - Keep error response format consistent
- Meaningful Codes - Use descriptive error codes
- Clear Messages - Provide helpful error messages
- Status Codes - Use appropriate HTTP status codes
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this package helpful, please consider supporting it:
- goerror - Core error interface for Go
- Echo - High performance, minimalist Go web framework
- fibererror - Error handler for Fiber framework