errorstack

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 1 Imported by: 8

Documentation

Overview

Package errorstack exposes a set of utilities for consistent error handling in the helix ecosystem. Every package in helix.go relies on this one.

This package must not import any other package of this ecosystem.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {

	// Integration is the name of the integration returning the error, if applicable.
	// Omit integration when working with JSON: we don't want to give internal
	// information to clients consuming HTTP APIs.
	//
	// Examples:
	//
	//   "nats"
	//   "vault"
	Integration string `json:"-"`

	// Message is the top-level message of the error.
	Message string `json:"message,omitempty"`

	// Validations represents a list of failure validations related to the error
	// itself. This allows to pass/retrieve additional details, such as validation
	// failures encountered in the request payload.
	Validations []Validation `json:"validations,omitempty"`

	// Children holds child errors encountered in cascade related to the current
	// error. Omit children errors when working with JSON: we don't want to give
	// internal information to clients consuming HTTP APIs.
	Children []error `json:"-"`
}

Error implements the Go native error type and is designed for handling errors in the helix ecosystem. When exposing errors to clients (such as via HTTP API), the root error should not give away too much information such as internal messages.

func New

func New(message string, opts ...With) *Error

New returns a new error given the message and options passed.

func NewFromError

func NewFromError(existing error, opts ...With) *Error

NewFromError returns a new error given the existing error and options passed.

func (*Error) Error

func (err *Error) Error() string

Error returns the stringified version of the error, including its validation failures.

func (*Error) HasChildren

func (err *Error) HasChildren() bool

HasChildren indicates if an error caused other (a.k.a. children) errors.

func (*Error) HasValidations

func (err *Error) HasValidations() bool

HasValidations indicates if an error encountered validation failures.

func (*Error) WithChildren

func (err *Error) WithChildren(children ...error) error

WithChildren adds a list of child errors encountered related to the current error.

func (*Error) WithValidations

func (err *Error) WithValidations(validations ...Validation) *Error

WithValidations adds validation failures to an error.

type Validation

type Validation struct {

	// Message is the cause of the validation failure.
	Message string `json:"message"`

	// Path represents the path to the key where the validation failure occurred.
	//
	// Example:
	//
	//   []string{"request", "body", "user", "email"}
	Path []string `json:"path,omitempty"`
}

Validation holds some details about a validation failure.

type With

type With func(*Error)

With allows to set optional values when creating a new error with New.

func WithIntegration

func WithIntegration(inte string) With

WithIntegration sets the integration at the origin of the error.

Jump to

Keyboard shortcuts

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