Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Code ¶ added in v1.4.0
Code returns the oops error code, if the error is an oops error. if the error code is not set, it looks for a "code" attribute set to an int.
func EnableSource ¶ added in v1.2.0
func EnableSource(enabled bool)
EnableSource enables or disables source tracking. default is true.
func NewOopsHandler ¶ added in v1.2.0
NewOopsHandler creates a handler that logs oops errors with additional context.
func SetAttrField ¶ added in v1.2.0
func SetAttrField(field string)
SetAttrField overrides the field used to oops error attributes. default is "oops".
func Wrap ¶
Wrap wraps an error as an oops error. supports supports [string, any]... pairs or slog.Attr values.
Example ¶
l := logger()
err := errors.New("oops")
err = Wrap(err, "id", "wrap")
fmt.Println(err)
l.Error("error", "err", err)
Output: oops level=ERROR msg=error err.err=oops err.id=wrap err.source="[{Function:github.com/jesse0michael/oops.ExampleWrap File:oops_test.go Line:29}]"
Example (Nil) ¶
l := logger()
err := Wrap(nil)
if err != nil {
fmt.Println(err)
}
l.Error("error", "err", err)
Output: level=ERROR msg=error err=<nil>
Example (OopsError) ¶
l := logger()
oopsErr := New("oops").With("id", "new", "component", "example")
err := Wrap(oopsErr, "id", "wrap")
fmt.Println(err)
l.Error("error", "err", err)
Output: oops level=ERROR msg=error err.err=oops err.component=example err.id=wrap err.source="[{Function:github.com/jesse0michael/oops.ExampleWrap_oopsError File:oops_test.go Line:42} {Function:github.com/jesse0michael/oops.ExampleWrap_oopsError File:oops_test.go Line:43}]"
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func Errorf ¶
Errorf formats a string and returns a new oops error.
Example ¶
l := logger()
err := Errorf("failure: %s, %w", "Errorf", errors.New("oops"))
fmt.Println(err)
l.Error("error", "err", err)
Output: failure: Errorf, oops level=ERROR msg=error err.err="failure: Errorf, oops" err.source="[{Function:github.com/jesse0michael/oops.ExampleErrorf File:oops_test.go Line:56}]"
Example (WrapOopsError) ¶
l := logger()
err := New("oops").With("id", "new", "component", "example")
err = Errorf("failure: %s, %w", "Errorf", err).With("id", "wrap")
fmt.Println(err)
l.Error("error", "err", err)
Output: failure: Errorf, oops level=ERROR msg=error err.err="failure: Errorf, oops" err.component=example err.id=wrap err.source="[{Function:github.com/jesse0michael/oops.ExampleErrorf_wrapOopsError File:oops_test.go Line:70} {Function:github.com/jesse0michael/oops.ExampleErrorf_wrapOopsError File:oops_test.go Line:69}]"
func New ¶
New returns a new oops error.
Example ¶
l := logger()
err := New("oops").With("id", "new")
fmt.Println(err)
l.Error("error", "err", err)
Output: oops level=ERROR msg=error err.err=oops err.id=new err.source="[{Function:github.com/jesse0michael/oops.ExampleNew File:oops_test.go Line:15}]"
Example (NonOopsWrappedError) ¶
// This example shows how the LogValue won't be hit for a wrapped error :(
l := logger()
var err error
err = New("oops").With("id", "new", "component", "example")
err = &wrappedError{underlying: err}
fmt.Println(err)
l.Error("error", "err", err)
Output: oops level=ERROR msg=error err=oops
func (*Error) Code ¶ added in v1.4.0
Code sets the error code.
Example ¶
l := logger()
err := New("oops").Code(404)
fmt.Println(err)
l.Error("error", "err", err)
fmt.Println(Code(err))
Output: oops level=ERROR msg=error err.err=oops err.source="[{Function:github.com/jesse0michael/oops.ExampleError_Code File:oops_test.go Line:97}]" 404
Click to show internal directories.
Click to hide internal directories.