Documentation
¶
Overview ¶
Package rterror implements runtime error with message string formatted using "replacement fields" surrounded by curly braces {} format strings from the Go Formatter library.
Index ¶
- Constants
- func IsTemporary(err error) bool
- func IsTimeout(err error) bool
- func Wrap(err error, message string, arguments ...interface{}) error
- type RuntimeError
- func (r *RuntimeError) Arguments() []interface{}
- func (r *RuntimeError) Error() (result string)
- func (r *RuntimeError) Errors() []error
- func (r *RuntimeError) File() string
- func (r *RuntimeError) FileBase() string
- func (r *RuntimeError) Function() string
- func (r *RuntimeError) FunctionBase() string
- func (r *RuntimeError) GetFormat() string
- func (r *RuntimeError) GetFormatter() *formatter.Formatter
- func (r *RuntimeError) Line() int
- func (r *RuntimeError) MarshalJSON() ([]byte, error)
- func (r *RuntimeError) MarshalText() ([]byte, error)
- func (r *RuntimeError) Message() string
- func (r *RuntimeError) Package() string
- func (r *RuntimeError) PackageBase() string
- func (r *RuntimeError) ResetFormat() *RuntimeError
- func (r *RuntimeError) SetFormat(format string) *RuntimeError
- func (r *RuntimeError) SetFormatter(f *formatter.Formatter) *RuntimeError
- func (r *RuntimeError) String() string
- func (r *RuntimeError) TopError() string
- func (r *RuntimeError) Unwrap() error
- func (r *RuntimeError) Wrap(errs ...error) *RuntimeError
- type Temporarer
- type Timeouter
Examples ¶
Constants ¶
const ( SkipCall = 1 DefaultTreeSpace = " " DefaultTreeMiddle = "|--" DefaultTreeContinue = "| " DefaultTreeEnd = "`--" DefaultTreeIndent = 3 DefaultFormat = `{cyan | bright}{.Package}{reset}:{bold}{cyan}{.FileBase}{reset}:{bold}{magenta}{.Line}{reset}:` + `{bold}{blue | bright}{.FunctionBase}(){reset}: {.String}` )
These constants define default values for runtime error.
Variables ¶
This section is empty.
Functions ¶
func IsTemporary ¶ added in v1.9.0
IsTemporary returns true if provided error is temporary. Otherwise, it returns false.
Types ¶
type RuntimeError ¶
type RuntimeError struct {
// contains filtered or unexported fields
}
RuntimeError defines a runtime error with message string formatted using "replacement fields" surrounded by curly braces {} format strings from the Go Formatter library. It contains line number, file path and function name from where a runtime error was called.
Example (SetFormat) ¶
package main import ( "fmt" "gitlab.com/tymonx/go-error/rterror" ) func main() { err := rterror.New("Error message {p1} - {p0}", 5, "bar").SetFormat("#{.Package | base}.{.FunctionBase}: '{.String}'") fmt.Println(err) }
Output: #rterror_test.ExampleRuntimeError_setFormat: 'Error message bar - 5'
Example (Unwrap) ¶
package main import ( "errors" "fmt" "gitlab.com/tymonx/go-error/rterror" ) func main() { wrapped := rterror.New("wrapped error").SetFormat("{.Message}") err := rterror.New("Error message", 5).Wrap(wrapped) fmt.Println(errors.Is(err, wrapped)) fmt.Println(err) }
Output: true gitlab.com/tymonx/go-error/rterror_test:example_test.go:48:ExampleRuntimeError_unwrap(): Error message 5 `--wrapped error
Example (WithArguments) ¶
package main import ( "fmt" "gitlab.com/tymonx/go-error/rterror" ) func main() { err := rterror.New("Error message {p1} - {p0}", 3, "foo") fmt.Println(err) }
Output: gitlab.com/tymonx/go-error/rterror_test:example_test.go:32:ExampleRuntimeError_withArguments(): Error message foo - 3
Example (WithoutArguments) ¶
package main import ( "fmt" "gitlab.com/tymonx/go-error/rterror" ) func main() { err := rterror.New("Error message") fmt.Println(err) }
Output: gitlab.com/tymonx/go-error/rterror_test:example_test.go:25:ExampleRuntimeError_withoutArguments(): Error message
func New ¶
func New(message string, arguments ...interface{}) *RuntimeError
New creates a new runtime error object with message string formatted using "replacement fields" surrounded by curly braces {} format strings, line number, file path and function name from where the New() function was called.
func NewSkipCaller ¶
func NewSkipCaller(skip int, message string, arguments ...interface{}) *RuntimeError
NewSkipCaller creates a new runtime error object with message string formatted using "replacement fields" surrounded by curly braces {} format strings, line number, file path and function name from where the NewSkipCaller() function was called. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of NewSkipCaller.
Example ¶
package main import ( "fmt" "gitlab.com/tymonx/go-error/rterror" ) func main() { MyNewError := func(message string, arguments ...interface{}) *rterror.RuntimeError { return rterror.NewSkipCaller(1, message, arguments...) } err := MyNewError("Error message {p1}", "caller", "skip") fmt.Println(err) }
Output: gitlab.com/tymonx/go-error/rterror_test:example_test.go:63:ExampleNewSkipCaller(): Error message skip caller
func (*RuntimeError) Arguments ¶
func (r *RuntimeError) Arguments() []interface{}
Arguments returns error arguments.
func (*RuntimeError) Error ¶
func (r *RuntimeError) Error() (result string)
Error returns formatted error message string.
With wrapped errors it returns:
<error> `--<error> `--<error> `--<error>
func (*RuntimeError) Errors ¶ added in v1.15.0
func (r *RuntimeError) Errors() []error
Errors returns list of wrapped errors.
func (*RuntimeError) FileBase ¶ added in v1.6.0
func (r *RuntimeError) FileBase() string
FileBase returns file base path.
func (*RuntimeError) Function ¶
func (r *RuntimeError) Function() string
Function returns function full name.
func (*RuntimeError) FunctionBase ¶ added in v1.6.0
func (r *RuntimeError) FunctionBase() string
FunctionBase returns function base name.
func (*RuntimeError) GetFormat ¶
func (r *RuntimeError) GetFormat() string
GetFormat returns error message format string for formatter.
func (*RuntimeError) GetFormatter ¶
func (r *RuntimeError) GetFormatter() *formatter.Formatter
GetFormatter returns formatter.
func (*RuntimeError) MarshalJSON ¶ added in v1.8.0
func (r *RuntimeError) MarshalJSON() ([]byte, error)
MarshalJSON encodes runtime error to JSON.
func (*RuntimeError) MarshalText ¶ added in v1.8.0
func (r *RuntimeError) MarshalText() ([]byte, error)
MarshalText encodes runtime error to text.
func (*RuntimeError) Message ¶
func (r *RuntimeError) Message() string
Message returns unformatted error message.
func (*RuntimeError) Package ¶ added in v1.1.0
func (r *RuntimeError) Package() string
Package returns full package path.
func (*RuntimeError) PackageBase ¶ added in v1.14.0
func (r *RuntimeError) PackageBase() string
PackageBase returns package name.
func (*RuntimeError) ResetFormat ¶
func (r *RuntimeError) ResetFormat() *RuntimeError
ResetFormat resets error message format string for formatter to default value.
func (*RuntimeError) SetFormat ¶
func (r *RuntimeError) SetFormat(format string) *RuntimeError
SetFormat sets error message format string for formatter.
func (*RuntimeError) SetFormatter ¶
func (r *RuntimeError) SetFormatter(f *formatter.Formatter) *RuntimeError
SetFormatter sets formatter.
func (*RuntimeError) String ¶ added in v1.7.0
func (r *RuntimeError) String() string
String returns formatted error message string.
func (*RuntimeError) TopError ¶ added in v1.14.0
func (r *RuntimeError) TopError() string
TopError returns top error message without any wrapped error messages.
With wrapped errors it simple returns:
<error>
func (*RuntimeError) Wrap ¶ added in v1.10.0
func (r *RuntimeError) Wrap(errs ...error) *RuntimeError
Wrap wraps provided errors into runtime error.
type Temporarer ¶ added in v1.9.0
type Temporarer interface {
Temporary() bool
}
Temporarer defines an interface to check if returned error is temporary.