ero

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package ero provides a way to create error wrapper.

Example Usage

The following is a complete example using ero package

import (
	ero "github.com/phamtai97/go-utils/utils/error"
	"github.com/phamtai97/go-utils/utils/logger"

	"go.uber.org/zap"
)

func doError() error {
	return ero.New("Failed to open file")
}

func main() {
	logger.InitProduction("")

	// err := ero.Newf("Not found file")
	err := ero.New("Failed to open file")

	// Add context
	errA := err.AddContext("Call by component A")
	errB := errA.AddContext("Call by component B")
	errC := errB.AddContextf("Call by component C %d", 123)

	// Create error by pre-existing error
	errWrap := ero.Wrap(err)

	// Add stackstrace
	errWrapA := errWrap.AddStackTrace("Call by component wrapper A")
	errWrapB := errWrapA.AddStackTrace("Call by component wrapperB")
	errWrapC := errWrapB.AddStackTracef("Call by component wrapper C %d", 123)

	// Logger error
	logger.Error("Test", zap.Error(errC.Detail()))
	logger.Error("Test", zap.Error(errWrapC.Detail()))
	logger.Error("Test", zap.Error(errC))
	logger.Error("Test", zap.Error(errWrapC))
	logger.Error("Test", zap.String("Failed", errA.Error()))
	logger.Error("Test", zap.String("Failed", errWrapA.Error()))

	// Check error with Is
	if errC.Is(errA) {
		logger.Info("Error C is error A", zap.String("ErrorC", errC.Error()), zap.String("ErrorA", errA.Error()))
	}

	if !errWrap.Is(err) {
		logger.Info("errWrap is not err", zap.String("errWrap", errWrap.Error()), zap.String("err", err.Error()))
	}

	// Get root cause of error
	logger.Error("Root cause", zap.String("Root cause", errC.RootCause().Error()))
	logger.Error("Root cause", zap.String("Root cause", errC.RootCauseStr()))
	logger.Error("Root cause", zap.Error(errC.RootCause().Detail()))

	// Case error to ErrorWrapper
	errCast := doError().(*ero.ErrorWrapper)
	logger.Error("Cast error to ErrorWrapper", zap.Error(errCast.Detail()))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorWrapper

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

ErrorWrapper wraps any error for ease of use.

func New

func New(message string) *ErrorWrapper

New returns the *ErrorWrapper with message.

func Newf

func Newf(format string, args ...interface{}) *ErrorWrapper

Newf returns the *ErrorWrapper with formated message.

func Wrap

func Wrap(err error) *ErrorWrapper

Wrap returns the *ErrorWrapper with exist error.

func (*ErrorWrapper) AddContext

func (e *ErrorWrapper) AddContext(message string) *ErrorWrapper

AddContext returns *ErrorWrapper containing error has been added context.

func (*ErrorWrapper) AddContextf

func (e *ErrorWrapper) AddContextf(format string, args ...interface{}) *ErrorWrapper

AddContextf returns *ErrorWrapper containing error has been added context with format message.

func (*ErrorWrapper) AddStackTrace

func (e *ErrorWrapper) AddStackTrace(message string) *ErrorWrapper

AddStackTrace returns *ErrorWrapper containing error has been added stackstrace.

func (*ErrorWrapper) AddStackTracef

func (e *ErrorWrapper) AddStackTracef(format string, args ...interface{}) *ErrorWrapper

AddStackTracef returns *ErrorWrapper containing error has been added stackstrace with format message.

func (*ErrorWrapper) Detail

func (e *ErrorWrapper) Detail() error

Detail return the error in the ErrorWrapper.

func (*ErrorWrapper) Error

func (e *ErrorWrapper) Error() string

Error returns the error string.

func (*ErrorWrapper) Is

func (e *ErrorWrapper) Is(target *ErrorWrapper) bool

Is checks current error is targer error.

func (*ErrorWrapper) RootCause

func (e *ErrorWrapper) RootCause() *ErrorWrapper

RootCause returns *ErrorWrapper that contains the root cause of error.

func (*ErrorWrapper) RootCauseStr

func (e *ErrorWrapper) RootCauseStr() string

RootCauseStr returns the root cause string.

Jump to

Keyboard shortcuts

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