GithubHelp home page GithubHelp logo

go-exception's Introduction

go-exception

Build Status

This is a simple library for wrapping errors.Error with a stack trace.

##Sample Output

If we run ex.Error() on an Exception we will get a more detailed output than a normal errorString

Exception: this is a sample error
       At: foo_controller.go:20 testExceptions()
           http.go:198 func1()
           http.go:213 func1()
           http.go:117 func1()
           router.go:299 ServeHTTP()
           server.go:1862 ServeHTTP()
           server.go:1361 serve()
           asm_amd64.s:1696 goexit()

##Usage

If we want to create a new exception we can use New

	return exception.New("this is a test exception")

New will create a stack trace at the given line. It ignores stack frames within the exception package itself. There is also a convenience method Newf that will mimic Printf like functions.

If we want to wrap an existing golang error all we have to do is call Wrap

	file, fileErr := os.ReadFile("my_file.txt")
	if fileErr != nil {
		return exception.Wrap(fileErr)
	}

A couple properties of wrap:

  • It will return nil if the input error is nil
  • It will not modify an error that is actually an exception, it will simply return (propagate) it.
  • It will create a stack trace for the error if it is not nil, and assign the message from the existing error.

If we want to merge a couple exceptions together, i.e. we can use WrapMany

if q.Rows != nil {
	if closeErr := q.Rows.Close(); closeErr != nil {
		return exception.WrapMany(q.Error, closeErr)
	}
}

WrapMany will "nest" the exceptions, and this will be reflected in the output.

go-exception's People

Contributors

apremalal2 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.