rterror

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 11

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

Examples

Constants

View Source
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

func IsTemporary(err error) bool

IsTemporary returns true if provided error is temporary. Otherwise, it returns false.

func IsTimeout added in v1.9.0

func IsTimeout(err error) bool

IsTimeout returns true if provided error is a timeout. Otherwise, it returns false.

func Wrap added in v1.16.0

func Wrap(err error, message string, arguments ...interface{}) error

Wrap wraps provided errors into runtime error.

If provided error is nil, it also returns nil. It is used for wrapping errors from returned functions without using extra if-return statement.

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) File

func (r *RuntimeError) File() string

File returns file absolute path.

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) Line

func (r *RuntimeError) Line() int

Line returns line number.

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) Unwrap

func (r *RuntimeError) Unwrap() error

Unwrap returns wrapped 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.

type Timeouter added in v1.9.0

type Timeouter interface {
	Timeout() bool
}

Timeouter defines an interface to check if returned error is a timeout.

Jump to

Keyboard shortcuts

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