lusherr

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

LUSH Core Errors

This package is used to streamline dealing with errors and error messages within the LUSH infrastructure. Using the errors provided by this package will collect a lot of useful debug information about where exactly the error occurred.

Error Types

These are the standard error types that can be used within a project's domain logic to aid with debugging and error reporting to any of its API consumers.

Internal Error

InternalError can be used to wrap any error e.g. Trying to generate a random UUID, but the generation failed.

id, err := uuid.NewV4()
if err != nil {
    return NewInternalError(err)
}
Unauthorized Error

UnauthorizedError should be used when an action is performed by a user that they don't have permission to do e.g. Someone tried to access something they were not allowed to according to a permission policy.

if err := policy.Permit(consumer); err != nil {
    return NewUnauthorizedError(err)
}
Validation Error

ValidationError should be used to detail what user generated information is incorrect and why e.g. Someone set the name field for a user to be empty, but the validation requires it to be present.

type ProductRevision struct {
    plu string
}

func (r ProductRevision) validate() error {
    if plu == "" {
        return NewValidationError("product revision", "plu", fmt.Errorf("must be present"))
    }
}
Database Query Error

DatabaseQueryError should be used to provide detail about a failed database query e.g. Trying to query the database, but the database rejects the query.

const stmt = `SELECT * FROM user`
rows, err := qu.Query(stmt)
if err != nil {
    return nil, NewDatabaseQueryError(stmt, err)
}
Not Found Error

NotFoundError should be used when an entity cannot be found e.g. Someone tries to retrieve a user, but the user for the given ID does not exist in the database.

const stmt = `SELECT * FROM user WHERE id = $1`
rows, err := qu.Query(stmt, id)
if err != nil {
    switch err {
    case sql.ErrNoRows:
        return nil, NewNotFoundError("user", id, err)
    default:
        return nil, NewDatabaseQueryError(stmt, err)
    }
}
Not Allowed Error

NotAllowedError should be used when an certain action is not allowed e.g. Someone tries to delete something, but the record has been marked as permanent.

if product.permanent {
    return NewNotAllowedError(fmt.Errorf("not allowed to remove permanent products"))
}

Locate

Errors produced with the lusherr package can be located to return the runtime.Frame of where it occurred.

frame, found := lusherr.Locate(err)
if found {
    log.Println(err, frame)
} else {
    log.Println(err, "frame could not be found")
}
Locator Interface

Implement the Locator interface on an error type to return its caller frame to be used in conjunction with the lusherr package and associated tooling.

type Locator interface {
    Locate() runtime.Frame
}

Pin

Call Pin to wrap an error with information about the caller frame of where Pin was invoked for an error that does not already carry a caller frame. The resulting error can be cast to Locate.

func UploadToBucket(w io.Writer) error {
    if err := upload(w); err != nil {
        return lusherr.Pin(err)
    }
}
Pinner Interface

Implement the Pinner interface on an error type to prevent errors that already implements Locator to be wrapped multiple times.

type Pinner interface {
    Pin(runtime.Frame) error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(err error) string

Debug where an error was raised.

func Locate

func Locate(err error) (runtime.Frame, bool)

Locate where an error was raised.

func NewBadRequestError added in v0.4.1

func NewBadRequestError(inner error) error

NewBadRequestError builds an error for when a client send a bad request. e.g. a client sends a request that is missing a required field.

func NewDatabaseQueryError

func NewDatabaseQueryError(query string, inner error) error

NewDatabaseQueryError builds an error for a failed database query. e.g. Trying to query the database, but the database rejects the query.

func NewInternalError

func NewInternalError(inner error) error

NewInternalError builds a generic error. e.g. Trying to generate a random UUID, but the generation failed.

func NewNotAllowedError

func NewNotAllowedError(inner error) error

NewNotAllowedError builds an error for when a certain action is not allowed. e.g. Someone tries to delete something, but the record has been marked as permanenet.

func NewNotFoundError

func NewNotFoundError(entity string, identifier interface{}, inner error) error

NewNotFoundError builds an error for an entity that cannot be found. e.g. Someone tries to retrieve a user, but the user for the given ID does not exist in the database.

func NewUnauthorizedError

func NewUnauthorizedError(inner error) error

NewUnauthorizedError builds a new unauthorized error. e.g. Someone tried to access something they were not allowed to according to a permission policy.

func NewValidationError

func NewValidationError(entity, field string, inner error) error

NewValidationError builds an error for failing to validate a field. e.g. Someone set the name field for a user to be empty, but the validation requires it to be present.

func Pin

func Pin(err error) error

Pin an error to its caller frame to carry the location in code where the error occurred.

Types

type BadRequestError added in v0.4.1

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

BadRequestError indicates that the server cannot or will not process the request due to something that is perceived to be a client error.

func (BadRequestError) Error added in v0.4.1

func (e BadRequestError) Error() string

func (BadRequestError) Locate added in v0.4.1

func (e BadRequestError) Locate() runtime.Frame

Locate the frame of the error.

func (BadRequestError) Pin added in v0.4.1

func (e BadRequestError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (BadRequestError) Unwrap added in v0.4.1

func (e BadRequestError) Unwrap() error

Unwrap the inner error.

type DatabaseQueryError

type DatabaseQueryError struct {
	Query string
	// contains filtered or unexported fields
}

DatabaseQueryError should be used to provide detail about a failed database query. e.g. Trying to query the database, but the database rejects the query.

func (DatabaseQueryError) Error

func (e DatabaseQueryError) Error() string

func (DatabaseQueryError) Locate

func (e DatabaseQueryError) Locate() runtime.Frame

Locate the frame of the error.

func (DatabaseQueryError) Pin

func (e DatabaseQueryError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (DatabaseQueryError) Unwrap

func (e DatabaseQueryError) Unwrap() error

Unwrap the inner error.

type InternalError

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

InternalError can be used to wrap any error. e.g. Trying to generate a random UUID, but the generation failed.

func (InternalError) Error

func (e InternalError) Error() string

func (InternalError) Locate

func (e InternalError) Locate() runtime.Frame

Locate the frame of the error.

func (InternalError) Pin

func (e InternalError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (InternalError) Unwrap

func (e InternalError) Unwrap() error

Unwrap the inner error.

type Locator

type Locator interface {
	Locate() runtime.Frame
}

Locator defines behavior for locating an error frame.

type NotAllowedError

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

NotAllowedError should be used when an certain action is not allowed. e.g. Someone tries to delete something, but the record has been marked as permanent.

func (NotAllowedError) Error

func (e NotAllowedError) Error() string

func (NotAllowedError) Locate

func (e NotAllowedError) Locate() runtime.Frame

Locate the frame of the error.

func (NotAllowedError) Pin

func (e NotAllowedError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (NotAllowedError) Unwrap

func (e NotAllowedError) Unwrap() error

Unwrap the inner error.

type NotFoundError

type NotFoundError struct {
	Entity     string
	Identifier interface{}
	// contains filtered or unexported fields
}

NotFoundError should be used when an entity cannot be found. e.g. Someone tries to retrieve a user, but the user for the given ID does not exist in the database.

func (NotFoundError) Error

func (e NotFoundError) Error() string

func (NotFoundError) Locate

func (e NotFoundError) Locate() runtime.Frame

Locate the frame of the error.

func (NotFoundError) Pin

func (e NotFoundError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (NotFoundError) Unwrap

func (e NotFoundError) Unwrap() error

Unwrap the inner error.

type Pinner

type Pinner interface {
	Pin(runtime.Frame) error
}

Pinner defines behavior for defining an origin frame for an error.

type UnauthorizedError

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

UnauthorizedError should be used when an action is performed by a user that they don't have permission to do. e.g. Someone tried to access something they were not allowed to according to a permission policy.

func (UnauthorizedError) Error

func (e UnauthorizedError) Error() string

func (UnauthorizedError) Locate

func (e UnauthorizedError) Locate() runtime.Frame

Locate the frame of the error.

func (UnauthorizedError) Pin

func (e UnauthorizedError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (UnauthorizedError) Unwrap

func (e UnauthorizedError) Unwrap() error

Unwrap the inner error.

type ValidationError

type ValidationError struct {
	Entity, Field string
	// contains filtered or unexported fields
}

ValidationError should be used to detail what user generated information is incorrect and why. e.g. Someone set the name field for a user to be empty, but the validation requires it to be present.

func (ValidationError) Error

func (e ValidationError) Error() string

func (ValidationError) Locate

func (e ValidationError) Locate() runtime.Frame

Locate the frame of the error.

func (ValidationError) Pin

func (e ValidationError) Pin(frame runtime.Frame) error

Pin the error to a caller frame.

func (ValidationError) Unwrap

func (e ValidationError) Unwrap() error

Unwrap the inner error.

Jump to

Keyboard shortcuts

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