Giter VIP home page Giter VIP logo

ego's Introduction

ego

Package for error wrapping, unwrapping, transforming to gql or http status code. (Inspired by pkg/errors)

go get github.com/maasasia/ego

Adding context to an error

_, err := ioutil.ReadAll(r)
if err != nil {
        return ego.Wrap(err, "read failed")
}
if err != nil {
        extra := 123
        return ego.Wrapf(err, "read failed with some code %d", extra)
}

Retrieving the cause of an error

func ErrorFunction() error {
	return ego.NewBadInputError("not found")
}

func Call1() error {
	err := ErrorFunction()
	if err != nil {
		return ego.Wrap(err, "FindById fail")
	}
	return nil
}

func Call2() error {
	err := Call1()
	if err != nil {
		return ego.Wrap(err, "Call1 fail")
	}
	return nil
}

func main() {
	err := Call2()
	if err != nil {
		rootErr := ego.Cause(err)
		switch rootErr.(type) {
		case ego.InternalServerError:
			fmt.Println("internal")
		case ego.ForbiddenError:
			fmt.Println("forbidden")
		case ego.BadInputError:
			fmt.Println("bad")
		default:
			fmt.Println("default")
		}
	}
}

// -> print "bad"

ego's People

Contributors

jeyong-dev avatar

Watchers

 avatar

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.