errors

package module
v1.0.0-alpha-f4ca6f151... Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

errors

Golang errors library.

About The Project

Library defines common mechanism for business error instantiation and handling.

Features:

  • Stack tracing
  • JSON/XML serialization/deserialization
  • Custom fields

Also, package defines most popular error templates:

  • ValidationError
  • BlockingLink
  • ChecksumError
  • Unauthorized
  • Forbidden
  • NotFound
  • Timeout
  • AlreadyExists
  • AlreadyInProgress
  • IllegalState
  • PreconditionFailed
  • PreconditionRequired
  • ToManyRequests
  • InternalError
  • NotImplemented

Usage

Define error template:

var CustomError = errors.Template{
	Code:    "MyCustomError",
	Message: Message("Some user-friendly description"),
	Params: errors.Params{
		errors.Param{Name: "Param1": Value: "param1"},
	},
}

Instantiate error:

...
if condition {
	return errors.New(CustomError, errors.Param{Name: "Param2": Value: "param2"})
}
...

Similar projects

License

Errors is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Contact

  • Email: cherkashin.evgeny.viktorovich@gmail.com
  • Telegram: @evgeny_cherkashin

Documentation

Index

Constants

View Source
const CodeChecksumError = "ChecksumError"

Variables

View Source
var AlreadyExists = Template{
	Code: CodeAlreadyExists,
	Message: func(params map[string]any) string {
		resource, found := params[keyResource]
		if !found {
			return "Resource already exists"
		}
		return fmt.Sprintf("%s already exists", resource)
	},
	Params: Params{},
}
View Source
var AlreadyInProgress = Template{
	Code: CodeAlreadyInProgress,
	Message: func(params map[string]any) string {
		return "Already in progress"
	},
	Params: Params{},
}
View Source
var BlockingLink = Template{
	Code: CodeBlockingLink,
	Message: func(params map[string]any) string {
		return "There are links to this resource"
	},
	Params: Params{},
}
View Source
var ChecksumError = Template{
	Code: CodeChecksumError,
	Message: func(params map[string]any) string {
		return "Checksum does not match"
	},
	Params: Params{},
}
View Source
var Forbidden = Template{
	Code: CodeForbidden,
	Message: func(params map[string]any) string {
		return fmt.Sprintf("Forbidden")
	},
	Params: Params{},
}
View Source
var IllegalState = Template{
	Code: CodeIllegalState,
	Message: func(params map[string]any) string {
		reason, found := params[keyReason]
		if !found {
			return "Illegal state"
		}
		return fmt.Sprintf("Illegal state: %s", reason)
	},
	Params: Params{},
}
View Source
var InternalError = Template{
	Code: CodeInternalError,
	Message: func(params map[string]any) string {
		return "Internal error"
	},
	Params: Params{},
}
View Source
var NotFound = Template{
	Code: CodeNotFound,
	Message: func(params map[string]any) string {
		resource, found := params[keyResource]
		if !found {
			return "Resource not found"
		}
		return fmt.Sprintf("%s not found", resource)
	},
	Params: Params{},
}
View Source
var NotImplemented = Template{
	Code: CodeNotImplemented,
	Message: func(params map[string]any) string {
		return "Not implemented"
	},
	Params: Params{},
}
View Source
var PreconditionFailed = Template{
	Code: CodePreconditionFailed,
	Message: func(params map[string]any) string {
		precondition, found := params[keyPrecondition]
		if !found {
			return "Precondition failed"
		}
		return fmt.Sprintf("Precondition %s failed", precondition)
	},
	Params: Params{},
}
View Source
var PreconditionRequired = Template{
	Code: CodePreconditionFailed,
	Message: func(params map[string]any) string {
		precondition, found := params[keyPrecondition]
		if !found {
			return "Precondition required"
		}
		return fmt.Sprintf("Precondition %s required", precondition)
	},
	Params: Params{},
}
View Source
var Timeout = Template{
	Code:    CodeTimeout,
	Message: Message(`Timeout`),
	Params:  Params{},
}
View Source
var ToManyRequests = Template{
	Code:    CodeToManyRequests,
	Message: Message("To many request, please reduce your requests rate"),
}
View Source
var Unauthorized = Template{
	Code: CodeUnauthorized,
	Message: func(params map[string]any) string {
		return fmt.Sprintf("Unauthorized")
	},
	Params: Params{},
}
View Source
var ValidationError = Template{
	Code: CodeValidationError,
	Message: func(params map[string]any) string {
		resource, found := params[keyResource]
		if !found {
			return "Resource validation error"
		}
		return fmt.Sprintf("%s validation error", resource)
	},
	Params: Params{},
}

Functions

func Configure

func Configure(f func(*Config))

func GetId

func GetId(e Error) (string, bool)

func GetPrecondition

func GetPrecondition(e Error) (string, bool)

func GetReason

func GetReason(e Error) (string, bool)

func GetResource

func GetResource(e Error) (string, bool)

func GetValidationErrors

func GetValidationErrors(e Error) (map[string]string, bool)

func Is

func Is(err error, template Template) bool

func Message

func Message(str string) func(params map[string]any) string

func New

func New(template Template, params ...Param) error

func Wrap

func Wrap(err error, template Template, params ...Param) error

Types

type Code

type Code string
const CodeAlreadyExists Code = "AlreadyExists"
const CodeAlreadyInProgress Code = "AlreadyInProgress"
const CodeBlockingLink Code = "BlockingLink"
const CodeForbidden Code = "Forbidden"
const CodeIllegalState Code = "IllegalState"
const CodeInternalError Code = "InternalError"
const CodeNotFound Code = "NotFound"
const CodeNotImplemented Code = "NotImplemented"
const CodePreconditionFailed Code = "PreconditionFailed"
const CodePreconditionRequired Code = "PreconditionRequired"
const CodeTimeout Code = "Timeout"
const CodeToManyRequests Code = "ToManyRequests"
const CodeUnauthorized Code = "Unauthorized"
const CodeValidationError Code = "ValidationError"

type Config

type Config struct {
	CollectStackTrace bool

	IsPrivateParam func(name string) bool

	MarshalJsonKey     func(name string) string
	MarshalJsonParam   map[string]func(value any) ([]byte, error)
	UnmarshalJsonKey   func(name string) string
	UnmarshalJsonParam map[string]func(data []byte) (any, error)

	MarshalXMLKey     func(name string) string
	MarshalXmlParam   map[string]func(en *xml.Encoder, start xml.StartElement, value any) error
	UnmarshalXMLKey   func(name string) string
	UnmarshalXmlParam map[string]func(d *xml.Decoder, start xml.StartElement) (any, error)

	MarshalCause      bool
	MarshalStackTrace bool
}

type Error

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

func Cast

func Cast(err error) *Error

func (*Error) Cause

func (e *Error) Cause() error

func (*Error) Code

func (e *Error) Code() Code

func (*Error) Error

func (e *Error) Error() string

func (*Error) Get

func (e *Error) Get(key string) any

func (Error) MarshalJSON

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

func (Error) MarshalXML

func (e Error) MarshalXML(en *xml.Encoder, start xml.StartElement) error

func (*Error) Params

func (e *Error) Params() Params

func (*Error) StackTrace

func (e *Error) StackTrace() StackTrace

func (*Error) UnmarshalJSON

func (e *Error) UnmarshalJSON(bytes []byte) error

func (*Error) UnmarshalXML

func (e *Error) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Frame

type Frame uintptr

func (Frame) File

func (f Frame) File() string

func (Frame) Func

func (f Frame) Func() string

func (Frame) Line

func (f Frame) Line() int

type Param

type Param struct {
	Name  string
	Value any
}

func WithId

func WithId(id string) Param

func WithPrecondition

func WithPrecondition(precondition string) Param

func WithReason

func WithReason(reason string) Param

func WithResource

func WithResource(resource string) Param

func WithValidationErrors

func WithValidationErrors(errors map[string]string) Param

type Params

type Params []Param

type StackTrace

type StackTrace []Frame

func (StackTrace) String

func (s StackTrace) String() string

type Template

type Template struct {
	Code    Code
	Message func(params map[string]any) string
	Params  Params
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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