https://github.com/chyroc/anyhow
error handle
https://github.com/chyroc/anyhow
Last synced: 10 months ago
JSON representation
error handle
- Host: GitHub
- URL: https://github.com/chyroc/anyhow
- Owner: chyroc
- License: apache-2.0
- Created: 2022-03-20T05:58:37.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-19T04:21:16.000Z (over 1 year ago)
- Last Synced: 2025-01-29T08:22:54.193Z (12 months ago)
- Language: Go
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# anyhow
go impl for https://docs.rs/anyhow/latest/anyhow/
[](https://codecov.io/gh/chyroc/anyhow)
[](https://goreportcard.com/report/github.com/chyroc/anyhow)
[](https://github.com/chyroc/anyhow/actions)
[](https://opensource.org/licenses/Apache-2.0)
[](https://pkg.go.dev/github.com/chyroc/anyhow)
[](https://badge.fury.io/go/github.com%2Fchyroc%2Fanyhow)
## Installation
```shell
go get github.com/chyroc/anyhow
```
## Features
- result struct
- `Result1[T]`
- `Result2[T1, T2]`
- `Result3[T1, T2, T3]`
- `Result4[T1, T2, T3, T4]`
- `Result5[T1, T2, T3, T4, T5]`
- `Result6[T1, T2, T3, T4, T5, T6]`
- functions:
- `And(self R[T], res R[U]) R[U]`: Returns res if the result is Ok, otherwise returns the Err value of self
- `AndThen(self R[T], op (T) -> R[U]) R[U]`: Calls op if the result is Ok, otherwise returns the Err value of self
- `Map(self R[T], op (T) -> U) R[U]`: Maps a Result to Result by applying a function to a contained Ok value, leaving an Err value untouched
- `MapOr(self R[T], default U, op func(T) U) U`: Returns the provided default (if Err), or applies a function to the contained value (if Ok)
- methods:
- `(R[T]) IntoErr() error`: Returns the contained Err value, but never panics
- `(R[T]) IntoOk() T1`: Returns the contained Ok value, but never panics
- `(R[T]) Expect(string) T`: Returns the contained Ok value, otherwise panics with a panic message including the passed message, and the content of the Err
- `(R[T]) ExpectErr(string) error`: Returns the contained Err value, otherwise panics with a panic message including the passed message, and the content of the Ok
- `(R[T]) Inspect(f (T) -> void) R[T]`: Calls a function with a reference to the contained value if Ok
- `(R[T]) InspectErr(f (error) -> void) R[T]`: Calls a function with a reference to the contained value if Err
- `(R[T]) MapErr(op func(error) error) R[T]`: Maps a R[T] to R[T] by applying a function to a contained Err value, leaving an Ok value untouched
- `(R[T]) IsErr() bool`: Returns true if the result is Err
- `(R[T]) IsOk() bool`: Returns true if the result is Ok
- `(R[T]) Or(res R[T]) R[T]`: Returns res if the result is Err, otherwise returns the Ok value of self
- `(R[T]) OrElse(op (error) -> R[T]) R[T]`: Calls op if the result is Err, otherwise returns the Ok value of self
- `(R[T]) Unwrap() T`: Returns the contained Ok value, otherwise panics with Err
- `(R[T]) UnwrapOr(default T) T`: Returns the contained Ok value or a provided default
- `(R[T]) UnwrapOrDefault() T`: Returns the contained Ok value or a default value
- `(R[T]) UnwrapOrElse(op (error) -> T) T`: Returns the contained Ok value or computes it from a closure
## Usage
```go
package main
import (
"log"
"os/user"
. "github.com/chyroc/anyhow"
)
func getHomePath() Result1[string] {
user, err := user.Current()
if err != nil {
return Err1[string](err)
}
return Ok1(user.HomeDir)
}
func Example_AndThen_UnwrapOr() {
// get home path, and then get log path, or use /tmp/log
_ = AndThen11(getHomePath(), func(t1 string) Result1[string] {
return Ok1(t1 + "/.log")
}).UnwrapOr("/tmp/log")
}
func Example_Inspect_InspectErr() {
// get home path, log it, or log error
_ = getHomePath().Inspect(func(t1 string) {
log.Println("home:", t1)
}).InspectErr(func(err error) {
log.Println("error:", err)
}).UnwrapOr("/tmp/log")
}
func Example_Expect_ExpectErr() {
// get home path, or panic with `must get home` msg
_ = getHomePath().Expect("must get home")
// get home path with err, or panic with `must get err` msg
_ = getHomePath().ExpectErr("must get err")
}
```