Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/netologist/yeh
Yet another Error Handler for Golang
https://github.com/netologist/yeh
error-handling go-library golang
Last synced: 4 days ago
JSON representation
Yet another Error Handler for Golang
- Host: GitHub
- URL: https://github.com/netologist/yeh
- Owner: netologist
- License: mit
- Created: 2022-05-21T10:26:22.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-05-28T06:19:32.000Z (over 2 years ago)
- Last Synced: 2024-06-21T18:15:55.533Z (5 months ago)
- Topics: error-handling, go-library, golang
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# yeh
**Y**et another **E**rror **H**andler for Golang.*This Project An **Experimental** Idea For Error Handler Implementation With Go Generics*
## Examples
### Must
Also Must has support *Must0, Must2, Must3, Must4* and *Must5* methods. For multiple return types.
```go
got := func() (err error) {
defer yeh.Recover(&err)value := yeh.Must(os.Open("file.not.exists.txt"))
fmt.Printf("Output: %d\n", value)
return nil
}()if got == nil {
t.Errorf("Failed")
}if got != fs.ErrExist {
t.Errorf("Failed")
}```
### MustWith
Also Must has support *MustWith0, MustWith2, MustWith3, MustWith4* and *MustWith5* methods. For multiple return types.
#### Replace
```gogot := func() (err error) {
defer yeh.Recover(&err)outputValue := yeh.MustWith(os.Open("file.not.exists.txt")).Replace(ErrCustomExists)
fmt.Printf("Output: %d\n", outputValue)
return nil
}()if got == nil {
t.Errorf("Failed, unexpected error")
}if got == fs.ErrExist {
t.Errorf("Failed, unexpected error")
}if got != ErrCustomExists {
t.Errorf("Failed, unexpected error")
}```
#### Wrap
```gogot := func() (err error) {
defer yeh.Recover(&err)outputValue := yeh.MustWith(os.Open("file.not.exists.txt")).Wrap(ErrCustomExists)
fmt.Printf("Output: %d\n", outputValue)
return nil
}()if got == nil {
t.Errorf("Failed, unexpected error")
}if !strings.HasPrefix(got.Error(), fs.ErrExist.Error()) {
t.Errorf("Failed, unexpected error")
}if !errors.Is(got, ErrCustomExists) {
t.Errorf("Failed, unexpected error")
}```
#### Callback
```gogot := func() (err error) {
defer yeh.Recover(&err)outputValue := yeh.MustWith(os.Open("file.not.exists.txt")).Callback(func(err error) error {
if errors.Is(err, fs.) {
return ErrCustomExists
}
return err
})fmt.Printf("Output: %d\n", outputValue)
return nil
}()if got == nil {
t.Errorf("Failed, unexpected error")
}if got == fs.ErrExist {
t.Errorf("Failed, unexpected error")
}if got != ErrCustomExists {
t.Errorf("Failed, unexpected error")
}```