rustatian / errors

Project to work with errors in nice way. Motivated by https://commandcenter.blogspot.com/2017/12/error-handling-in-upspin.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Errors

This is a project to work with errors in a nice way. Motivated by https://commandcenter.blogspot.com/2017/12/error-handling-in-upspin.html Errors fully compatible with the std errors

Sample of a usage

  1. Clone this repo into your project
  2. Create the folder errors and put this code into that folder
  3. Define your own error kinds and update tests a little bit
  4. Profit ++

You can use this with different build tags also:

  1. debug is used to get the call stack in case of error with operations (if they defined)
  2. w/o tags is used to see errors and operations only (Op data type), w/o stack trace

Examples:

  1. Using with operations:
func foo() error {
    const Op = "foo"
    err := someOperation()
    return E(Op, err)
}
  1. Using with kinds of errors:
func foo() error {
	err := someFunc() // <- select from database
}

func someFunc() (int, error) {
	const Op = "GET"
	// GET from database...
	err := getFromDatabase // <- just to simplify
	if err != nil {
		return E(Op, err, DataBaseFail) // <- DataBaseFail is error kind defined in errors.go
	}
	return 
}

// As the result you will see error with error code and operations. 
// So, you will find the place where this error occurred easily and fast 

Sample of output with debug mode on:

 /debug_test.go:53: github.com/ValeryPiashchynski/errors.func1:
	/debug_test.go:58: ...T.func2:
	/debug_test.go:62: ...func3:
	/debug_test.go:67: ...func4: func2 invoke func3: Network error:
	func4 operation: error in action

Sample of output w/o debug mode:

func2 invoke func3: Network error:
	func4 operation: error in action

Valery Piashchynski, SpiralScout.com

About

Project to work with errors in nice way. Motivated by https://commandcenter.blogspot.com/2017/12/error-handling-in-upspin.html

License:MIT License


Languages

Language:Go 100.0%