Documentation
ΒΆ
Index ΒΆ
- Variables
- func Errorf(format string, args ...any) error
- func GetPublic(err error, defaultPublicMessage string) string
- func Join(e ...error) error
- func New(message string) error
- func Recover(cb func()) (err error)
- func Recoverf(cb func(), msg string, args ...any) (err error)
- func WithBuilder(ctx context.Context, builder OopsErrorBuilder) context.Context
- func Wrap(err error) error
- 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[A any](a A, err error) (A, error)
- func Wrap3[A any, B any](a A, b B, err error) (A, B, error)
- func Wrap4[A any, B any, C any](a A, b B, c C, err error) (A, B, C, error)
- 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[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[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[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[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(err error, format string, args ...any) error
- 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, ...) (A, B, C, D, E, F, G, H, I, error)
- func Wrapf2[A any](a A, err error, format string, args ...any) (A, error)
- func Wrapf3[A any, B any](a A, b B, err error, format string, args ...any) (A, B, error)
- 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[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[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[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[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[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)
- type OopsError
- func (o OopsError) Code() string
- func (o OopsError) Context() map[string]any
- func (o OopsError) Domain() string
- func (o OopsError) Duration() time.Duration
- func (o OopsError) Error() string
- func (o OopsError) Format(s fmt.State, verb rune)
- func (o OopsError) HasTag(tag string) bool
- func (o OopsError) Hint() string
- func (c OopsError) Is(err error) bool
- func (o OopsError) LogValue() slog.Value
- func (o OopsError) LogValuer() slog.Valuedeprecated
- func (o OopsError) MarshalJSON() ([]byte, error)
- func (o OopsError) Owner() string
- func (o OopsError) Public() string
- func (o OopsError) Request() *http.Request
- func (o OopsError) Response() *http.Response
- func (o OopsError) Sources() string
- func (o OopsError) Span() string
- func (o OopsError) StackFrames() []runtime.Frame
- func (o OopsError) Stacktrace() string
- func (o OopsError) Tags() []string
- func (o OopsError) Tenant() (string, map[string]any)
- func (o OopsError) Time() time.Time
- func (o OopsError) ToMap() map[string]any
- func (o OopsError) Trace() string
- func (o OopsError) Unwrap() error
- func (o OopsError) User() (string, map[string]any)
- type OopsErrorBuilder
- func Assert(condition bool) OopsErrorBuilder
- func Assertf(condition bool, msg string, args ...any) OopsErrorBuilder
- func Code(code string) OopsErrorBuilder
- func Duration(duration time.Duration) OopsErrorBuilder
- func FromContext(ctx context.Context) OopsErrorBuilder
- func Hint(hint string) OopsErrorBuilder
- func In(domain string) OopsErrorBuilder
- func Owner(owner string) OopsErrorBuilder
- func Public(public string) OopsErrorBuilder
- func Request(req *http.Request, withBody bool) OopsErrorBuilder
- func Response(res *http.Response, withBody bool) OopsErrorBuilder
- func Since(time time.Time) OopsErrorBuilder
- func Span(span string) OopsErrorBuilder
- func Tags(tags ...string) OopsErrorBuilder
- func Tenant(tenantID string, data map[string]any) OopsErrorBuilder
- func Time(time time.Time) OopsErrorBuilder
- func Trace(trace string) OopsErrorBuilder
- func User(userID string, data map[string]any) OopsErrorBuilder
- func With(kv ...any) OopsErrorBuilder
- func WithContext(ctx context.Context, keys ...any) OopsErrorBuilder
- func (o OopsErrorBuilder) Assert(condition bool) OopsErrorBuilder
- func (o OopsErrorBuilder) Assertf(condition bool, msg string, args ...any) OopsErrorBuilder
- func (o OopsErrorBuilder) Code(code string) OopsErrorBuilder
- func (o OopsErrorBuilder) Duration(duration time.Duration) OopsErrorBuilder
- func (o OopsErrorBuilder) Errorf(format string, args ...any) error
- func (o OopsErrorBuilder) Hint(hint string) OopsErrorBuilder
- func (o OopsErrorBuilder) In(domain string) OopsErrorBuilder
- func (o OopsErrorBuilder) Join(e ...error) error
- func (o OopsErrorBuilder) New(message string) error
- func (o OopsErrorBuilder) Owner(owner string) OopsErrorBuilder
- func (o OopsErrorBuilder) Public(public string) OopsErrorBuilder
- func (o OopsErrorBuilder) Recover(cb func()) (err error)
- func (o OopsErrorBuilder) Recoverf(cb func(), msg string, args ...any) (err error)
- func (o OopsErrorBuilder) Request(req *http.Request, withBody bool) OopsErrorBuilder
- func (o OopsErrorBuilder) Response(res *http.Response, withBody bool) OopsErrorBuilder
- func (o OopsErrorBuilder) Since(t time.Time) OopsErrorBuilder
- func (o OopsErrorBuilder) Span(span string) OopsErrorBuilder
- func (o OopsErrorBuilder) Tags(tags ...string) OopsErrorBuilder
- func (o OopsErrorBuilder) Tenant(tenantID string, tenantData ...any) OopsErrorBuilder
- func (o OopsErrorBuilder) Time(time time.Time) OopsErrorBuilder
- func (o OopsErrorBuilder) Trace(trace string) OopsErrorBuilder
- func (o OopsErrorBuilder) User(userID string, userData ...any) OopsErrorBuilder
- func (o OopsErrorBuilder) With(kv ...any) OopsErrorBuilder
- func (o OopsErrorBuilder) WithContext(ctx context.Context, keys ...any) OopsErrorBuilder
- func (o OopsErrorBuilder) Wrap(err error) error
- func (o OopsErrorBuilder) Wrapf(err error, format string, args ...any) error
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // SourceFragmentsHidden controls whether source code fragments are included in error output. // When true, source code context around error locations is hidden to reduce output size. SourceFragmentsHidden = true // DereferencePointers controls whether pointer values in error context are automatically // dereferenced when converting to map representations. This can be useful for logging // but may cause issues with nil pointers. DereferencePointers = true // Local specifies the timezone used for error timestamps. Defaults to UTC. Local *time.Location = time.UTC )
Global configuration variables that control the behavior of error handling.
var ( // StackTraceMaxDepth controls the maximum number of stack frames // to capture in a stack trace. This prevents stack traces from // becoming excessively long while still providing sufficient // context for debugging. // // The default value of 10 provides a good balance between // detail and readability. For deep call stacks, this will // capture the most recent 10 frames, which typically include // the most relevant debugging information. StackTraceMaxDepth = 10 )
Global configuration for stack trace generation.
Functions ΒΆ
func GetPublic ΒΆ
GetPublic returns a message that is safe to show to an end user, or a default generic message.
func Recover ΒΆ
func Recover(cb func()) (err error)
Recover handle panic and returns `oops.OopsError` object that satisfies `error`.
func Recoverf ΒΆ
Recoverf handle panic and returns `oops.OopsError` object that satisfies `error` and formats an error message.
func WithBuilder ΒΆ
func WithBuilder(ctx context.Context, builder OopsErrorBuilder) context.Context
WithBuilder stores an OopsErrorBuilder in a Go context for later retrieval. This function creates a new context with the builder stored under the package's internal key, allowing the builder to be accessed by subsequent middleware or handlers in the request chain.
This is particularly useful in web applications where you want to propagate error context (like request IDs, user information, or tracing data) through the entire request processing pipeline without explicitly passing the builder to every function.
The original context is not modified; a new context is returned with the builder added. This follows Go's context immutability pattern.
Example usage:
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Create a builder with request-specific context
builder := oops.
Trace(r.Header.Get("X-Request-ID")).
With("user_agent", r.UserAgent())
// Store the builder in the request context
ctx := oops.WithBuilder(r.Context(), builder)
// Pass the enhanced context to the next handler
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func handler(w http.ResponseWriter, r *http.Request) {
// Retrieve the builder from context and create an error
err := oops.FromContext(r.Context()).
In("handler").
Errorf("something went wrong")
// The error will automatically include the trace ID and user agent
// that were set in the middleware
}
func Wrap10 ΒΆ
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)
Wrap10 wraps an error while preserving nine return values. This function is useful for functions that return (value1, value2, value3, value4, value5, value6, value7, value8, value9, error) and need the error to be wrapped with oops error handling.
The function takes nine values of any types A, B, C, D, E, F, G, H, and I and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
func Wrap2 ΒΆ
Wrap2 wraps an error while preserving a single return value. This function is useful for functions that return (value, error) pairs and need the error to be wrapped with oops error handling.
The function takes a value of any type A and an error, wraps the error using oops.Wrap, and returns the original value along with the wrapped error.
Example usage:
func getUser(id string) (User, error) {
user, err := database.GetUser(id)
return oops.Wrap2(user, err)
}
// Equivalent to:
func getUser(id string) (User, error) {
user, err := database.GetUser(id)
if err != nil {
return user, oops.Wrap(err)
}
return user, nil
}
func Wrap3 ΒΆ
Wrap3 wraps an error while preserving two return values. This function is useful for functions that return (value1, value2, error) and need the error to be wrapped with oops error handling.
The function takes two values of any types A and B and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
Example usage:
func getUserAndProfile(id string) (User, Profile, error) {
user, profile, err := database.GetUserAndProfile(id)
return oops.Wrap3(user, profile, err)
}
func Wrap4 ΒΆ
Wrap4 wraps an error while preserving three return values. This function is useful for functions that return (value1, value2, value3, error) and need the error to be wrapped with oops error handling.
The function takes three values of any types A, B, and C and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
Example usage:
func getUserProfileAndSettings(id string) (User, Profile, Settings, error) {
user, profile, settings, err := database.GetUserProfileAndSettings(id)
return oops.Wrap4(user, profile, settings, err)
}
func Wrap5 ΒΆ
Wrap5 wraps an error while preserving four return values. This function is useful for functions that return (value1, value2, value3, value4, error) and need the error to be wrapped with oops error handling.
The function takes four values of any types A, B, C, and D and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
func Wrap6 ΒΆ
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)
Wrap6 wraps an error while preserving five return values. This function is useful for functions that return (value1, value2, value3, value4, value5, error) and need the error to be wrapped with oops error handling.
The function takes five values of any types A, B, C, D, and E and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
func Wrap7 ΒΆ
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)
Wrap7 wraps an error while preserving six return values. This function is useful for functions that return (value1, value2, value3, value4, value5, value6, error) and need the error to be wrapped with oops error handling.
The function takes six values of any types A, B, C, D, E, and F and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
func Wrap8 ΒΆ
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)
Wrap8 wraps an error while preserving seven return values. This function is useful for functions that return (value1, value2, value3, value4, value5, value6, value7, error) and need the error to be wrapped with oops error handling.
The function takes seven values of any types A, B, C, D, E, F, and G and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
func Wrap9 ΒΆ
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)
Wrap9 wraps an error while preserving eight return values. This function is useful for functions that return (value1, value2, value3, value4, value5, value6, value7, value8, error) and need the error to be wrapped with oops error handling.
The function takes eight values of any types A, B, C, D, E, F, G, and H and an error, wraps the error using oops.Wrap, and returns the original values along with the wrapped error.
func Wrapf ΒΆ
Wrapf wraps an error into an `oops.OopsError` object that satisfies `error` and formats an error message.
func Wrapf10 ΒΆ
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)
Wrapf10 wraps an error with a formatted message while preserving nine return values. This function is similar to Wrap10 but uses oops.Wrapf to add additional context to the error message.
The function takes nine values of any types A, B, C, D, E, F, G, H, and I, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
func Wrapf2 ΒΆ
Wrapf2 wraps an error with a formatted message while preserving a single return value. This function is similar to Wrap2 but uses oops.Wrapf to add additional context to the error message.
The function takes a value of any type A, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original value along with the wrapped error.
Example usage:
func getUser(id string) (User, error) {
user, err := database.GetUser(id)
return oops.Wrapf2(user, err, "failed to get user with id %s", id)
}
func Wrapf3 ΒΆ
Wrapf3 wraps an error with a formatted message while preserving two return values. This function is similar to Wrap3 but uses oops.Wrapf to add additional context to the error message.
The function takes two values of any types A and B, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
Example usage:
func getUserAndProfile(id string) (User, Profile, error) {
user, profile, err := database.GetUserAndProfile(id)
return oops.Wrapf3(user, profile, err, "failed to get user and profile for id %s", id)
}
func Wrapf4 ΒΆ
func Wrapf4[A any, B any, C any](a A, b B, c C, err error, format string, args ...any) (A, B, C, error)
Wrapf4 wraps an error with a formatted message while preserving three return values. This function is similar to Wrap4 but uses oops.Wrapf to add additional context to the error message.
The function takes three values of any types A, B, and C, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
func Wrapf5 ΒΆ
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)
Wrapf5 wraps an error with a formatted message while preserving four return values. This function is similar to Wrap5 but uses oops.Wrapf to add additional context to the error message.
The function takes four values of any types A, B, C, and D, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
func Wrapf6 ΒΆ
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)
Wrapf6 wraps an error with a formatted message while preserving five return values. This function is similar to Wrap6 but uses oops.Wrapf to add additional context to the error message.
The function takes five values of any types A, B, C, D, and E, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
func Wrapf7 ΒΆ
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)
Wrapf7 wraps an error with a formatted message while preserving six return values. This function is similar to Wrap7 but uses oops.Wrapf to add additional context to the error message.
The function takes six values of any types A, B, C, D, E, and F, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
func Wrapf8 ΒΆ
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)
Wrapf8 wraps an error with a formatted message while preserving seven return values. This function is similar to Wrap8 but uses oops.Wrapf to add additional context to the error message.
The function takes seven values of any types A, B, C, D, E, F, and G, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
func Wrapf9 ΒΆ
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)
Wrapf9 wraps an error with a formatted message while preserving eight return values. This function is similar to Wrap9 but uses oops.Wrapf to add additional context to the error message.
The function takes eight values of any types A, B, C, D, E, F, G, and H, an error, and a format string with arguments, wraps the error using oops.Wrapf, and returns the original values along with the wrapped error.
Types ΒΆ
type OopsError ΒΆ
type OopsError struct {
// contains filtered or unexported fields
}
OopsError represents an enhanced error with additional contextual information. It implements the standard error interface while providing rich metadata for debugging, logging, and error handling.
func AsOops ΒΆ
AsOops checks if an error is an oops.OopsError instance and returns it if so. This function is an alias to errors.As and provides a convenient way to type-assert errors to oops.OopsError without importing the errors package.
This function is useful when you need to access the rich metadata and context information stored in oops.OopsError instances, such as error codes, stacktraces, user information, or custom context data.
Example usage:
err := someFunction()
if oopsErr, ok := oops.AsOops(err); ok {
// Access oops-specific information
fmt.Printf("Error code: %s\n", oopsErr.Code())
fmt.Printf("Domain: %s\n", oopsErr.Domain())
fmt.Printf("Stacktrace: %s\n", oopsErr.Stacktrace())
// Check for specific tags
if oopsErr.HasTag("critical") {
// Handle critical errors differently
sendAlert(oopsErr)
}
}
// Chain with other error handling
if oopsErr, ok := oops.AsOops(err); ok && oopsErr.Code() == "database_error" {
// Handle database errors specifically
retryOperation()
}
func (OopsError) Code ΒΆ
Code returns the error code from the deepest error in the chain. Error codes are machine-readable identifiers that can be used for programmatic error handling and cross-service error correlation.
func (OopsError) Context ΒΆ
Context returns a flattened key-value context map from the error chain. Context from all errors in the chain is merged, with later errors taking precedence. Pointer values are dereferenced if DereferencePointers is enabled. Lazy evaluation functions are executed to get their values.
func (OopsError) Domain ΒΆ
Domain returns the domain/feature category of the error. Returns the domain from the deepest error in the chain.
func (OopsError) Duration ΒΆ
Duration returns the duration associated with the error. Returns the duration from the deepest error in the chain.
func (OopsError) Error ΒΆ
Error returns the error message without additional context. This method implements the error interface. If the error wraps another error, it returns "message: wrapped_error". Otherwise, it returns just the message.
func (OopsError) Format ΒΆ
Format implements the fmt.Formatter interface for custom formatting. Supports the following format verbs: - %v: standard error message - %+v: verbose format with stack trace and context - %#v: Go syntax representation.
func (OopsError) HasTag ΒΆ
HasTag checks if the error or any of its wrapped errors contain the specified tag. This is useful for conditional error handling based on error categories.
func (OopsError) Hint ΒΆ
Hint returns a debugging hint for resolving the error. Returns the hint from the deepest error in the chain.
func (OopsError) Is ΒΆ
Is checks if this error matches the target error. This method implements the errors.Is interface for error comparison.
func (OopsError) LogValue ΒΆ
LogValue returns a slog.Value representation of the error for structured logging. This method implements the slog.LogValuer interface and provides a flattened representation of the error's context and metadata suitable for logging systems.
func (OopsError) MarshalJSON ΒΆ
MarshalJSON implements the json.Marshaler interface. This allows OopsError to be directly serialized to JSON.
func (OopsError) Owner ΒΆ
Owner returns the name/email of the person/team responsible for handling this error. Returns the owner from the deepest error in the chain.
func (OopsError) Public ΒΆ
Public returns a user-safe error message. Returns the public message from the deepest error in the chain.
func (OopsError) Request ΒΆ
Request returns the associated HTTP request. Returns the request from the deepest error in the chain.
func (OopsError) Response ΒΆ
Response returns the associated HTTP response. Returns the response from the deepest error in the chain.
func (OopsError) Sources ΒΆ
Sources returns formatted source code fragments around the error location. This provides context about the code that caused the error, which is particularly useful for debugging. The output includes line numbers and highlights the exact line where the error occurred.
func (OopsError) Span ΒΆ
Span returns the current span identifier. Unlike other attributes, span returns the current error's span, not the deepest one.
func (OopsError) StackFrames ΒΆ
StackFrames returns the raw stack frames as runtime.Frame objects. This is useful for custom stack trace formatting or analysis.
func (OopsError) Stacktrace ΒΆ
Stacktrace returns a formatted string representation of the error's stack trace. The stack trace shows the call hierarchy leading to the error, excluding frames from the Go standard library and this package. The stacktrace is basically written from the bottom to the top, in order to dedup frames. It support recursive code.
func (OopsError) Tags ΒΆ
Tags returns all unique tags from the error chain. Tags are merged from all errors in the chain and deduplicated.
func (OopsError) Tenant ΒΆ
Tenant returns the tenant ID and associated tenant data. Returns the tenant information from the deepest error in the chain.
func (OopsError) Time ΒΆ
Time returns the timestamp when the error occurred. Returns the time from the deepest error in the chain.
func (OopsError) ToMap ΒΆ
ToMap converts the error to a map representation suitable for JSON serialization. This method provides a flattened view of all error attributes and is useful for logging, debugging, and cross-service error transmission.
func (OopsError) Trace ΒΆ
Trace returns the transaction/trace/correlation ID. If no trace ID is set, generates a new ULID-based trace ID. Returns the trace ID from the deepest error in the chain.
type OopsErrorBuilder ΒΆ
type OopsErrorBuilder OopsError
OopsErrorBuilder implements the builder pattern for creating OopsError instances. It provides a fluent API for setting error attributes and creating error objects. The builder is designed to be chainable, allowing multiple method calls in sequence.
func Assert ΒΆ
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 ΒΆ
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 intended 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 FromContext ΒΆ
func FromContext(ctx context.Context) OopsErrorBuilder
func Owner ΒΆ
func Owner(owner string) OopsErrorBuilder
Owner set the name/email of the colleague/team responsible for handling this error. Useful for alerting purpose.
func Public ΒΆ
func Public(public string) OopsErrorBuilder
Public sets a message that is safe to show to an end user.
func Request ΒΆ
func Request(req *http.Request, withBody bool) OopsErrorBuilder
Request supplies a http.Request.
func Response ΒΆ
func Response(res *http.Response, withBody bool) OopsErrorBuilder
Response supplies a http.Response.
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 ΒΆ
func WithContext(ctx context.Context, keys ...any) OopsErrorBuilder
WithContext supplies a list of values declared in context.
func (OopsErrorBuilder) Assert ΒΆ
func (o OopsErrorBuilder) Assert(condition bool) OopsErrorBuilder
Assert panics if the condition is false. This method provides a way to add assertions to code that will panic with an OopsError if the condition fails. The assertion can be chained with other builder methods.
Example:
oops.
Code("assertion_failed").
Assert(userID != "", "user ID cannot be empty").
Assert(email != "", "user email cannot be empty").
Assert(orgID != "", "user organization ID cannot be empty")
func (OopsErrorBuilder) Assertf ΒΆ
func (o OopsErrorBuilder) Assertf(condition bool, msg string, args ...any) OopsErrorBuilder
Assertf panics if the condition is false with a custom message. Similar to Assert, but allows for a custom formatted message when the assertion fails.
Example:
oops.
Code("assertion_failed").
Assertf(userID != "", "user ID cannot be empty, got: %s", userID).
Assertf(email != "", "user email cannot be empty, got: %s", email).
Assertf(orgID != "", "user organization ID cannot be empty, got: %s", orgID)
func (OopsErrorBuilder) Code ΒΆ
func (o OopsErrorBuilder) Code(code string) OopsErrorBuilder
Code sets a machine-readable error code or slug. Error codes are useful for programmatic error handling and cross-service error correlation. They should be consistent and well-documented.
Example:
oops.Code("database_connection_failed").Errorf("connection timeout")
func (OopsErrorBuilder) Duration ΒΆ
func (o OopsErrorBuilder) Duration(duration time.Duration) OopsErrorBuilder
Duration sets the duration associated with the error. This is useful for errors that are related to timeouts or performance issues.
Example:
oops.Duration(5 * time.Second).Errorf("request timeout")
func (OopsErrorBuilder) Errorf ΒΆ
func (o OopsErrorBuilder) Errorf(format string, args ...any) error
Errorf creates a new error with a formatted message. Similar to New, but allows for formatted messages using printf-style formatting.
Example:
err := oops.
Code("validation_error").
Errorf("invalid input: expected %s, got %s", expectedType, actualType)
func (OopsErrorBuilder) Hint ΒΆ
func (o OopsErrorBuilder) Hint(hint string) OopsErrorBuilder
Hint provides a debugging hint for resolving the error. Hints should provide actionable guidance for developers.
Example:
oops.Hint("Check database connection and credentials").Errorf("connection failed")
func (OopsErrorBuilder) In ΒΆ
func (o OopsErrorBuilder) In(domain string) OopsErrorBuilder
In sets the domain or feature category for the error. Domains help categorize errors by the part of the system they relate to.
Example:
oops.In("database").Errorf("connection failed")
func (OopsErrorBuilder) Join ΒΆ
func (o OopsErrorBuilder) Join(e ...error) error
Join combines multiple errors into a single error. This method uses the standard errors.Join function to combine multiple errors while preserving the builder's context.
Example:
err := oops.
Code("multi_error").
Join(err1, err2, err3)
func (OopsErrorBuilder) New ΒΆ
func (o OopsErrorBuilder) New(message string) error
New creates a new error with the specified message. This method creates a simple error without wrapping an existing one. The message is treated as the primary error message.
Example:
err := oops.
Code("validation_error").
New("invalid input parameters")
func (OopsErrorBuilder) Owner ΒΆ
func (o OopsErrorBuilder) Owner(owner string) OopsErrorBuilder
Owner sets the person or team responsible for handling this error. This is useful for alerting and error routing.
Example:
oops.Owner("database-team@company.com").Errorf("connection failed")
func (OopsErrorBuilder) Public ΒΆ
func (o OopsErrorBuilder) Public(public string) OopsErrorBuilder
Public sets a user-safe error message. This message should be safe to display to end users without exposing internal system details.
Example:
oops.Public("Unable to process your request").Errorf("internal server error")
func (OopsErrorBuilder) Recover ΒΆ
func (o OopsErrorBuilder) Recover(cb func()) (err error)
Recover handles panics and converts them to OopsError instances. This method executes the provided callback function and catches any panics, converting them to properly formatted OopsError instances with stack traces. If the panic payload is already an error, it wraps that error. Otherwise, it creates a new error from the panic value.
Example:
err := oops.
Code("panic_recovered").
Recover(func() {
// Potentially panicking code
riskyOperation()
})
func (OopsErrorBuilder) Recoverf ΒΆ
func (o OopsErrorBuilder) Recoverf(cb func(), msg string, args ...any) (err error)
Recoverf handles panics with additional context message. Similar to Recover, but adds a formatted message to describe the context in which the panic occurred.
Example:
err := oops.
Code("panic_recovered").
Recoverf(func() {
riskyOperation()
}, "panic in operation: %s", operationName)
func (OopsErrorBuilder) Request ΒΆ
func (o OopsErrorBuilder) Request(req *http.Request, withBody bool) OopsErrorBuilder
Request adds HTTP request information to the error context. The withBody parameter controls whether the request body is included. Including request bodies may impact performance and memory usage.
Example:
oops.Request(req, true).Errorf("request processing failed")
func (OopsErrorBuilder) Response ΒΆ
func (o OopsErrorBuilder) Response(res *http.Response, withBody bool) OopsErrorBuilder
Response adds HTTP response information to the error context. The withBody parameter controls whether the response body is included. Including response bodies may impact performance and memory usage.
Example:
oops.Response(res, false).Errorf("response processing failed")
func (OopsErrorBuilder) Since ΒΆ
func (o OopsErrorBuilder) Since(t time.Time) OopsErrorBuilder
Since calculates the duration since the specified time. This is useful for measuring how long an operation took before failing.
Example:
start := time.Now()
// ... perform operation ...
oops.Since(start).Errorf("operation timed out")
func (OopsErrorBuilder) Span ΒΆ
func (o OopsErrorBuilder) Span(span string) OopsErrorBuilder
Span sets the current span identifier. Spans represent units of work and are useful for distributed tracing.
Example:
oops.Span("database-query").Errorf("query failed")
func (OopsErrorBuilder) Tags ΒΆ
func (o OopsErrorBuilder) Tags(tags ...string) OopsErrorBuilder
Tags adds multiple tags for categorizing the error. Tags are useful for filtering and grouping errors in monitoring systems.
Example:
oops.Tags("auth", "permission", "critical").Errorf("access denied")
func (OopsErrorBuilder) Tenant ΒΆ
func (o OopsErrorBuilder) Tenant(tenantID string, tenantData ...any) OopsErrorBuilder
Tenant adds tenant information to the error context. This method accepts a tenant ID followed by key-value pairs for tenant data.
Example:
oops.Tenant("tenant-456", "name", "Acme Corp", "plan", "premium").Errorf("quota exceeded")
func (OopsErrorBuilder) Time ΒΆ
func (o OopsErrorBuilder) Time(time time.Time) OopsErrorBuilder
Time sets the timestamp when the error occurred. If not set, the error will use the current time when created.
Example:
oops.Time(time.Now()).Errorf("operation failed")
func (OopsErrorBuilder) Trace ΒΆ
func (o OopsErrorBuilder) Trace(trace string) OopsErrorBuilder
Trace sets a transaction, trace, or correlation ID. This is useful for distributed tracing and correlating errors across services.
Example:
oops.Trace("req-123-456").Errorf("service call failed")
func (OopsErrorBuilder) User ΒΆ
func (o OopsErrorBuilder) User(userID string, userData ...any) OopsErrorBuilder
User adds user information to the error context. This method accepts a user ID followed by key-value pairs for user data.
Example:
oops.User("user-123", "firstname", "John", "lastname", "Doe").Errorf("permission denied")
func (OopsErrorBuilder) With ΒΆ
func (o OopsErrorBuilder) With(kv ...any) OopsErrorBuilder
With adds key-value pairs to the error context. Context values are useful for debugging and provide additional information about the error. Values can be of any type and will be serialized appropriately.
Performance: Context values are stored in a map and processed during error creation. Large numbers of context values may impact performance later, but not during error creation.
Example:
oops.With("user_id", 123, "operation", "create").Errorf("validation failed")
func (OopsErrorBuilder) WithContext ΒΆ
func (o OopsErrorBuilder) WithContext(ctx context.Context, keys ...any) OopsErrorBuilder
WithContext extracts values from a Go context and adds them to the error context. This is useful for propagating context values through error chains.
Example:
oops.WithContext(ctx, "request_id", "user_id").Errorf("operation failed")
func (OopsErrorBuilder) Wrap ΒΆ
func (o OopsErrorBuilder) Wrap(err error) error
Wrap wraps an existing error into an OopsError with the current builder's context. If the input error is nil, returns nil. Otherwise, creates a new OopsError that wraps the original error while preserving all the contextual information set in the builder.
Example:
err := oops.
Code("database_error").
In("database").
Wrap(originalError)
func (OopsErrorBuilder) Wrapf ΒΆ
func (o OopsErrorBuilder) Wrapf(err error, format string, args ...any) error
Wrapf wraps an existing error with additional formatted message. Similar to Wrap, but adds a formatted message that describes the context in which the error occurred.
Example:
err := oops.
Code("database_error").
In("database").
Wrapf(originalError, "failed to execute query: %s", queryName)