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
- Clone this repo into your project
- Create the folder
errors
and put this code into that folder - Define your own error kinds and update tests a little bit
- Profit ++
You can use this with different build tags also:
debug
is used to get the call stack in case of error with operations (if they defined)w/o tags
is used to see errors and operations only (Op
data type), w/o stack trace
Examples:
- Using with operations:
func foo() error {
const Op = "foo"
err := someOperation()
return E(Op, err)
}
- 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