oops

package module
v1.14.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: MIT Imports: 19 Imported by: 111

README ΒΆ

Oops - Error handling with context, assertion, stack trace and source fragments

tag Go Version GoDoc Build Status Go report Coverage Contributors License

(Yet another) error handling library: oops.Errorf is a dead-simple drop-in replacement for built-in error, adding contextual information such as stack trace, extra attributes, error code, and bug-fixing hints...

⚠️ This is NOT a logging library. oops should be used as a complement to your existing logging toolchain (zap, zerolog, logrus, slog, go-sentry...).

πŸ₯· Start hacking oops with this playground.

Jump:

πŸ€” Motivations

Loggers usually allow developers to build records with contextual attributes, that describe errors, such as:

  • zap.Infow("failed to fetch URL", "url", url)
  • logrus.WithFields("url", url).Error("failed to fetch URL")).

Go recommends cascading error handling, which can cause the error to be triggered far away from the call to the logger. Returning context over X callers is painful, and to be meaningful, the stack trace must be gathered by the error builder instead of the logger.

This is why we need an error wrapper!

πŸ₯΅ Why develop yet another library?

  • drop-in replacement to error
  • easy to integrate without large refactoring
  • separation of concern (logger vs error)
  • extra attributes
  • developer-friendly error builder
  • no extra code for output: can be used with loggers, printf syntax...
  • out-of-the-box stack trace and source fragments
  • one-line panic handling
  • one-line assertion
❌ Before samber/oops

In the following example, we try to propagate an error with contextual information and stack trace, to the caller function handler():

func c(token string) error {
    userID := ...   // <-- How do I transport `userID` and `role` from here...
    role := ...

    // ...

    return fmt.Errorf("an error")
}

func b() error {
    // ...
    return c()
}

func a() {
    err := b()
    if err != nil {
        // print log
        slog.Error(err.Error(),
            slog.String("user.id", "????"),      // <-- ...to here ??
            slog.String("user.role", "????"),    // <-- ...and here ??
            slog.String("stracktrace", generateStacktrace()))  // <-- this won't contain the exact error location 😩
    }
}
βœ… Using samber/oops

I would rather write something like that:

func d() error {
    return oops.
        Code("iam_missing_permission").
        In("authz").
        Tags("authz").
        Time(time.Now()).
        With("user_id", 1234).
        With("permission", "post.create").
        Hint("Runbook: https://doc.acme.org/doc/abcd.md").
        User("user-123", "firstname", "john", "lastname", "doe").
        Errorf("permission denied")
}

func c() error {
	return d()
}

func b() error {
    // add more context
    return oops.
        In("iam").
        Tags("iam").
        Trace("e76031ee-a0c4-4a80-88cb-17086fdd19c0").
        With("hello", "world").
        Wrapf(c(), "something failed")
}

func a() error {
	return b()
}

func main() {
    logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

    err := a()
    if err != nil {
        logger.Error(
            err.Error(),
            slog.Any("error", err), // unwraps and flattens error context
        )
    }
}
Why "oops"?

Have you already heard a developer yelling at unclear error messages in Sentry, with no context, just before figuring out he wrote this piece of shit by himself?

Yes. Me too.

oops!

πŸš€ Install

go get github.com/samber/oops

This library is v1 and follows SemVer strictly.

No breaking changes will be made to APIs before v2.0.0.

This library has no dependencies outside the Go standard library.

πŸ’‘ Quick start

This library provides a simple error builder for composing structured errors, with contextual attributes and stack trace.

Since oops.OopsError implements the error interface, you will be able to compose and wrap native errors with oops.OopsError.

πŸ₯· Start hacking oops with this playground.

🧠 Spec

GoDoc: https://godoc.org/github.com/samber/oops

Error constructors
Constructor Description
.Errorf(format string, args ...any) error Formats an error and returns oops.OopsError object that satisfies error
.Wrap(err error) error Wraps an error into an oops.OopsError object that satisfies error
.Wrapf(err error, format string, args ...any) error Wraps an error into an oops.OopsError object that satisfies error and formats an error message
.Recover(cb func()) error Handle panic and returns oops.OopsError object that satisfies error.
.Recoverf(cb func(), format string, args ...any) error Handle panic and returns oops.OopsError object that satisfies error and formats an error message.
.Assert(condition bool) OopsErrorBuilder Panics if condition is false. Assertions can be chained.
.Assertf(condition bool, format string, args ...any) OopsErrorBuilder Panics if condition is false and formats an error message. Assertions can be chained.
.Join(err1 error, err2 error, ...) error Join returns an error that wraps the given errors.
Examples
// with error wrapping
err0 := oops.
    In("repository").
    Tags("database", "sql").
    Wrapf(sql.Exec(query), "could not fetch user")  // Wrapf returns nil when sql.Exec() is nil

// with panic recovery
err1 := oops.
    In("repository").
    Tags("database", "sql").
    Recover(func () {
        panic("caramba!")
    })

// with assertion
err2 := oops.
    In("repository").
    Tags("database", "sql").
    Recover(func () {
        // ...
        oops.Assertf(time.Now().Weekday() == 1, "This code should run on Monday only.")
        // ...
    })
Context

The library provides an error builder. Each method can be used standalone (eg: oops.With(...)) or from a previous builder instance (eg: oops.In("iam").User("user-42")).

The oops.OopsError builder must finish with either .Errorf(...), .Wrap(...) or .Wrapf(...).

Builder method Getter Description
.With(string, any) err.Context() map[string]any Supply a list of attributes key+value. Values of type func() any {} are accepted and evaluated lazily.
.WithContext(context.Context, ...any) err.Context() map[string]any Supply a list of values declared in context. Values of type func() any {} are accepted and evaluated lazily.
.Code(string) err.Code() string Set a code or slug that describes the error. Error messages are intented to be read by humans, but such code is expected to be read by machines and be transported over different services
.Public(string) err.Public() string Set a message that is safe to show to an end user
.Time(time.Time) err.Time() time.Time Set the error time (default: time.Now())
.Since(time.Time) err.Duration() time.Duration Set the error duration
.Duration(time.Duration) err.Duration() time.Duration Set the error duration
.In(string) err.Domain() string Set the feature category or domain
.Tags(...string) err.Tags() []string Add multiple tags, describing the feature returning an error
.Trace(string) err.Trace() string Add a transaction id, trace id, correlation id... (default: ULID)
.Span(string) err.Span() string Add a span representing a unit of work or operation... (default: ULID)
.Hint(string) err.Hint() string Set a hint for faster debugging
.Owner(string) err.Owner() (string) Set the name/email of the collegue/team responsible for handling this error. Useful for alerting purpose
.User(string, any...) err.User() (string, map[string]any) Supply user id and a chain of key/value
.Tenant(string, any...) err.Tenant() (string, map[string]any) Supply tenant id and a chain of key/value
.Request(*http.Request, bool) err.Request() *http.Request Supply http request
.Response(*http.Response, bool) err.Response() *http.Response Supply http response
Examples
// simple error with public facing message
err0 := oops.
    Public("Could not fetch user.").
    Errorf("sql: bad connection")

// simple error with stacktrace
err1 := oops.Errorf("could not fetch user")

// with optional domain
err2 := oops.
    In("repository").
    Tags("database", "sql").
    Errorf("could not fetch user")

// with custom attributes
ctx := context.WithContext(context.Background(), "a key", "value")
err3 := oops.
    With("driver", "postgresql").
    With("query", query).
    With("query.duration", queryDuration).
    With("lorem", func() string { return "ipsum" }).	// lazy evaluation
    WithContext(ctx, "a key", "another key").
    Errorf("could not fetch user")

// with trace+span
err4 := oops.
    Trace(traceID).
    Span(spanID).
    Errorf("could not fetch user")

// with hint and ownership, for helping developer to solve the issue
err5 := oops.
    Hint("The user could have been removed. Please check deleted_at column.").
    Owner("Slack: #api-gateway").
    Errorf("could not fetch user")

// with optional userID
err6 := oops.
    User(userID).
    Errorf("could not fetch user")

// with optional user data
err7 := oops.
    User(userID, "firstname", "Samuel").
    Errorf("could not fetch user")

// with optional user and tenant
err8 := oops.
    User(userID, "firstname", "Samuel").
    Tenant(workspaceID, "name", "my little project").
    Errorf("could not fetch user")

// with optional http request and response
err9 := oops.
    Request(req, false).
    Response(res, true).
    Errorf("could not fetch user")
Other helpers
  • oops.AsError[MyError](error) (MyError, bool) as an alias to errors.As(...)
Stack trace

This library provides a pretty printed stack trace for each generated error.

The stack trace max depth can be set using:

// default: 10
oops.StackTraceMaxDepth = 42

The stack trace will be printed this way:

err := oops.Errorf("permission denied")

fmt.Println(err.(oops.OopsError).Stacktrace())
Stacktrace

Wrapped errors will be reported as an annotated stack trace:

err1 := oops.Errorf("permission denied")
// ...
err2 := oops.Wrapf(err, "something failed")

fmt.Println(err2.(oops.OopsError).Stacktrace())
Stacktrace
Source fragments

The exact error location can be provided in a Go file extract.

Source fragments are hidden by default. You must run oops.SourceFragmentsHidden = false to enable this feature. Go source files being read at run time, you have to keep the source code at the same location.

In a future release, this library is expected to output a colorized extract. Please contribute!

oops.SourceFragmentsHidden = false

err1 := oops.Errorf("permission denied")
// ...
err2 := oops.Wrapf(err, "something failed")

fmt.Println(err2.(oops.OopsError).Sources())
Sources
Panic handling

oops library is delivered with a try/catch -ish error handler. 2 handlers variants are available: oops.Recover() and oops.Recoverf(). Both can be used in the oops error builder with usual methods.

πŸ₯· Start hacking oops.Recover() with this playground.

func mayPanic() {
	panic("permission denied")
}

func handlePanic() error {
    return oops.
        Code("iam_authz_missing_permission").
        In("authz").
        With("permission", "post.create").
        Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9").
        Hint("Runbook: https://doc.acme.org/doc/abcd.md").
        Recoverf(func() {
            // ...
            mayPanic()
            // ...
        }, "unexpected error %d", 42)
}
Assertions

Assertions may be considered an anti-pattern for Golang since we only call panic() for unexpected and critical errors. In this situation, assertions might help developers to write safer code.

func mayPanic() {
    x := 42

    oops.
        Trace("6710668a-2b2a-4de6-b8cf-3272a476a1c9").
        Hint("Runbook: https://doc.acme.org/doc/abcd.md").
        Assertf(time.Now().Weekday() == 1, "This code should run on Monday only.").
        With("x", x).
        Assertf(x == 42, "expected x to be equal to 42, but got %d", x)

    oops.Assert(re.Match(email))

    // ...
}

func handlePanic() error {
    return oops.
        Code("iam_authz_missing_permission").
        In("authz").
        Recover(func() {
            // ...
            mayPanic()
            // ...
        })
}
Output

Errors can be printed in many ways. Logger formatters provided in this library use these methods.

Errorf %w
str := fmt.Errorf("something failed: %w", oops.Errorf("permission denied"))

fmt.Println(err.Error())
// Output:
// something failed: permission denied
printf %v
err := oops.Errorf("permission denied")

fmt.Printf("%v", err)
// Output:
// permission denied
printf %+v
err := oops.Errorf("permission denied")

fmt.Printf("%+v", err)
Output
JSON Marshal
b := json.MarshalIndent(err, "", "  ")
Output
slog.Valuer
err := oops.Errorf("permission denied")

attr := slog.Error(err.Error(),
    slog.Any("error", err))

// Output:
// slog.Group("error", ...)
Custom timezone
loc, _ := time.LoadLocation("Europe/Paris")
oops.Local = loc

πŸ“« Loggers

Some loggers may need a custom formatter to extract attributes from oops.OopsError.

Available loggers:

We are looking for contributions and examples for:

  • zap
  • zerolog
  • go-sentry
  • otel
  • other?

Examples of formatters can be found in ToMap(), Format(), Marshal() and LogValuer methods of oops.OopsError.

πŸ₯· Tips and best practices

Public facing error message

Humans do not like technical errors. The oops container can bring an additional human-readable message.

err := oops.
    Public("Could not fetch user.").
    Errorf("sql: bad connection")

userMessage := oops.GetPublic(err, "Unexpected error")
Wrap/Wrapf shortcut

oops.Wrap(...) and oops.Wrapf(...) returns nil if the provided error is nil.

❌ So don't write:

err := mayFail()
if err != nil {
    return oops.Wrapf(err, ...)
}

return nil

βœ… but write:

return oops.Wrapf(mayFail(), ...)
Reuse error builder

Writing a full contextualized error can be painful and very repetitive. But a single context can be used for multiple errors in a single function:

❌ So don't write:

err := mayFail1()
if err != nil {
    return oops.
        In("iam").
        Trace("77cb6664").
        With("hello", "world").
        Wrap(err)
}

err = mayFail2()
if err != nil {
    return oops.
        In("iam").
        Trace("77cb6664").
        With("hello", "world").
        Wrap(err)
}

return oops.
    In("iam").
    Trace("77cb6664").
    With("hello", "world").
    Wrap(mayFail3())

βœ… but write:

errorBuilder := oops.
    In("iam").
    Trace("77cb6664").
    With("hello", "world")

err := mayFail1()
if err != nil {
    return errorBuilder.Wrap(err)
}

err = mayFail2()
if err != nil {
    return errorBuilder.Wrap(err)
}

return errorBuilder.Wrap(mayFail3())
Caller/callee attributes

Also, think about feeding error context in every caller, instead of adding extra information at the last moment.

❌ So don't write:

func a() error {
    return b()
}

func b() error {
    return c()
}

func c() error {
    return d()
}

func d() error {
    return oops.
        Code("iam_missing_permission").
        In("authz").
        Trace("4ea76885-a371-46b0-8ce0-b72b277fa9af").
        Time(time.Now()).
        With("hello", "world").
        With("permission", "post.create").
        Hint("Runbook: https://doc.acme.org/doc/abcd.md").
        User("user-123", "firstname", "john", "lastname", "doe").
        Tenant("organization-123", "name", "Microsoft").
        Errorf("permission denied")
}

βœ… but write:

func a() error {
	return b()
}

func b() error {
    return oops.
        In("iam").
        Trace("4ea76885-a371-46b0-8ce0-b72b277fa9af").
        With("hello", "world").
        Wrapf(c(), "something failed")
}

func c() error {
    return d()
}

func d() error {
    return oops.
        Code("iam_missing_permission").
        In("authz").
        Time(time.Now()).
        With("permission", "post.create").
        Hint("Runbook: https://doc.acme.org/doc/abcd.md").
        User("user-123", "firstname", "john", "lastname", "doe").
        Tenant("organization-123", "name", "Microsoft").
        Errorf("permission denied")
}

🀝 Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

πŸ‘€ Contributors

Contributors

πŸ’« Show your support

Give a ⭐️ if this project helped you!

GitHub Sponsors

πŸ“ License

Copyright Β© 2023 Samuel Berthe.

This project is MIT licensed.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var (
	SourceFragmentsHidden                = true
	DereferencePointers                  = true
	Local                 *time.Location = time.UTC
)
View Source
var (
	StackTraceMaxDepth int = 10
)

Functions ΒΆ

func Errorf ΒΆ

func Errorf(format string, args ...any) error

Errorf formats an error and returns `oops.OopsError` object that satisfies `error`.

func GetPublic ΒΆ added in v1.13.0

func GetPublic(err error, defaultPublicMessage string) string

GetPublic returns a message that is safe to show to an end user, or a default generic message.

func Join ΒΆ added in v1.12.1

func Join(e ...error) error

func Recover ΒΆ

func Recover(cb func()) (err error)

Recover handle panic and returns `oops.OopsError` object that satisfies `error`.

func Recoverf ΒΆ

func Recoverf(cb func(), msg string, args ...any) (err error)

Recoverf handle panic and returns `oops.OopsError` object that satisfies `error` and formats an error message.

func Wrap ΒΆ

func Wrap(err error) error

Wrap wraps an error into an `oops.OopsError` object that satisfies `error`

func Wrap10 ΒΆ added in v1.6.0

func Wrap10[A any, B any, C any, D any, E any, F any, G any, H any, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I, err error) (A, B, C, D, E, F, G, H, I, error)

func Wrap2 ΒΆ added in v1.6.0

func Wrap2[A any](a A, err error) (A, error)

func Wrap3 ΒΆ added in v1.6.0

func Wrap3[A any, B any](a A, b B, err error) (A, B, error)

func Wrap4 ΒΆ added in v1.6.0

func Wrap4[A any, B any, C any](a A, b B, c C, err error) (A, B, C, error)

func Wrap5 ΒΆ added in v1.6.0

func Wrap5[A any, B any, C any, D any](a A, b B, c C, d D, err error) (A, B, C, D, error)

func Wrap6 ΒΆ added in v1.6.0

func Wrap6[A any, B any, C any, D any, E any](a A, b B, c C, d D, e E, err error) (A, B, C, D, E, error)

func Wrap7 ΒΆ added in v1.6.0

func Wrap7[A any, B any, C any, D any, E any, F any](a A, b B, c C, d D, e E, f F, err error) (A, B, C, D, E, F, error)

func Wrap8 ΒΆ added in v1.6.0

func Wrap8[A any, B any, C any, D any, E any, F any, G any](a A, b B, c C, d D, e E, f F, g G, err error) (A, B, C, D, E, F, G, error)

func Wrap9 ΒΆ added in v1.6.0

func Wrap9[A any, B any, C any, D any, E any, F any, G any, H any](a A, b B, c C, d D, e E, f F, g G, h H, err error) (A, B, C, D, E, F, G, H, error)

func Wrapf ΒΆ

func Wrapf(err error, format string, args ...any) error

Wrapf wraps an error into an `oops.OopsError` object that satisfies `error` and formats an error message.

func Wrapf10 ΒΆ added in v1.10.0

func Wrapf10[A any, B any, C any, D any, E any, F any, G any, H any, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I, err error, format string, args ...any) (A, B, C, D, E, F, G, H, I, error)

func Wrapf2 ΒΆ added in v1.10.0

func Wrapf2[A any](a A, err error, format string, args ...any) (A, error)

func Wrapf3 ΒΆ added in v1.10.0

func Wrapf3[A any, B any](a A, b B, err error, format string, args ...any) (A, B, error)

func Wrapf4 ΒΆ added in v1.10.0

func Wrapf4[A any, B any, C any](a A, b B, c C, err error, format string, args ...any) (A, B, C, error)

func Wrapf5 ΒΆ added in v1.10.0

func Wrapf5[A any, B any, C any, D any](a A, b B, c C, d D, err error, format string, args ...any) (A, B, C, D, error)

func Wrapf6 ΒΆ added in v1.10.0

func Wrapf6[A any, B any, C any, D any, E any](a A, b B, c C, d D, e E, err error, format string, args ...any) (A, B, C, D, E, error)

func Wrapf7 ΒΆ added in v1.10.0

func Wrapf7[A any, B any, C any, D any, E any, F any](a A, b B, c C, d D, e E, f F, err error, format string, args ...any) (A, B, C, D, E, F, error)

func Wrapf8 ΒΆ added in v1.10.0

func Wrapf8[A any, B any, C any, D any, E any, F any, G any](a A, b B, c C, d D, e E, f F, g G, err error, format string, args ...any) (A, B, C, D, E, F, G, error)

func Wrapf9 ΒΆ added in v1.10.0

func Wrapf9[A any, B any, C any, D any, E any, F any, G any, H any](a A, b B, c C, d D, e E, f F, g G, h H, err error, format string, args ...any) (A, B, C, D, E, F, G, H, error)

Types ΒΆ

type OopsError ΒΆ

type OopsError struct {
	// contains filtered or unexported fields
}

func AsOops ΒΆ

func AsOops(err error) (OopsError, bool)

AsOops checks if an error is an `oops.OopsError` object. Alias to errors.As.

func (OopsError) Code ΒΆ

func (o OopsError) Code() string

Code returns the error cause. Error code is intented to be used by machines.

func (OopsError) Context ΒΆ

func (o OopsError) Context() map[string]any

Context returns a k/v context of the error.

func (OopsError) Domain ΒΆ

func (o OopsError) Domain() string

Domain returns the domain of the error.

func (OopsError) Duration ΒΆ

func (o OopsError) Duration() time.Duration

Duration returns the duration of the error.

func (OopsError) Error ΒΆ

func (o OopsError) Error() string

Error returns the error message, without context.

func (OopsError) Format ΒΆ

func (o OopsError) Format(s fmt.State, verb rune)

Format implements fmt.Formatter. If the format is "%+v", then the details of the error are included. Otherwise, using "%v", just the summary is included.

func (OopsError) Hint ΒΆ

func (o OopsError) Hint() string

Hint returns a hint to the user on how to resolve the error.

func (OopsError) Is ΒΆ added in v1.5.0

func (c OopsError) Is(err error) bool

func (OopsError) LogValuer ΒΆ

func (o OopsError) LogValuer() slog.Value

LogValuer returns a slog.Value for logging.

func (OopsError) MarshalJSON ΒΆ

func (o OopsError) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (OopsError) Owner ΒΆ

func (o OopsError) Owner() string

Owner identify the owner responsible for resolving the error.

func (OopsError) Public ΒΆ added in v1.13.0

func (o OopsError) Public() string

Public returns a message that is safe to show to an end user.

func (OopsError) Request ΒΆ added in v1.3.0

func (o OopsError) Request() *http.Request

Request returns the http request.

func (OopsError) Response ΒΆ added in v1.3.0

func (o OopsError) Response() *http.Response

Response returns the http response.

func (OopsError) Sources ΒΆ

func (o OopsError) Sources() string

Sources returns the source fragments of the error.

func (OopsError) Span ΒΆ

func (o OopsError) Span() string

Span returns the current span instead of the deepest one.

func (OopsError) Stacktrace ΒΆ

func (o OopsError) Stacktrace() string

Stacktrace returns a pretty printed stacktrace of the error.

func (OopsError) Tags ΒΆ

func (o OopsError) Tags() []string

Tags returns the tags of the error.

func (OopsError) Tenant ΒΆ

func (o OopsError) Tenant() (string, map[string]any)

Tenant returns the tenant id and tenant data.

func (OopsError) Time ΒΆ

func (o OopsError) Time() time.Time

Time returns the time when the error occured.

func (OopsError) ToMap ΒΆ

func (o OopsError) ToMap() map[string]any

ToMap returns a map representation of the error.

func (OopsError) Trace ΒΆ

func (o OopsError) Trace() string

Trace returns the transaction id, trace id, request id, correlation id, etc.

func (OopsError) Unwrap ΒΆ

func (o OopsError) Unwrap() error

Unwrap returns the underlying error.

func (OopsError) User ΒΆ

func (o OopsError) User() (string, map[string]any)

User returns the user id and user data.

type OopsErrorBuilder ΒΆ

type OopsErrorBuilder OopsError

*

  • Builder pattern. *
  • oops.Errorf("Could not fetch users: %w", err) *
  • oops.
  • User("steve@apple.com", "firstname", "Samuel").
  • Tenant("apple", "country", "us").
  • Errorf("403 not permitted") *
  • oops.
  • Time(requestDate).
  • Duration(requestDuration).
  • Tx(traceID).
  • Errorf("Failed to execute http request") *
  • oops.
  • With("project_id", project.ID, "created_at", project.CreatedAt).
  • Errorf("Could not update settings") *

func Assert ΒΆ added in v1.1.0

func Assert(condition bool) OopsErrorBuilder

Assert panics if condition is false. Panic payload will be of type oops.OopsError. Assertions can be chained.

func Assertf ΒΆ added in v1.1.0

func Assertf(condition bool, msg string, args ...any) OopsErrorBuilder

Assertf panics if condition is false. Panic payload will be of type oops.OopsError. Assertions can be chained.

func Code ΒΆ

func Code(code string) OopsErrorBuilder

Code set a code or slug that describes the error. Error messages are intented to be read by humans, but such code is expected to be read by machines and even transported over different services.

func Duration ΒΆ

func Duration(duration time.Duration) OopsErrorBuilder

Duration set the error duration.

func Hint ΒΆ

func Hint(hint string) OopsErrorBuilder

Hint set a hint for faster debugging.

func In ΒΆ

func In(domain string) OopsErrorBuilder

In set the feature category or domain.

func Owner ΒΆ

func Owner(owner string) OopsErrorBuilder

Owner set the name/email of the collegue/team responsible for handling this error. Useful for alerting purpose.

func Public ΒΆ added in v1.13.1

func Public(public string) OopsErrorBuilder

Public sets a message that is safe to show to an end user.

func Request ΒΆ added in v1.3.0

func Request(req *http.Request, withBody bool) OopsErrorBuilder

Request supplies a http.Request.

func Response ΒΆ added in v1.3.0

func Response(res *http.Response, withBody bool) OopsErrorBuilder

Response supplies a http.Response.

func Since ΒΆ

func Since(time time.Time) OopsErrorBuilder

Since set the error duration.

func Span ΒΆ

func Span(span string) OopsErrorBuilder

Span represents a unit of work or operation.

func Tags ΒΆ

func Tags(tags ...string) OopsErrorBuilder

Tags adds multiple tags, describing the feature returning an error.

func Tenant ΒΆ

func Tenant(tenantID string, data map[string]any) OopsErrorBuilder

Tenant supplies tenant id and a chain of key/value.

func Time ΒΆ

func Time(time time.Time) OopsErrorBuilder

Time set the error time. Default: `time.Now()`

func Trace ΒΆ

func Trace(trace string) OopsErrorBuilder

Trace set a transaction id, trace id or correlation id...

func User ΒΆ

func User(userID string, data map[string]any) OopsErrorBuilder

User supplies user id and a chain of key/value.

func With ΒΆ

func With(kv ...any) OopsErrorBuilder

With supplies a list of attributes declared by pair of key+value.

func WithContext ΒΆ added in v1.8.0

func WithContext(ctx context.Context, keys ...any) OopsErrorBuilder

With supplies a list of attributes declared by pair of key+value.

func (OopsErrorBuilder) Assert ΒΆ added in v1.1.0

func (o OopsErrorBuilder) Assert(condition bool) OopsErrorBuilder

Assert panics if condition is false. Panic payload will be of type oops.OopsError. Assertions can be chained.

func (OopsErrorBuilder) Assertf ΒΆ added in v1.1.0

func (o OopsErrorBuilder) Assertf(condition bool, msg string, args ...any) OopsErrorBuilder

Assertf panics if condition is false. Panic payload will be of type oops.OopsError. Assertions can be chained.

func (OopsErrorBuilder) Code ΒΆ

Code set a code or slug that describes the error. Error messages are intented to be read by humans, but such code is expected to be read by machines and even transported over different services.

func (OopsErrorBuilder) Duration ΒΆ

func (o OopsErrorBuilder) Duration(duration time.Duration) OopsErrorBuilder

Duration set the error duration.

func (OopsErrorBuilder) Errorf ΒΆ

func (o OopsErrorBuilder) Errorf(format string, args ...any) error

Errorf formats an error and returns `oops.OopsError` object that satisfies `error`.

func (OopsErrorBuilder) Hint ΒΆ

Hint set a hint for faster debugging.

func (OopsErrorBuilder) In ΒΆ

In set the feature category or domain.

func (OopsErrorBuilder) Join ΒΆ added in v1.12.0

func (o OopsErrorBuilder) Join(e ...error) error

func (OopsErrorBuilder) Owner ΒΆ

func (o OopsErrorBuilder) Owner(owner string) OopsErrorBuilder

Owner set the name/email of the collegue/team responsible for handling this error. Useful for alerting purpose.

func (OopsErrorBuilder) Public ΒΆ added in v1.13.0

func (o OopsErrorBuilder) Public(public string) OopsErrorBuilder

Public represents a message that is safe to be shown to an end-user.

func (OopsErrorBuilder) Recover ΒΆ

func (o OopsErrorBuilder) Recover(cb func()) (err error)

Recover handle panic and returns `oops.OopsError` object that satisfies `error`.

func (OopsErrorBuilder) Recoverf ΒΆ

func (o OopsErrorBuilder) Recoverf(cb func(), msg string, args ...any) (err error)

Recoverf handle panic and returns `oops.OopsError` object that satisfies `error` and formats an error message.

func (OopsErrorBuilder) Request ΒΆ added in v1.3.0

func (o OopsErrorBuilder) Request(req *http.Request, withBody bool) OopsErrorBuilder

Request supplies a http.Request.

func (OopsErrorBuilder) Response ΒΆ added in v1.3.0

func (o OopsErrorBuilder) Response(res *http.Response, withBody bool) OopsErrorBuilder

Response supplies a http.Response.

func (OopsErrorBuilder) Since ΒΆ

Since set the error duration.

func (OopsErrorBuilder) Span ΒΆ

Span represents a unit of work or operation.

func (OopsErrorBuilder) Tags ΒΆ

func (o OopsErrorBuilder) Tags(tags ...string) OopsErrorBuilder

Tags adds multiple tags, describing the feature returning an error.

func (OopsErrorBuilder) Tenant ΒΆ

func (o OopsErrorBuilder) Tenant(tenantID string, tenantData ...any) OopsErrorBuilder

Tenant supplies tenant id and a chain of key/value.

func (OopsErrorBuilder) Time ΒΆ

Time set the error time. Default: `time.Now()`

func (OopsErrorBuilder) Trace ΒΆ

func (o OopsErrorBuilder) Trace(trace string) OopsErrorBuilder

Trace set a transaction id, trace id or correlation id...

func (OopsErrorBuilder) User ΒΆ

func (o OopsErrorBuilder) User(userID string, userData ...any) OopsErrorBuilder

User supplies user id and a chain of key/value.

func (OopsErrorBuilder) With ΒΆ

func (o OopsErrorBuilder) With(kv ...any) OopsErrorBuilder

With supplies a list of attributes declared by pair of key+value.

func (OopsErrorBuilder) WithContext ΒΆ added in v1.8.0

func (o OopsErrorBuilder) WithContext(ctx context.Context, keys ...any) OopsErrorBuilder

WithContext supplies a list of values declared in context.

func (OopsErrorBuilder) Wrap ΒΆ

func (o OopsErrorBuilder) Wrap(err error) error

Wrap wraps an error into an `oops.OopsError` object that satisfies `error`

func (OopsErrorBuilder) Wrapf ΒΆ

func (o OopsErrorBuilder) Wrapf(err error, format string, args ...any) error

Wrapf wraps an error into an `oops.OopsError` object that satisfies `error` and formats an error message.

Directories ΒΆ

Path Synopsis
examples
log Module
logrus Module
panic Module
segfault Module
slog Module
sources Module
zerolog Module
loggers
logrus Module
recovery
gin Module

Jump to

Keyboard shortcuts

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