https://github.com/hanagantig/aerrors
async errors handling in GO
https://github.com/hanagantig/aerrors
async error-handling go go-errors golang
Last synced: 30 days ago
JSON representation
async errors handling in GO
- Host: GitHub
- URL: https://github.com/hanagantig/aerrors
- Owner: hanagantig
- License: mit
- Created: 2021-05-28T19:34:43.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-12-31T17:51:06.000Z (over 4 years ago)
- Last Synced: 2024-06-19T06:46:16.541Z (almost 2 years ago)
- Topics: async, error-handling, go, go-errors, golang
- Language: Go
- Homepage:
- Size: 32.2 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](http://godoc.org/github.com/hanagantig/aerrors)
[](https://travis-ci.com/hanagantig/aerrors)
# aerrors
Package aerrors adds **async errors** handling support in GO
This is effective when you want to recover panics in all your goroutines, build an error from it and handle all such errors in one place (logging them or monitor).
There is additionally the `Wrap()` method, which helps you to build manually 'stacktrace' with errors chain.
## Getting started
To download the package, run:
```bash
go get github.com/hanagantig/aerrors
```
Import it in your program as:
```go
import "github.com/hanagantig/aerrors"
```
It requires Go 1.13 or later.
Refer to the documentation here:
http://godoc.org/github.com/hanagantig/aerrors
### Basic usage
```go
package main
import (
"github.com/hanagantig/aerrors"
)
func crashFunc() {
panic("crashFunc panic")
}
func main() {
aerror := aerrors.New()
aerror.StartHandle()
server := runHTTP()
// runs crashFunc in panic-safe goroutine and adds error to handle
aerror.Go(crashFunc)
server.Stop()
aerror.Stop()
}
```
### With custom handler function
You also can implement your custom handler.
Merely use available option aerrors.WithHandler().
```go
package main
import (
"github.com/hanagantig/aerrors"
)
type CustomErrorHandler struct {}
func (eh *CustomErrorHandler) HandleError(err error) {
// do what you want with your error here
}
func main() {
h := CustomErrorHandler{}
aerror := aerrors.New(aerrors.WithHandler(&h))
_ = aerror.StartHandle()
aerror.Close() // for graceful shutdown
}
```
### Global usage
As well as create particular error struct you can initialize it globally and call
from where you want.
```go
package main
import (
"errors"
"log"
"github.com/hanagantig/aerrors"
)
func foo(id int) (err error) {
aerrors.Get().Go(crashFunc)
aerrors.Get().Add(errors.New("some error to handle"))
}
func crashFunc() {
panic("crashFunc panic")
}
func main() {
err := aerrors.Init()
if err != nil {
log.Fatal(err)
}
aerrors.Get().StartHandle()
server := runHTTP()
server.Stop()
aerrors.Get().Stop()
}
```
### Some more features
In addition, you are capable to build your own stack trace with needed functions contains all desired information.
```go
package main
import (
"fmt"
"errors"
"github.com/hanagantig/aerrors"
)
func foo(id int) (err error){
defer aerrors.Wrap(&err, "foo(%v)", id)
err = errors.New("errors in foo")
return
}
func bar(id int, tag string) (err error) {
defer aerrors.Wrap(&err, "bar(%v, %v)", id, tag)
err = foo(id)
return
}
func main() {
err := bar(1, "aerrors_wrap")
fmt.Printf("%v", err)
}
// output
// bar(1, aerrors_wrap): foo(1): errors in foo
```