orerr

package
v1.93.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Description: Bed request error

Description: Custom error with list of details compatible with JSON API spec

Package orerr implements outreach specific error utilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelWithError

func CancelWithError(ctx context.Context) (c context.Context, cancel func(err error))

CancelWithError returns a context and a cancel function where the cancel function can override the error reported by the context.

This function is similar to context.WithCancel except that the cancel function can specify an error. Note that the cancel function can be called with nil args to make it behave exactly like context.WithCancel.

Example
package main

import (
	"context"
	"fmt"

	"github.com/pkg/errors"

	"github.com/getoutreach/gobox/pkg/orerr"
)

func main() {
	origErr := errors.New("something went wrong")
	shutdownErr := &orerr.ShutdownError{Err: origErr}
	ctx, cancel := orerr.CancelWithError(context.Background())
	cancel(shutdownErr)

	fmt.Println("Err", ctx.Err())

}
Output:

Err process has shutdown

func ExtractErrorMetadata added in v1.8.0

func ExtractErrorMetadata(err error) map[string]string

ExtractErrorMetadata returns any embedded grpc metadata in

func ExtractErrorStatusCategory

func ExtractErrorStatusCategory(err error) statuscodes.StatusCategory

func ExtractErrorStatusCode

func ExtractErrorStatusCode(err error) statuscodes.StatusCode

func Info

func Info(err error, info ...log.Marshaler) error

Info adds extra logging info to an error.

func IsErrorStatusCategory

func IsErrorStatusCategory(err error, category statuscodes.StatusCategory) bool

func IsErrorStatusCode

func IsErrorStatusCode(err error, code statuscodes.StatusCode) bool

func IsOneOf

func IsOneOf(err error, errs ...error) bool

IsOneOf returns true if the supplied error is identical to an error supplied in the remaining function error arguments

Example
package main

import (
	"context"
	"fmt"
	"io"

	"github.com/getoutreach/gobox/pkg/orerr"
)

func main() {
	errList := []error{io.EOF, context.Canceled, context.DeadlineExceeded}

	if orerr.IsOneOf(io.EOF, errList...) {
		fmt.Println("io.EOF is part of the error list")
	}

}
Output:

io.EOF is part of the error list

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns whether or not err is retryable.

func Meta added in v1.8.0

func Meta(err error, meta map[string]string) error

Meta adds grpc metadata to an error.

func New

func New(err error, opts ...ErrOption) error

New creates a new error wrapping all the error options onto it.

For convenience, if err is nil, this function returns nil.

Example
package main

import (
	"fmt"

	"github.com/pkg/errors"

	"github.com/getoutreach/gobox/pkg/log"
	"github.com/getoutreach/gobox/pkg/orerr"
)

func main() {
	origErr := errors.New("something went wrong")
	info := log.F{"hello": "world"}
	err := orerr.New(origErr, orerr.WithInfo(info), orerr.WithRetry())

	formatted := log.F{}
	//nolint:errorlint // Why: test
	err.(log.Marshaler).MarshalLog(formatted.Set)
	fmt.Println("Err", err, orerr.IsRetryable(err), formatted)

}
Output:

Err something went wrong true map[hello:world]

func NewBadRequestError added in v1.64.0

func NewBadRequestError(err error, violations ...Violation) error

NewBadRequestError return ready made intance of the BadRequestError error. It wraps given error with the BadRequest status

func NewErrDetails added in v1.81.0

func NewErrDetails(err error, details ...ErrDetail) error

NewErrDetails return ready made intance of the ErrDetails error. It wraps given error

func NewErrorStatus

func NewErrorStatus(errToWrap error, errCode statuscodes.StatusCode) error

func Retryable

func Retryable(err error) *retryable

Retryable wraps err in a type which denotes that the originating process is retryable.

Example
process := func(attempt int) error {
	err := errors.New("something went wrong")
	if attempt < 2 {
		return Retryable(err)
	}

	return err
}

for i := 0; ; i++ {
	if err := process(i); err != nil {
		if IsRetryable(err) {
			fmt.Println("error is retryable")
			continue
		}

		fmt.Println("error is not retryable:", err)
		break
	}
}
Output:

error is retryable
error is retryable
error is not retryable: something went wrong

Types

type BadRequestError added in v1.64.0

type BadRequestError struct {
	// Err is an original err
	Err error

	// Violations particular violations
	Violations []Violation
}

BadRequestError represents an invalidate input error

func (BadRequestError) Error added in v1.64.0

func (e BadRequestError) Error() string

Error implements the err interface.

func (BadRequestError) Unwrap added in v1.64.0

func (e BadRequestError) Unwrap() error

Unwrap returns the inner error.

func (*BadRequestError) WithViolations added in v1.64.0

func (e *BadRequestError) WithViolations(violations ...Violation) *BadRequestError

WithViolations adds more violations into the error

type ErrDetail added in v1.81.0

type ErrDetail struct {
	ID     string
	Title  string
	Detail string
	Code   *string
	Source *ErrSource
	Meta   map[string]string
}

ErrDetail describes detail of an error It's compatible with a single API error as per: https://jsonapi.org/format/#error-objects

func NewErrDetail added in v1.81.0

func NewErrDetail(id, title, detail string) ErrDetail

NewErrDetail creates a new intance of ErrDetail

func (ErrDetail) WithCode added in v1.81.0

func (v ErrDetail) WithCode(code *string) ErrDetail

WithCode allows to specify error code

func (ErrDetail) WithMeta added in v1.81.0

func (v ErrDetail) WithMeta(m map[string]string) ErrDetail

WithField allows to specify violation meta data

func (ErrDetail) WithSourcePointer added in v1.81.0

func (v ErrDetail) WithSourcePointer(pointer string) ErrDetail

WithSourcePointer allows to specify source pointer

type ErrDetails added in v1.81.0

type ErrDetails struct {

	// Details is the list of error details
	Details []ErrDetail
	// contains filtered or unexported fields
}

ErrDetails represents an error with list of details

func (ErrDetails) Error added in v1.81.0

func (e ErrDetails) Error() string

Error implements the err interface.

func (ErrDetails) Unwrap added in v1.81.0

func (e ErrDetails) Unwrap() error

Unwrap returns the inner error.

func (*ErrDetails) WithDetails added in v1.81.0

func (e *ErrDetails) WithDetails(details ...ErrDetail) *ErrDetails

WithDetails adds more details into the error

type ErrOption

type ErrOption func(err error) error

ErrOption just wraps an error.

func WithDetails added in v1.81.0

func WithDetails(details ...ErrDetail) ErrOption

WithDetails calls NewErrorDetails with the given error details.

It is a functional option for use with New.

func WithInfo

func WithInfo(info ...log.Marshaler) ErrOption

WithInfo attaches the provided logs to the error.

It is a functional option for use with New.

func WithMeta added in v1.8.0

func WithMeta(meta map[string]string) ErrOption

WithMeta attaches the provided grpc metadata to the error.

It is a functional option for use with New.

func WithRetry

func WithRetry() ErrOption

WithRetry marks the error as retryable.

It is a functional option for use with New.

func WithStatus added in v1.8.0

func WithStatus(code statuscodes.StatusCode) ErrOption

WithStatus calls NewErrorStatus with the given code.

It is a functional option for use with New.

type ErrSource added in v1.81.0

type ErrSource struct {
	Pointer string
}

ErrSource describes source location of the error

type LimitExceededError

type LimitExceededError struct {
	// Kind refers to the kind of rate whose limit has been exceeded.
	Kind string

	Err error
}

LimitExceededError indicates some limit has exceeded. The actual limit that has exceeded is indicated via the Kind field. An inner error may be provided via Err.

func (LimitExceededError) Error

func (e LimitExceededError) Error() string

Error implements the err interface.

func (LimitExceededError) Unwrap

func (e LimitExceededError) Unwrap() error

Unwrap returns the inner error.

type SentinelError

type SentinelError string

A SentinelError is a constant which ought to be compared using errors.Is.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/getoutreach/gobox/pkg/orerr"
)

const ErrUsernameTaken orerr.SentinelError = "username already taken"

func createUser(_ string) error {
	return ErrUsernameTaken
}

func main() {
	if err := createUser("joe"); err != nil {
		if errors.Is(err, ErrUsernameTaken) {
			fmt.Println("User 'joe' already exists")
			return
		}

		panic(err)
	}

}
Output:

User 'joe' already exists

func (SentinelError) Error

func (s SentinelError) Error() string

Error returns s as a string.

type ShutdownError

type ShutdownError struct {
	Err error
}

ShutdownError incidates the process is shutting down. An inner error may be provided via Err.

func (ShutdownError) Error

func (e ShutdownError) Error() string

Error implements the err interface.

func (ShutdownError) Unwrap

func (e ShutdownError) Unwrap() error

Unwrap returns the inner error.

type StatusCodeWrapper

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

func (*StatusCodeWrapper) Error

func (w *StatusCodeWrapper) Error() string

func (*StatusCodeWrapper) StatusCategory

func (w *StatusCodeWrapper) StatusCategory() statuscodes.StatusCategory

func (*StatusCodeWrapper) StatusCode

func (w *StatusCodeWrapper) StatusCode() statuscodes.StatusCode

func (*StatusCodeWrapper) Unwrap

func (w *StatusCodeWrapper) Unwrap() error

type Violation added in v1.64.0

type Violation struct {
	// Field describes the field path with request object graph.
	// When not provided the violation is related to a whole entitity.
	Field *string

	// Domain is optional identifier of the domain name of the validation subject.
	Domain *string

	// Reason of the error. In most of the cases the validation rule indentifier.
	Reason string

	// Additional structured details about this error. Could be used for localization of the error.
	Metadata map[string]string
}

Violation describe particular input violation. It might be field and domain specific.

func NewViolation added in v1.64.0

func NewViolation(reason string) Violation

NewViolation creates a new intance of Violation

func (Violation) WithDomain added in v1.64.0

func (v Violation) WithDomain(domain string) Violation

WithField allows to specify domain name of the field that violation belongs to

func (Violation) WithField added in v1.64.0

func (v Violation) WithField(field string) Violation

WithField allows to specify field path of the field that violation belongs to

func (Violation) WithMeta added in v1.64.0

func (v Violation) WithMeta(m map[string]string) Violation

WithField allows to specify violation meta data

Jump to

Keyboard shortcuts

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