Skip to content

Create a custom response using the error interface in Go

License

Notifications You must be signed in to change notification settings

prongbang/echoerror

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

echoerror 🚨

Go Report Card Go Reference License: MIT Go Version

Elegant error response handling for Echo web framework with built-in support for standard HTTP errors and custom error types.

✨ Features

  • 🎯 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

πŸ“¦ Installation

go get github.com/prongbang/echoerror

πŸš€ Quick Start

Basic Usage with Standard HTTP Status

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

πŸ› οΈ Advanced Usage

Custom Error Types

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

πŸ“š Error Types

Standard HTTP Errors

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

Custom Error Structure

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,
    }
}

πŸ” Examples

API Error Handling

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

Multiple Error Types

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

βš™οΈ Configuration Options

echoerror.Config

Option Type Description
Custom *Custom Custom error response handler

Error Response Format

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

πŸ”§ Best Practices

  1. Use Standard Errors - Prefer standard HTTP errors when possible
  2. Consistent Format - Keep error response format consistent
  3. Meaningful Codes - Use descriptive error codes
  4. Clear Messages - Provide helpful error messages
  5. Status Codes - Use appropriate HTTP status codes

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

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

πŸ’– Support the Project

If you find this package helpful, please consider supporting it:

"Buy Me A Coffee"

πŸ”— Related Projects

  • goerror - Core error interface for Go
  • Echo - High performance, minimalist Go web framework
  • fibererror - Error handler for Fiber framework

About

Create a custom response using the error interface in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages