feat: adding ErrorsAs

This commit is contained in:
Samuel Berthe
2022-10-02 21:51:02 +02:00
parent 31f3bc3a85
commit edda23923b
4 changed files with 62 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package lo
import (
"errors"
"fmt"
"reflect"
)
@@ -199,3 +200,10 @@ func TryCatchWithErrorValue(callback func() error, catch func(any)) {
catch(err)
}
}
// ErrorsAs is a shortcut for errors.As(err, &&T).
func ErrorsAs[T error](err error) (T, bool) {
var t T
ok := errors.As(err, &t)
return t, ok
}