errors

package module
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: BSD-3-Clause Imports: 7 Imported by: 2

README

errors Go Reference

Better error handling for REST/GraphQL servers.

User Story

As a developer, I want to avoid exposing the application errors to the user, so that users are not exposed to the internal errors.

Context

Most REST applications (as well as GraphQL) requires mapping errors and returning them to the client. This errors are normally tied to the HTTP status code too, and hence requires mapping from domain errors to the corresponding status code

However, the current error package has two main limitations

  • it does not provide sufficient context for categorizing errors
  • you cannot pass values through the errors

WIP

Error message

Functional requirements

  • system should not expose internal error message
  • system should contain detailed explanation on where and why the error occured
  • for client facing side, system should return human readable error message
  • for internal side, system should provide stack
  • system should provide hint on how to solve the problem (or if it could be solved)

TODO

https://wix-ux.com/when-life-gives-you-lemons-write-better-error-messages-46c5223e1a2f

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKind   = errors.New("errors: kind is invalid")
	ErrCodeNotFound  = errors.New("errors: error code not found")
	ErrDuplicateCode = errors.New("errors: duplicate error code")
)

Errors

View Source
var (
	Get          = bundle.Get
	Add          = bundle.Add
	AddKinds     = bundle.AddKinds
	MustAddKinds = bundle.MustAddKinds
	Len          = bundle.Len
	Load         = bundle.Load
	Iter         = bundle.Iter
	MustLoad     = bundle.MustLoad
	LoadFS       = bundle.LoadFS
	MustLoadFS   = bundle.MustLoadFS
)

Functions

This section is empty.

Types

type Bundle

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

func NewBundle

func NewBundle() *Bundle

func (*Bundle) Add added in v0.8.0

func (b *Bundle) Add(kind Kind, code Code, message string, tags ...string) *Error

func (*Bundle) AddKinds added in v0.12.0

func (b *Bundle) AddKinds(kinds ...Kind) error

func (*Bundle) Get added in v0.8.0

func (b *Bundle) Get(code Code) *Error

func (*Bundle) Iter added in v0.12.0

func (b *Bundle) Iter(fn func(code Code, err *Error) error) error

func (*Bundle) Len added in v0.12.0

func (b *Bundle) Len() int

func (*Bundle) Load

func (b *Bundle) Load(errorBytes []byte, unmarshal unmarshalFn) error

func (*Bundle) LoadFS added in v0.12.0

func (b *Bundle) LoadFS(fs embed.FS, unmarshal unmarshalFn) error

func (*Bundle) MustAddKinds added in v0.12.1

func (b *Bundle) MustAddKinds(kinds ...Kind) bool

func (*Bundle) MustLoad

func (b *Bundle) MustLoad(errorBytes []byte, unmarshal unmarshalFn) bool

func (*Bundle) MustLoadFS added in v0.12.0

func (b *Bundle) MustLoadFS(fs embed.FS, unmarshal unmarshalFn) bool

type Code

type Code string

type Error

type Error struct {
	Kind    string
	Code    string
	Message string
	Params  any
	Tags    []string
	Cause   error
}

Error represents an error.

func (*Error) Copy added in v0.8.0

func (e *Error) Copy() *Error

func (Error) Error

func (e Error) Error() string

Error fulfills the error interface.

func (*Error) Is

func (e *Error) Is(target error) bool

Is satisfies the error interface.

func (*Error) MarshalJSON added in v0.13.1

func (e *Error) MarshalJSON() ([]byte, error)

func (Error) String added in v0.4.0

func (e Error) String() string

func (*Error) Unwrap added in v0.10.0

func (e *Error) Unwrap() error

func (*Error) WithParams added in v0.6.0

func (e *Error) WithParams(params any) *Error

func (*Error) WithTag added in v0.6.0

func (e *Error) WithTag(tags ...string) *Error

func (*Error) Wrap added in v0.10.0

func (e *Error) Wrap(cause error) *Error

type Kind

type Kind string

type PartialError

type PartialError[T any] struct {
	// contains filtered or unexported fields
}

func ToPartial added in v0.8.0

func ToPartial[T any](err *Error) *PartialError[T]

func (*PartialError[T]) Unwrap added in v0.8.0

func (p *PartialError[T]) Unwrap() *Error

func (*PartialError[T]) WithParams added in v0.8.0

func (p *PartialError[T]) WithParams(t T) *Error

func (*PartialError[T]) WithTag added in v0.8.0

func (p *PartialError[T]) WithTag(tags ...string) *PartialError[T]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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