errorex

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: MIT Imports: 3 Imported by: 2

README

ErrorEX

ErrorEX is a custom Go library for error handling that provides custom error types with additional details. It enables greater control over the creation, handling, and checking of errors, making it easier to identify and resolve issues in complex systems.

Motivation

The motivation behind Errorex is to improve error management in Go by providing a means to associate unique codes and detailed messages with errors. This helps to standardize error responses in APIs and backend services, making the errors more expressive and easier to track.

Usage

To use Errorex, you need to register error codes and their corresponding descriptions. After registration, you can create new custom errors and check if an error matches a particular error code.

Advantages

  • Error Standardization: Provides a uniform structure for errors in Go applications.
  • Additional Details: Allows associating additional details with errors for better diagnostics.
  • Prevention of Duplicate Codes: Ensures that each error has a unique code.

Possible Pitfalls

  • Panic in Cases of Unregistered Error: The librarywill panic if you try to create an error with an unregistered code.
  • Strict Detail Type: When create an error, the detail type must match the registered detail type.
Installation
go get github.com/fkmatsuda/errorex
Example
package main

import (
    "fmt"
    "github.com/fkmatsuda/errorex"
)

init() {
    errorex.Register("E001", "Invalid input", ErrInvalidInputDetail{})
}

type ErrInvalidInputDetail struct {
    Field string `json:"field"`
    Value string `json:"value"`
}

func main() {
    err := errorex.New("E001", ErrInvalidInputDetail{Field: "name", Value: "John"})

    if errorex.Is(err, "E001") {
        fmt.Println("Invalid input error")
        fmt.Println(err)
        return

        // Output:
        // Invalid input error
        // {
        //   "code": "E001",
        //   "message": "Invalid input",
        //   "detail": {
        //     "field": "name",
        //     "value": "John"
        //   }
        // }
    }

    fmt.Println("Other error")

}

License

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

Documentation

Index

Constants

View Source
const (
	// ErrCodeUnknownError is the errorex code for when the errorex code is unknown
	ErrCodeUnknownError = "errorex.000"
	// ErrCodeNotRegistered is the errorex code for when the errorex code is not registered
	ErrCodeNotRegistered = "errorex.001"
	// ErrCodeAlreadyRegistered is the errorex code for when the errorex code is already registered
	ErrCodeAlreadyRegistered = "errorex.002"
	// ErrDetailTypeMismatch is the errorex code for when the errorex detail type does not match the registered type
	ErrDetailTypeMismatch = "errorex.003"
)

Variables

This section is empty.

Functions

func Is

func Is(err error, code string) bool

Is checks if the errorex is of type EX and if the code matches

func RegisterErrorCode

func RegisterErrorCode[T any](code string, description string, detail T)

RegisterErrorCode registers errorex codes to prevent repeats

Types

type BaseErrorConverter

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

BaseErrorConverter provides a basic implementation of the ErrorConverter interface, holding a reference to the next handler in the chain.

func (*BaseErrorConverter) ConvertError

func (b *BaseErrorConverter) ConvertError(err error) EX

ConvertError in BaseErrorConverter should be overridden by concrete handlers. This default implementation delegates the conversion task to the next handler.

func (*BaseErrorConverter) SetNext

func (b *BaseErrorConverter) SetNext(next ErrorConverter)

SetNext sets the next handler in the chain.

type EX

type EX interface {
	error
	// Code is the errorex code
	Code() string
	// Detail returns the detail of the error
	Detail() any
}

EX is a custom errorex type with additional information

func New

func New[T any](code string, detail T) EX

New returns a new errorex.EX Code is the errorex code. Detail is the errorex detail.

type ErrorConstructor

type ErrorConstructor[T any] func(code string, detail T) EX

ErrorConstructor is a function that creates an errorEX

type ErrorConverter

type ErrorConverter interface {
	// ConvertError tries to convert an error into another error compatible with errorex.EX.
	// If it cannot handle the conversion, it attempts to delegate the task to the next handler in the chain.
	// It returns an error which will be nil if conversion is not possible by any handler.
	ConvertError(err error) EX

	// SetNext sets the next handler in the chain.
	SetNext(next ErrorConverter)
}

ErrorConverter defines the interface for handlers in the chain of responsibility that attempt to convert an error into an error compatible with errorex.EX. If the handler cannot convert the error, it delegates the task to the next handler in the chain. A large number of handlers in the chain of responsibilities can cause delays in the process, so I do not recommend having a single chain for the entire application. Instead, have multiple chains with handlers that make sense in their contexts.

func BuildErrorConverterChain added in v1.3.0

func BuildErrorConverterChain(converters ...ErrorConverter) ErrorConverter

BuildErrorConverterChain creates a chain of error converters. The chain starts ExErrorConverter, then the provided converters, and ends with UnknownErrorConverter.

func NewEXErrorConverter added in v1.2.0

func NewEXErrorConverter(next ErrorConverter) ErrorConverter

NewEXErrorConverter creates a new exErrorConverter this converter will check if the error passed implements EX, and if so, returns the error itself, otherwise it attempts to delegate the conversion to the next handler in the chain. This converter should be used as the first handler in the chain.

func NewUnknownErrorConverter

func NewUnknownErrorConverter() ErrorConverter

NewUnknownErrorConverter creates a new unknownErrorConverter this converter will convert any error into an unknown error with the message of the error as the detail. This converter should be used as the last handler in the chain.

type ErrorEXDetail

type ErrorEXDetail struct {
	Code string `json:"code"`
}

ErrorEXDetail is the type of the detail of an errorex

type ErrorEXDetailTypeMismatch

type ErrorEXDetailTypeMismatch struct {
	ExpectedType string `json:"expectedType"`
	ActualType   string `json:"actualType"`
}

ErrorEXDetailTypeMismatch is the type of the detail of an errorex

type UnknownErrorDetail

type UnknownErrorDetail struct {
	Detail string `json:"detail"`
}

UnknownErrorDetail is the type of the detail of an unknown errorex

Jump to

Keyboard shortcuts

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