zeroerr

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: Unlicense Imports: 2 Imported by: 0

README

Idea.

Packgae idea is to separate static errors, that are used to determine behavior if error happened.

From context-of-error, like dynamic data, trace and/or messages, that, on the other hand, used for logging and debugging.

This is [github.com/rs/zerolog] specific package, so it is not suitable to use with any other logger, or for different purpose.


Usage

Every exported bit are documented very clearly.

return nil, zeroerror.WithMsg(ErrSomethingBadHappened,"details")

zerologger.Debug().Func(TryInsert(err)).Send()

Compatability.

Fully compatable with [errors] package.
All compatable logic covered with succesful tests.


Test,Linter,Coverage.

make lint
make test
make coverage

Coverage 90+%.


Hint.

Any mention of a "context" in this package means messages or values, that stored inside [ZeroError], and not [context.Context].

Documentation

Overview

Packgae idea is to separate static errors, that are used to determine behavior if error happened.

From context-of-error, like dynamic data, trace and/or messages, that, on the other hand, used for logging and debugging.

This is zerolog specific package, so it is not suitable to use with any other logger, or for different purpose.

Compatable with errors package.

Any mention of a "context" in this package means messages or values, that stored inside ZeroError, and not context.Context.

Index

Constants

View Source
const (
	// Default key for first message.
	FirstKey = "root"
	// Defaylt key for string message.
	MessageKey = "msg"
	// Default key for value message.
	ValueKey = "data"
	// Default key for error.
	ErrorKey = "error"

	// Default key when dictionary logged out.
	InsertedKey = "errcontext"
)

Variables

This section is empty.

Functions

func TryInsert

func TryInsert(err error) func(*zerolog.Event)

TryInsert retuned func will add static error, followed by context to zerolog.Event.

Usage: logger.Debug().Func(TryInsert(err)).Send()  .

It is guaranteed that key will be InsertedKey key.

If error is not an ZeroError -- returned function is a no-op.

Types

type ZeroError

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

ZeroError is a container for separately stored static error and logging context.

Any mention of a "context" in this package means messages or values, that stored inside ZeroError, and not context.Context.

func New

func New() *ZeroError

New returns ready to use ZeroError.

Fields:

  • err filed initialized with 0 len string error, not nil.
  • event field initialized with ready-to-use zerolog.Dict(), not nil.

It is better for maintenance and readability to use [With*] functions !

func WithKeyMsg

func WithKeyMsg(err error, key, msg string) *ZeroError

WithKeyMsg adds key-msg pair to newly initialized ZeroError.

If called on already initialized ZeroError then WithKeyMsg just add more info.

It is guaranteed:

  • even if this is the rirst message of the event, key will be used, insted of FirstKey key.

func WithKeyVal

func WithKeyVal(err error, key string, val any) *ZeroError

WithKeyVal adds key-value pair to newly initialized ZeroError.

If called on already initialized ZeroError then WithKeyVal just add more info.

It is guaranteed:

  • even if this is the rirst message of the event, key will be used, insted of FirstKey key.

func WithMsg

func WithMsg(err error, msg string) *ZeroError

WithMsg adds msg to newly initialized ZeroError.

If called on already initialized ZeroError then WithMsg just add more info.

It is guaranteed:

  • if message is first in event -- it will be with FirstKey key.
  • added msg will be with MessageKey key.

func WithVal

func WithVal(err error, val any) *ZeroError

WithVal adds value to newly initialized ZeroError.

Be careful, only exported fields of a struct will be added !!!

If called on already initialized ZeroError then WithVal just add more info.

It is guaranteed:

  • if message is first in event -- it will be with FirstKey key.
  • added val will be with ValueKey key.

func (*ZeroError) As

func (ze *ZeroError) As(target any) bool

As -- errors package compatibility.

func (ZeroError) Error

func (ze ZeroError) Error() string

Error implements error interface.

func (*ZeroError) Insert

func (ze *ZeroError) Insert(e *zerolog.Event)

Insert will add static error, followed by context to zerolog.Event.

func (*ZeroError) Is

func (ze *ZeroError) Is(err error) bool

Is -- errors package compatibility.

func (*ZeroError) Join

func (ze *ZeroError) Join(err error) *ZeroError

Join like errors package.

Pipelining supported.

func (*ZeroError) Unwrap

func (ze *ZeroError) Unwrap() error

Unwrap -- errors package compatibility.

Jump to

Keyboard shortcuts

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