echoerror

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 3 Imported by: 0

README

echoerror

Install
go get github.com/prongbang/echoerror
How to use
  • Use by Standard HTTP Status Code
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"))
}
  • Use by Custom Code
package main

import (
	"http"
	"github.com/prongbang/goerror"
	"github.com/prongbang/echoerror"
	"github.com/labstack/echo/v4"
)

type CustomError struct {
	goerror.Body
}

// Error implements error.
func (c *CustomError) Error() string {
	return c.Message
}

func NewCustomError() error {
	return &CustomError{
		Body: goerror.Body{
			Code:    "CUS001",
			Message: "Custom 001",
		},
	}
}

type customResponse struct {
}

// Response implements response.Custom.
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()

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Custom *Custom
	I18n   *I18n
}

type Custom

type Custom interface {
	Response(ctx echo.Context, err error) error
}

type HttpResponse

type HttpResponse interface {
	Response(err error) error
}

type I18n

type I18n struct {
	Enabled  bool
	Localize func(c echo.Context, code string) (string, error)
}

type Response

type Response interface {
	With(c echo.Context) HttpResponse
}

func New

func New(config ...*Config) Response

Jump to

Keyboard shortcuts

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