throw

package
v0.0.0-...-05bc493 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

View Source
const NoStackTrace int = math.MaxInt32
View Source
const StackTracePrefix = "Stack trace:"

Variables

This section is empty.

Functions

func Blame

func Blame(msg string, description ...interface{}) error

func DeadCanary

func DeadCanary(msg string, description ...interface{}) error

func E

func E(msg string, description ...interface{}) error

E creates an error by the provided message and description. Description can be a log structure

func ES

func ES(msg string, description ...interface{}) error

ES creates an error with stack

func ErrorWithStack

func ErrorWithStack(errChain error) string

func Errorf

func Errorf(format string, args ...interface{}) error

deprecated Errorf must be replaced with either throw.New or with fmt.Errorf

func FailCaller

func FailCaller(msg string, skipFrames int) error

FailCaller creates an error that captures the entry of caller's stack after skipping (skipFrames).

func FailHere

func FailHere(msg string) error

FailHere creates an error that captures the topmost entry of caller's stack.

func Fatal

func Fatal(msg string, description ...interface{}) error

func FindDetail

func FindDetail(value error, target interface{}) bool

func Fraud

func Fraud(msg string, description ...interface{}) error

func IllegalState

func IllegalState() error

IllegalState is to indicate that an internal state of a function/object is incorrect or unexpected This error captures the topmost entry of caller's stack.

func IllegalValue

func IllegalValue() error

IllegalValue is to indicate that an argument provided to a calling function is incorrect This error captures the topmost entry of caller's stack.

func Impossible

func Impossible() error

Impossible is to indicate that the point can never be reached. This error captures the topmost entry of caller's stack.

func IsEqual

func IsEqual(err0, err1 error) bool

IsEqual does panic-safe comparison of error values. Incomparable values will always return false. It uses Is() on both sides

func IsInSystemPanic

func IsInSystemPanic(skipFrames int) bool

func JoinStackText

func JoinStackText(s0 string, s1 StackTrace) string

func LocalBreach

func LocalBreach(msg string, description ...interface{}) error

func MinimizeDebugStack

func MinimizeDebugStack(stackTrace []byte, boundPackage string, includeBoundary bool) []byte

func MinimizePanicStack

func MinimizePanicStack(stackTrace []byte, boundPackage string, includeBoundary bool) []byte

func New

func New(msg string, description ...interface{}) error

New creates an error with the given message text and description. Log structs can be used

func NewDescription

func NewDescription(description interface{}) error

NewDescription creates an error with the given description. Log structs can be used

func NewFmt

func NewFmt(msg string, errorDescription interface{}, fmtFn FormatterFunc) error

func NotImplemented

func NotImplemented() error

NotImplemented is to indicate that a calling function was not yet implemented, but it is expected to be completed soon This error captures the topmost entry of caller's stack.

func Print

func Print(errChain error) string

Print is a convenience wrapper for PrintTo()

func PrintTo

func PrintTo(errChain error, includeStack bool, b *strings.Builder)

PrintTo calls Walk() and builds a full list of errors and corresponding stacks in the chain.

func R

func R(recovered interface{}, prevErr error) error

R takes recovered panic and previous error if any, then wraps them together with current stack Returns (prevErr) when (recovered) is nil. NB! Must be called inside defer, e.g. defer func() { err = R(recover(), err) } ()

func RW

func RW(recovered interface{}, prevErr error, msg string, description ...interface{}) error

RW takes recovered panic, previous error if any, then wraps them together with current stack and message. Returns (prevErr) when (recovered) is nil. Description can be a log structure NB! Must be called inside defer, e.g. defer func() { err = RW(recover(), err, "msg", x) } ()

func RWn

func RWn(recovered interface{}, prevErr error, msg string, details ...interface{}) error

Rn takes recovered panic, previous error if any, then wraps them together with current stack and message. But without a panic mark. NB! Must be called inside defer, e.g. defer func() { err = RWn(recover(), err, "msg", x) } ()

func RemoteBreach

func RemoteBreach(msg string, description ...interface{}) error

func Rn

func Rn(recovered interface{}, prevErr error) error

Rn takes recovered panic and reports it as an error, without a panic mark. Result is wrapped with current stack. NB! Must be called inside defer, e.g. defer func() { err = Rn(recover(), err) } ()

func SetFormatter

func SetFormatter(fn FormatterFunc)

SetFormatter sets a formatter to be applied by NewDescription and New

func Severe

func Severe(severity Severity, msg string, description ...interface{}) error

func TrimStackTrace

func TrimStackTrace(s string) string

func Unsupported

func Unsupported() error

Unsupported is to indicate that a calling function is unsupported intentionally and will remain so for awhile This error captures the topmost entry of caller's stack.

func Violation

func Violation(msg string, description ...interface{}) error

func W

func W(err error, msg string, description ...interface{}) error

W wraps the given error with provided message and details. Returns nil when (err) == nil

func WS

func WS(err error, msg string, description ...interface{}) error

WS wraps the given error as W and adds stack

func Walk

func Walk(errChain error, fn func(error, StackTraceHolder) bool) bool

Walks through the given error chain and call (fn) for each entry, does unwrapping for stack trace data.

func WithDefaultSeverity

func WithDefaultSeverity(err error, s Severity) error

func WithDetails

func WithDetails(predecessor error, details ...interface{}) error

func WithEscalatedSeverity

func WithEscalatedSeverity(err error, s Severity) error

func WithSeverity

func WithSeverity(err error, s Severity) error

func WithStack

func WithStack(err error) error

WithStackExt wraps the error with stack of caller. Nil value will return nil.

func WithStackAndDetails

func WithStackAndDetails(predecessor error, details interface{}) error

func WithStackExt

func WithStackExt(err error, skipFrames int) error

WithStackExt wraps the error with stack with the given number of frames skipped. Nil value will return nil.

func WithStackTop

func WithStackTop(err error) error

WithStackTop wraps the error with stack's topmost entry only. Nil value will return nil. Use this method to augment an error specific to a code location.

func WithStackTopExt

func WithStackTopExt(err error, skipFrames int) error

WithStackTopExt wraps the error with stack's topmost entry after skipping the given number of frames. Nil value will return nil.

func WrapPanic

func WrapPanic(recovered interface{}) error

WrapPanic returns an error for the given recovered panic with a stack trace. The error is also marked as recovered panic. Will return nil for a nil value NB! Should be used inside a defer.

func WrapPanicExt

func WrapPanicExt(recovered interface{}, skipFrames int) error

WrapPanicNoStack returns an error for the given recovered panic with or without a stack trace. When (skipFrames) = NoStackTrace then no stack trace is included. The error is also marked as recovered panic. Will return nil for a nil value

func WrapPanicNoStack

func WrapPanicNoStack(recovered interface{}) error

WrapPanicNoStack returns an error for the given recovered panic without a stack trace. The error is also marked as recovered panic. Will return nil for a nil value NB! Should be used inside a defer.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

deprecated Wrapf must be replaced with throw.W and struct for details

Types

type DeepestStackMode

type DeepestStackMode uint8
const (
	InheritedTrace DeepestStackMode
	SupersededTrace
)

type FormatterFunc

type FormatterFunc func(string, interface{}) (msg string, extra interface{}, includeExtra bool)

func GetFormatter

func GetFormatter() FormatterFunc

type PanicHolder

type PanicHolder interface {
	StackTraceHolder
	Recovered() interface{}
}

PanicHolder is a marker interface for a recovered panic

func InnermostPanicWithStack

func InnermostPanicWithStack(errorOrHolder interface{}) PanicHolder

InnermostPanicWithStack returns the most distant panic holder from the chain. The value can be either error or PanicHolder. Otherwise will return nil.

type Severity

type Severity uint8
const (

	// NormalSeverity should only be used to override higher severity levels with WithSeverity
	NormalSeverity Severity

	// BlameSeverity indicates an attempt of a remote peer to compromise stability/performance of a peer network,
	// but there is no non-reputable proof available/possible, or multiple evidences from multiple peers are required.
	BlameSeverity

	// ViolationSeverity indicates an attempt of a remote peer to use an incorrect encoding / protocol combination that
	// can't compromise stability or performance of a peer network.
	ViolationSeverity

	// FraudSeverity indicates an attempt of a remote peer to compromise stability/performance of a peer network.
	// This severity can only be declared when there is either a non-reputable proof or a network majority proof.
	FraudSeverity

	// RemoteBreachSeverity indicates a presence of either security breach on a remote peer or MitM attack.
	// Connection to the peer must be terminated, and the peer may need to be quarantined / blacklisted.
	RemoteBreachSeverity

	// LocalBreachSeverity indicates a presence of local security breach. Application must be terminated or quarantined asap.
	LocalBreachSeverity

	// FatalSeverity indicates an error that requires an application to be terminated asap for a reason unknown.
	FatalSeverity
)

func GetSeverity

func GetSeverity(errChain error) (Severity, bool)

func SeverityOf

func SeverityOf(errChain error) Severity

func UnwrapExtraInfo

func UnwrapExtraInfo(err interface{}) (string, Severity, interface{}, bool)

func (Severity) IsCompromised

func (v Severity) IsCompromised() bool

func (Severity) IsDeadCanary

func (v Severity) IsDeadCanary() bool

func (Severity) IsError

func (v Severity) IsError() bool

func (Severity) IsFatal

func (v Severity) IsFatal() bool

func (Severity) IsFraudOrWorse

func (v Severity) IsFraudOrWorse() bool

func (Severity) IsWarn

func (v Severity) IsWarn() bool

type StackCompareMode

type StackCompareMode uint8

StackCompareMode defines precision of stack trace comparison

const (
	StrictMatch StackCompareMode = iota
	SameLine
	SameMethod
)

type StackRelation

type StackRelation int8

StackRelation shows relevance of 2 stacks

const (
	SubsetStack StackRelation = iota - 2
	StackTop
	EqualStack
	FullStack     // vs StackTop
	SupersetStack // vs SubsetStack
	DifferentStack
)

func CompareDebugStackTrace

func CompareDebugStackTrace(bst0, bst1 []byte, mode StackCompareMode) StackRelation

CompareDebugStackTrace returns a relation between the stacks produced by debug.Stack() with the given mode/precision.

func CompareStackTrace

func CompareStackTrace(st0, st1 StackTrace) StackRelation

CompareStackTrace returns a relation between the given stacks. Only supports StackTrace instances created by this module, otherwise will return DifferentStack

func CompareStackTraceExt

func CompareStackTraceExt(st0, st1 StackTrace, mode StackCompareMode) StackRelation

CompareStackTraceExt returns a relation between the given stacks with the given mode/precision. Only supports StackTrace instances created by this module, otherwise will return DifferentStack

type StackTrace

type StackTrace interface {
	StackTraceAsText() string
	WriteStackTraceTo(writer io.Writer) error
	IsFullStack() bool
}

func CaptureStack

func CaptureStack(skipFrames int) StackTrace

CaptureStack captures whole stack When (skipFrames) are more than stack depth then only "created by" entry will be returned

func CaptureStackTop

func CaptureStackTop(skipFrames int) StackTrace

CaptureStackTop is an optimized version to capture a limited info about top-level stack entry only When (skipFrames) are more than stack depth then only "created by" entry will be returned

func DeepestStackTraceOf

func DeepestStackTraceOf(errChain error) StackTrace

func ErrorWithTopOrFullStack

func ErrorWithTopOrFullStack(errChain error) (string, StackTrace)

func ErrorWithTopOrMinimizedStack

func ErrorWithTopOrMinimizedStack(errChain error, boundPackage string, includeBoundary bool) (string, StackTrace)

func ExtractStackTop

func ExtractStackTop(st StackTrace, skipFrames int) StackTrace

func MinimizeStackTrace

func MinimizeStackTrace(st StackTrace, boundPackage string, includeBoundary bool) StackTrace

type StackTraceHolder

type StackTraceHolder interface {
	// Cause returns a cause for this stack trace. It can NOT be used like Unwrap() as it may return self.
	Cause() error
	// ShallowStackTrace returns a stack trace registered for this cause
	ShallowStackTrace() StackTrace
	// DeepestStackTrace returns this or the deepest stack trace from cause's error chain that embeds ShallowStackTrace.
	// Bool value is true when the stack trace was inherited from the cause.
	DeepestStackTrace() (StackTrace, DeepestStackMode)
}

func InnermostFullStack

func InnermostFullStack(errChain error) (sth StackTraceHolder)

InnermostFullStack returns the most distant StackTraceHolder with non-nil ShallowStackTrace and IsFullStack from errChain

func InnermostStack

func InnermostStack(errChain error) (sth StackTraceHolder)

InnermostStack returns the most distant StackTraceHolder with non-nil ShallowStackTrace from errChain

func NearestStackOf

func NearestStackOf(errChain, target error) (error, StackTraceHolder, bool)

NearestStackOf returns a target-matched entry with a last encountered StackTraceHolder with a either shallow or deep non-nil stack (optional) When (target) is nil - returns the last error of the chain When (target) is not found, returns only the last StackTraceHolder. nolint

func OutermostStack

func OutermostStack(errChain error) StackTraceHolder

OutermostStack returns the closest StackTraceHolder with non-nil ShallowStackTrace from errChain

func StackOf

func StackOf(errChain, target error) (error, StackTraceHolder, bool)

StackOf returns a target-matched entry with a wrapping StackTraceHolder (optional) When (target) is nil - returns the last error of the chain nolint

Jump to

Keyboard shortcuts

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