friendly

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2020 License: MIT Imports: 3 Imported by: 0

README

go-friendly

go-friendly is a go package that lets you accompany your internal error messages with more friendly messages, that are suitable for and end-user to see.

For example, you may have encountered a database connection error that you want to log out, but don't want to return to the user. That message might look like:

error connecting to postgres db with host 127.0.0.1 and username 'my_user'

This might be useful for debugging, but might be too much information to display to your end user. So when generating the error, you can also choose to generate a distinct user-friendly error that might tell them a little about the error and nothing that they don't need to know, e.g. you might show them something like:

We encountered an internal error while trying to process your request. Please try again later :(

Using go-friendly, this would look something like:

import (
  "github.com/dylannz-sailthru/go-friendly"
)

func fetchSomethingFromTheDatabase() error {
  return errors.New("error connecting to postgres db with host 127.0.0.1 and username 'my_user'")
}

func fetchTime() error {
  err := fetchSomethingFromTheDatabase()

  return friendly.New().
    WithCause(err).
    WithFriendly("We encountered an internal error while trying to process your request. Please try again later :(")
}

Now, when you call err.Error(), you'll get see the internal error:

err := fetchTime()
fmt.Println(err.Error()) // error connecting to postgres db with host 127.0.0.1 and username 'my_user'

But if you call friendly.Friendly() on the error, it'll return your more user-friendly error:

err := fetchTime()
if f := friendly.Friendly(err); f != nil {
  fmt.Println(f.Error()) // We encountered an internal error while trying to process your request. Please try again later :(
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultUserError = errors.New("Something went wrong")

Functions

func Friendly

func Friendly(err error) error

Friendly takes any error and will return the first user-friendly error it finds as it traverses up through the linked list. If there are no user-friendly causes found, nil is returned.

func Wrap

func Wrap(cause error, friendly string) error

Wrap is a convience method to easily add a friendly message for an existing error.

func Wrapf

func Wrapf(cause error, friendly string, a ...interface{}) error

Wrapf is a convience method to easily add a friendly message for an existing error.

Types

type Error

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

func New

func New() Error

func (Error) Cause

func (e Error) Cause() error

Cause returns the underlying cause error.

func (Error) Err

func (e Error) Err() error

Err returns the friendly error as an 'error', if an underlying cause is present.

func (Error) Error

func (e Error) Error() string

func (Error) Friendly

func (e Error) Friendly() error

Friendly returns the underlying friendly error.

func (Error) WithCause

func (e Error) WithCause(err error) Error

WithCause sets the internal non-user-safe cause of the error.

func (Error) WithCauseString

func (e Error) WithCauseString(err string) Error

WithCauseString sets the internal non-user-safe cause of the error.

func (Error) WithFriendly

func (e Error) WithFriendly(err error) Error

WithFriendly sets the user-safe cause of the error.

func (Error) WithFriendlyString

func (e Error) WithFriendlyString(err string) Error

WithFriendlyString sets the user-safe cause of the error.

Jump to

Keyboard shortcuts

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