Giter Club home page Giter Club logo

go-friendly's Introduction

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.

Documentation

License: MIT

Example usage

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 :(
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.