calm

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: Unlicense Imports: 7 Imported by: 1

README

Calm

Dual mode error and result handling library for Golang 1.20+

Documentation

Index

Constants

View Source
const (
	// EConfig A configuration error is detected
	EConfig = 1
	// ERequest Request is not able to be served due to procedure prerequisites not satisfied
	ERequest = 2
	// EDenied The requesting party is not granted the access to this resource, or the grant has expired
	EDenied = 3
	// EInternal The request cannot be completed due to an unspecified internal error
	EInternal = 4

	// EStgNone Unable to find given storage record
	EStgNone = 64
	// EStgFull Unable to complete a storage write request due to short of storage quota
	EStgFull = 65
	// EStgQueue Storage data backlog too long
	EStgQueue = 66
	// EStgLost Storage handle lost due to max timeout exceeded during operation
	EStgLost = 67
	// EStgFail Generic storage failure
	EStgFail = 68

	// ENetNone Unable to find give data interface/connection
	ENetNone = 96
	// ENetEarly Data interface/connection not ready
	ENetEarly = 97
	// ENetDown Data interface/connection is down
	ENetDown = 98
	// ENetQueue Data interface/connection backlog too long
	ENetQueue = 99
	// ENetRetry Data request is unable to be completed at the time, retry by user is suggested
	ENetRetry = 100
	// ENetMaxRetry Data request is unable to be completed after max configured automatic retries
	ENetMaxRetry = 101
	// ENetLost Data interface/connection max timeout exceeded
	ENetLost = 102
	// ENetFail Generic data interface/connection failure
	ENetFail = 103

	// EResNone Requested resource not found
	EResNone = 128
	// EResAuth Resource requested authorization not satisfied
	EResAuth = 129
	// EResRetry Resource not available at this time. User retry is suggested
	EResRetry = 130
	// EResGone Resource permanently removed
	EResGone = 131
	// EResFail Resource generic failure
	EResFail = 132

	// ETimeout Action timeout exceeded
	ETimeout = 512
	// ECancel Action cancelled
	ECancel = 513
	// EBacklog Task backlog limit exceeded
	EBacklog = 514
)

Variables

View Source
var (
	AsEConfig   = AsSys(EConfig)
	AsERequest  = AsSys(ERequest)
	AsEDenied   = AsSys(EDenied)
	AsEInternal = AsSys(EInternal)

	AsEStgNone  = AsSys(EStgNone)
	AsEStgFull  = AsSys(EStgFull)
	AsEStgQueue = AsSys(EStgQueue)
	AsEStgLost  = AsSys(EStgLost)
	AsEStgFail  = AsSys(EStgFail)

	AsENetNone     = AsSys(ENetNone)
	AsENetEarly    = AsSys(ENetEarly)
	AsENetDown     = AsSys(ENetDown)
	AsENetQueue    = AsSys(ENetQueue)
	AsENetRetry    = AsSys(ENetRetry)
	AsENetMaxRetry = AsSys(ENetMaxRetry)
	AsENetLost     = AsSys(ENetLost)
	AsENetFail     = AsSys(ENetFail)

	AsEResNone  = AsSys(EResNone)
	AsEResAuth  = AsSys(EResAuth)
	AsEResRetry = AsSys(EResRetry)
	AsEResGone  = AsSys(EResGone)
	AsEResFail  = AsSys(EResFail)

	AsETimeout = AsSys(ETimeout)
	AsECancel  = AsSys(ECancel)
	AsEBacklog = AsSys(EBacklog)
)
View Source
var (
	FullPrint      = &StackPrintOptions{Formatter: _DefaultPrint, TrimStack: false}
	TrimFullPrint  = &StackPrintOptions{Formatter: _DefaultPrint, TrimStack: true}
	ShortPrint     = &StackPrintOptions{Formatter: _DefaultShortPrint, TrimStack: false}
	TrimShortPrint = &StackPrintOptions{Formatter: _DefaultShortPrint, TrimStack: true}
)

Functions

func AddErrType added in v0.0.2

func AddErrType(supply func(id uint32) IErrType) uint32

func AsSys added in v0.0.2

func AsSys(code uint32) func(Error)

func MakeErrCode added in v0.0.2

func MakeErrCode(typeCode uint32, errCode uint32) uint64

func PrintCleans added in v0.0.2

func PrintCleans(error Error) string

func PrintDetails added in v0.0.2

func PrintDetails(error Error, option *StackPrintOptions) string

func Throw

func Throw(err error)

Throw Raise an error by calling panic

func ThrowClean added in v0.0.2

func ThrowClean(err uint64, msg string)

ThrowClean Equivalent of Throw(ErrClean(err, msg))

func ThrowCleanN added in v0.0.2

func ThrowCleanN(nested Error, err uint64, msg string)

ThrowCleanN Equivalent of Throw(ErrCleanN(nested, err, msg))

func ThrowCode added in v0.0.2

func ThrowCode(err uint64)

ThrowCode Equivalent of Throw(ErrCode(err))

func ThrowCodeN added in v0.0.2

func ThrowCodeN(nested Error, err uint64)

ThrowCodeN Equivalent of Throw(ErrCodeN(nested, err))

func ThrowDetail added in v0.0.2

func ThrowDetail(err uint64, clean, detail string)

ThrowDetail Equivalent of Throw(ErrDetail(err, clean, detail))

func ThrowDetailN added in v0.0.2

func ThrowDetailN(nested Error, err uint64, clean, detail string)

ThrowDetailN Equivalent of Throw(ErrDetailN(nested, err, clean, detail))

func ThrowErr added in v0.0.2

func ThrowErr(err uint64, sys error)

ThrowErr Equivalent of Throw(ErrClean(err, sys))

func ThrowErrN added in v0.0.2

func ThrowErrN(nested Error, err uint64, sys error)

ThrowErrN Equivalent of Throw(ErrCleanN(nested, err, sys))

func ThrowStringer added in v0.0.2

func ThrowStringer(err uint64, msg string, meta fmt.Stringer)

ThrowStringer Equivalent of Throw(ErrDetail(err, msg, meta))

func ThrowStringerN added in v0.0.2

func ThrowStringerN(nested Error, err uint64, msg string, meta fmt.Stringer)

ThrowStringerN Equivalent of Throw(ErrDetailN(nested, err, msg, meta))

func Unreachable

func Unreachable[T any]() T

Unreachable Mark the current return path as unreachable. If an Unreachable is evaluated, an EInternal will be raised

func Unreachable2 added in v0.0.2

func Unreachable2[T any, U any]() (T, U)

Unreachable2 Mark the current return path as unreachable. If an Unreachable2 is evaluated, an EInternal will be raised

func Unreachable3 added in v0.0.2

func Unreachable3[T any, U any, V any]() (T, U, V)

Unreachable3 Mark the current return path as unreachable. If an Unreachable3 is evaluated, an EInternal will be raised

Types

type Defer added in v0.0.2

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

func ErrDefer added in v0.0.2

func ErrDefer(err error) Defer

func RunAsync added in v0.0.2

func RunAsync(exec func()) (ret Defer)

func UnsafeMakeDefer added in v0.0.2

func UnsafeMakeDefer() (ret Defer)

func ValDefer added in v0.0.2

func ValDefer() Defer

func (*Defer) Dump added in v0.0.2

func (c *Defer) Dump() Error

func (*Defer) Fold added in v0.0.2

func (c *Defer) Fold(onError func(Error))

func (*Defer) Get added in v0.0.2

func (c *Defer) Get()

func (*Defer) UnsafeApplyDefer added in v0.0.2

func (c *Defer) UnsafeApplyDefer(exec func())

func (*Defer) Unwrap added in v0.0.2

func (c *Defer) Unwrap(onError func(Error))

func (*Defer) Wait added in v0.0.2

func (c *Defer) Wait()

type DeferT added in v0.0.2

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

func ErrDeferT added in v0.0.2

func ErrDeferT[T any](err error) DeferT[T]

func RunAsyncT added in v0.0.2

func RunAsyncT[T any](exec func() T) (ret DeferT[T])

func UnsafeMakeDeferT added in v0.0.2

func UnsafeMakeDeferT[T any]() (ret DeferT[T])

func ValDeferT added in v0.0.2

func ValDeferT[T any](v T) DeferT[T]

func (*DeferT[T]) Dump added in v0.0.2

func (c *DeferT[T]) Dump() (T, Error)

func (*DeferT[T]) Fold added in v0.0.2

func (c *DeferT[T]) Fold(onError func(Error) T) T

func (*DeferT[T]) Get added in v0.0.2

func (c *DeferT[T]) Get() T

func (*DeferT[T]) UnsafeApplyDefer added in v0.0.2

func (c *DeferT[T]) UnsafeApplyDefer(exec func() T)

func (*DeferT[T]) Unwrap added in v0.0.2

func (c *DeferT[T]) Unwrap(onError func(Error)) T

func (*DeferT[T]) Wait added in v0.0.2

func (c *DeferT[T]) Wait()

type Error

type Error interface {
	// TCode : Top error type code
	TCode() uint32
	// ECode : Top error error code
	ECode() uint32
	// Slices : raw []ErrorInfo for all nesting sites
	Slices() []ErrorInfo
	// Error : same as detail, for compatibility with golang built-in error handling
	error
}

func ErrByInfo added in v0.0.2

func ErrByInfo(info ErrorInfo) Error

ErrByInfo Create a new calm.Error with its root slice reference set to info. The corresponding OnErrRoot of the registered calm.IErrType will be called. Stack of the caller will be captured if OnErrRoot reports true. This function should never fail or panic.

func ErrClean added in v0.0.2

func ErrClean(err uint64, msg string) Error

ErrClean Equivalent of ErrByInfo(InfoClean(err, msg))

func ErrCleanN added in v0.0.2

func ErrCleanN(nested Error, err uint64, msg string) Error

ErrCleanN Equivalent of ErrNestByInfo(nested, InfoClean(err, msg))

func ErrCode added in v0.0.2

func ErrCode(err uint64) Error

ErrCode Equivalent of ErrByInfo(InfoCode(err))

func ErrCodeN added in v0.0.2

func ErrCodeN(nested Error, err uint64) Error

ErrCodeN Equivalent of ErrNestByInfo(nested, InfoCode(err))

func ErrDetail added in v0.0.2

func ErrDetail(err uint64, clean, detail string) Error

ErrDetail Equivalent of ErrByInfo(InfoDetail(err, clean, detail))

func ErrDetailN added in v0.0.2

func ErrDetailN(nested Error, err uint64, clean, detail string) Error

ErrDetailN Equivalent of ErrNestByInfo(nested, InfoInfoDetail(err, clean, detail))

func ErrErr added in v0.0.2

func ErrErr(err uint64, nested error) Error

ErrErr construct an Error that nests the given error

func ErrErrN added in v0.0.2

func ErrErrN(nested Error, err uint64, sys error) Error

ErrErrN Equivalent of ErrNestByInfo(nested, InfoErr(err, sys))

func ErrNestByInfo added in v0.0.2

func ErrNestByInfo(nested Error, info ErrorInfo) Error

ErrNestByInfo Return a calm.Error which contains the given nested calm.Error, and with its top level slice set to `info`. If the top-level slice is the exact same as `info` and no stack trace is attached, `nested` is returned. If `info` is returned by calm.InfoCode and the its TCode() and ECode() match the top slice, `nested` is returned. Otherwise, the corresponding OnErrNest of the registered calm.IErrType will be called. Stack of the caller will be captured it `nested` has stack captured or OnErrNest reports true. This function should never fail or panic.

func ErrStringer added in v0.0.2

func ErrStringer(err uint64, msg string, meta fmt.Stringer) Error

ErrStringer Equivalent of ErrByInfo(InfoDetail(err, msg, meta))

func ErrStringerN added in v0.0.2

func ErrStringerN(nested Error, err uint64, msg string, meta fmt.Stringer) Error

ErrStringerN Equivalent of ErrNestByInfo(nested, InfoStringer(err, msg, meta))

type ErrorInfo added in v0.0.2

type ErrorInfo interface {
	// TCode : error type code
	TCode() uint32
	// ECode : error error code
	ECode() uint32
	// Detail : message for internal systems, e.g. logging
	Detail() string
	// Clean : sanitized short message for passing to the user as error detail
	Clean() string
	// Meta : optional metadata object for internal data
	Meta() any
}

func InfoClean added in v0.0.2

func InfoClean(err uint64, msg string) ErrorInfo

InfoClean construct a ErrorInfo that represents an error code with a sanitized message

func InfoCode added in v0.0.2

func InfoCode(err uint64) ErrorInfo

InfoCode construct a ErrorInfo that represents an error code

func InfoDetail added in v0.0.2

func InfoDetail(err uint64, clean, detail string) ErrorInfo

InfoDetail construct a ErrorInfo that represents an error code with a sanitized message and a full message

func InfoErr added in v0.0.2

func InfoErr(err uint64, sys error) ErrorInfo

InfoErr construct a ErrorInfo that represents an error code with a nested error

func InfoStringer added in v0.0.2

func InfoStringer(err uint64, msg string, meta fmt.Stringer) ErrorInfo

InfoStringer construct a ErrorInfo that represents an error code with a sanitized message and a stringer object

type IErrType added in v0.0.2

type IErrType interface {
	Type() uint32
	// OnErrNest callback on a nested error is created
	// err: the nested error
	// return: if stacktrace should be included
	OnErrNest(err Error) bool
	// OnErrRoot callback on a new error is created
	// err: the newly created error
	// return: if stacktrace should be included
	OnErrRoot(err Error) bool
	// ErrName : name tag for a specific error code
	ErrName(err uint32) string
	// DefaultMsg get a default clean message for a specific code
	DefaultMsg(err uint32) string
}

type Result

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

func Assert

func Assert(condition bool, message ...string) Result

func ErrResult

func ErrResult(err error) Result

func ResResultU added in v0.0.2

func ResResultU[U any](res ResultT[U]) Result

func Run

func Run(exec func()) (ret Result)

func ValResult

func ValResult() Result

func Wrap

func Wrap(err error) Result

func (Result) Dump

func (c Result) Dump() Error

func (Result) Fold

func (c Result) Fold(onError func(Error))

func (Result) Get

func (c Result) Get()

func (Result) Unwrap

func (c Result) Unwrap(onError func(Error))

type ResultT

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

func ErrResultT

func ErrResultT[T any](err error) ResultT[T]

func RunT

func RunT[T any](exec func() T) (ret ResultT[T])

func ValResultT

func ValResultT[T any](v T) ResultT[T]

func WrapT

func WrapT[T any](v T, err error) ResultT[T]

func (ResultT[T]) Dump

func (c ResultT[T]) Dump() (T, Error)

func (ResultT[T]) Fold

func (c ResultT[T]) Fold(onError func(Error) T) T

func (ResultT[T]) Get

func (c ResultT[T]) Get() T

func (ResultT[T]) Unwrap

func (c ResultT[T]) Unwrap(onError func(Error)) T

type StackFrame added in v0.0.2

type StackFrame struct {
	Func, File string
	Line       int
	PC         uintptr
}

type StackPrintOptions added in v0.0.2

type StackPrintOptions struct {
	Formatter func(frame *StackFrame) string
	TrimStack bool
}

Jump to

Keyboard shortcuts

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