interrors

package module
v0.0.0-...-cdd8c79 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2018 License: MIT Imports: 1 Imported by: 6

README

internal errors

The package interrors is an addition to the juju/errors; interrors provides helpers to handle internal errors which should not be reported to the user.

When building, say, a backend service, we often want to distinguish public errors (e.g. caused by the invalid input received from a client) and internal errors, like the failure to execute SQL query or something else. In the former case, the client should be provided with the descriptive error message like handling bookmarks: saving bookmark "foo bar": URL can't be empty. In the latter case though, the client should only get internal error, and we want to log the exact (and descriptive) internal error for us to examine.

Consider:

errOrig := errors.New("specific error")

err := errOrig
err = errors.Annotatef(err, "some context")
err = errors.Annotatef(err, "some more context")

fmt.Println(err) // Prints "some more context: some context: specific error"

Now, if it was actually an internal error which we don't want to show to a client, here's what we can do:

internalServerError := errors.New("internal server error")
err = interrors.WrapInternalError(err, internalServerError)

So from now on, err can be further wrapped using errors.Annotatef() or friends, but it will behave like the cause was internalServerError, not errOrig.

err = errors.Annotatef(err, "some public context")

fmt.Println(err)  // Prints "some public context: internal server error"
errors.Cause(err) // Returns internalServerError
errors.ErrorStack(err) // Returns stack up to internalServerError, not further

And, of course, there are helpers to get the details of an internal error back.

fmt.Println(interrors.InternalErr(err))   // Prints "some more context: some context: specific error"
fmt.Println(interrors.InternalCause(err)) // Prints "specific error"
fmt.Println(interrors.ErrorStack(err))    // Prints full error stack, from "some public context" to "specific error"

The error stack printed by interrors.ErrorStack(err) would look like:

/path/to/file.go:46: specific error
/path/to/file.go:49: some context
/path/to/file.go:50: some more context
internal server error
/path/to/file.go:55: some public context

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorStack

func ErrorStack(err error) string

ErrorStack is similar to errors.ErrorStack, but it also includes the stack of the internal error, if given err is an internal error wrapper.

func InternalCause

func InternalCause(err error) error

InternalCause is like InternalErr, but it returns the original cause of the error.

func InternalErr

func InternalErr(err error) error

InternalErr takes an error, and if it's an internal error wrapper, returns the underlying internal error (which might still be wrapped with errors.Trace() or some such). Otherwise, just returns the given err back.

err is an "internal error wrapper" if it's a value returned from WrapInternalError, which can be further wrapped into errors.Trace() or friends.

func IsInternalError

func IsInternalError(err error) bool

IsInternalError reports whether the given err is an internal error wrapper.

func WrapInternalError

func WrapInternalError(
	intError, pubError error,
) error

WrapInternalError wraps two errors: internal intError and external pubError. All standard operations on the resulting error will behave as if pubError was the original cause, thus hiding the internal error, which is only retrievable with the accessor functions below.

func WrapInternalErrorf

func WrapInternalErrorf(
	intError error, pubMessageFormat string, args ...interface{},
) error

WrapInternalErrorf takes an internal error, creates a new (public) error with the formatted message, and calls WrapInternalError with those two errors.

Types

This section is empty.

Jump to

Keyboard shortcuts

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