errors

package module
v0.0.0-...-2238e12 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2019 License: MIT Imports: 5 Imported by: 0

README

go-errors

A simple Go error utility to record the error stack trace, which supports customized formatting and is easy to migrate.

How to

Replace import paths of import "errors" by:

import "github.com/IguteChung/go-errors"

Replace all the occurences of fmt.Errorf by:

errors.Errorf("something wrong: %v", err)

You are done, let's get the stacktrace

fmt.Println(errors.StackTrace(err))

Output:

/tmp/sandbox647637562/prog.go:24
/tmp/sandbox647637562/prog.go:19
/tmp/sandbox647637562/prog.go:14

More customization

Apply custom format for stack trace:

func init() {
	errors.ApplyFormatter("foo\n\tfile.go:152\n")
}

Output:

main.bar
	/tmp/sandbox639852497/prog.go:24
main.foo
	/tmp/sandbox639852497/prog.go:19
main.main
	/tmp/sandbox639852497/prog.go:14

Documentation

Index

Constants

View Source
const (
	DefaultFormatter    Formatter = "file.go:152\n"
	JavaLikeFormatter             = "at foo(file.go:152)\n"
	GoLikeFormatter               = "foo\n\tfile.go:152\n"
	PythonLikeFormatter           = "File file.go, line 152, in foo\n"
)

constants for Stack formatters.

Variables

This section is empty.

Functions

func ApplyFormatter

func ApplyFormatter(formatter Formatter)

ApplyFormatter specifies the formatter for errorTracer. Apply DefaultFormatter if not specified. The template of formatter: "foo" for function name, "file.go" for file name, "152" for line number.

func Cause

func Cause(err error) error

Cause returns the original error of a traceable error. If the error is not traceable, return itself.

func Errorf

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

Errorf creates a traceable error with formatted message. If args contains a traceable error, apply the stack of it. If args doesn't contain any traceable error, record the stack trace.

func GrpcError

func GrpcError(code codes.Code, msg string) error

GrpcError creates a traceable error with message and gRPC status code.

func GrpcErrorf

func GrpcErrorf(code codes.Code, msg string, args ...interface{}) error

GrpcErrorf creates a traceable error with formatted message and gRPC status code. If args contains a traceable error, apply the stack of it. If args doesn't contain a traceable error, record the stack trace.

func New

func New(msg string) error

New creates a traceable error with message.

func StackTrace

func StackTrace(err error) string

StackTrace prints the stack trace with given error by the formatter. If the error is not traceable, empty string is returned.

func Wrap

func Wrap(err error) error

Wrap wraps an error into a traceable error. If the wrapped error is traceable, do nothing. If the wrapped error is not traceable, record the stack trace.

func WrapMessage

func WrapMessage(err error, msg string) error

WrapMessage wraps an error into a traceable error with message. If the wrapped error is traceable, only append the error message. If the wrapped error is not traceable, record the stack trace.

func WrapMessagef

func WrapMessagef(err error, msg string, args ...interface{}) error

WrapMessagef wraps an error into a traceable error with formatted message. If the wrapped error is traceable, only append the error message. If the wrapped error is not traceable, record the stack trace.

Types

type Formatter

type Formatter string

Formatter defines how stack trace is formatted.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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