Documentation ¶
Overview ¶
Package try is a package for try.ToX functions that implement the error checking. try.ToX functions check 'if err != nil' and if it throws the err to the error handlers, which are implemented by the err2 package. More information about err2 and try packager roles can be seen in the FileCopy example:
... r := try.To1(os.Open(src)) defer r.Close() w := try.To1(os.Create(dst)) defer err2.Handle(&err, func() { os.Remove(dst) }) defer w.Close() try.To1(io.Copy(w, r)) return nil ...
All of the try package functions are as fast as the simple 'if err != nil {' statement, thanks to the compiler inlining and optimization.
Note that try.ToX function names end to a number (x) because:
"No variadic type parameters. There is no support for variadic type parameters, which would permit writing a single generic function that takes different numbers of both type parameters and regular parameters." - Go Generics
The leading number at the end of the To2 tells that To2 takes two different non-error arguments, and the third one must be an error value.
Looking at the FileCopy example again, you see that all the functions are directed to try.To1 are returning (type1, error) tuples. All of these tuples are the correct input to try.To1. However, if you have a function that returns (type1, type2, error), you must use try.To2 function to check the error. Currently the try.To3 takes (3 + 1) return values which is the greatest amount. If more is needed, let us know.
Example (CopyFile) ¶
package main import ( "fmt" "io" "os" "github.com/lainio/err2" "github.com/lainio/err2/try" ) func main() { copyFile := func(src, dst string) (err error) { defer err2.Returnf(&err, "copy %s %s", src, dst) // These try package helpers are as fast as Check() calls which is as // fast as `if err != nil {}` r := try.To1(os.Open(src)) defer r.Close() w := try.To1(os.Create(dst)) defer err2.Handle(&err, func() { os.Remove(dst) }) defer w.Close() try.To1(io.Copy(w, r)) return nil } err := copyFile("/notfound/path/file.go", "/notfound/path/file.bak") if err != nil { fmt.Println(err) } }
Output: copy /notfound/path/file.go /notfound/path/file.bak: open /notfound/path/file.go: no such file or directory
Index ¶
- func Is(err, filter error) bool
- func IsEOF(err error) bool
- func IsEOF1[T any](v T, err error) (bool, T)
- func IsEOF2[T, U any](v1 T, v2 U, err error) (bool, T, U)
- func To(err error)
- func To1[T any](v T, err error) T
- func To2[T, U any](v1 T, v2 U, err error) (T, U)
- func To3[T, U, V any](v1 T, v2 U, v3 V, err error) (T, U, V)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Is ¶
Is-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the filter before throwing an error. The false return value tells that there are no errors and the true value that the error is the filter.
Example (ErrorHappens) ¶
package main import ( "fmt" "io" "github.com/lainio/err2" "github.com/lainio/err2/try" ) var errForTesting = fmt.Errorf("error for %s", "testing") func main() { copyStream := func(src string) (s string, err error) { defer err2.Returnf(&err, "copy stream %s", src) err = errForTesting try.Is(err, io.EOF) return src, nil } str, err := copyStream("testing string") if err != nil { fmt.Println(err) } fmt.Println(str) }
Output: copy stream testing string: error for testing
Example (ErrorHappensNot) ¶
package main import ( "fmt" "github.com/lainio/err2" "github.com/lainio/err2/try" ) var errForTesting = fmt.Errorf("error for %s", "testing") func main() { copyStream := func(src string) (s string, err error) { defer err2.Returnf(&err, "copy stream %s", src) err = fmt.Errorf("something: %w", errForTesting) if try.Is(err, errForTesting) { return "wrapping works", nil } return src, nil } str, err := copyStream("testing string") if err != nil { fmt.Println(err) } fmt.Println(str) }
Output: wrapping works
func IsEOF ¶
IsEOF-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the 'io.EOF' before throwing an error. The false return value tells that there are no errors and the true value that the error is the 'io.EOF'.
func IsEOF1 ¶
IsEOF1-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the 'io.EOF' before throwing an error. The false return value tells that there are no errors and the true value that the error is the 'io.EOF'.
Example ¶
package main import ( "bytes" "fmt" "github.com/lainio/err2" "github.com/lainio/err2/try" ) func main() { copyStream := func(src string) (s string, err error) { defer err2.Returnf(&err, "copy stream %s", src) in := bytes.NewBufferString(src) tmp := make([]byte, 4) var out bytes.Buffer for eof, n := try.IsEOF1(in.Read(tmp)); !eof; eof, n = try.IsEOF1(in.Read(tmp)) { out.Write(tmp[:n]) } return out.String(), nil } str, err := copyStream("testing string") if err != nil { fmt.Println(err) } fmt.Println(str) }
Output: testing string
func IsEOF2 ¶
IsEOF2-function performs a filtered error check for the given argument. It's the same as To-function, but it checks if the error matches the 'io.EOF' before throwing an error. The false return value tells that there are no errors and the true value that the error is the 'io.EOF'.
func To ¶
func To(err error)
To is a helper function to call functions which returns (error) and check the error value. If an error occurs, it panics the error where err2 handlers can catch it if needed.
func To1 ¶
To1 is a helper function to call functions which returns (any, error) and check the error value. If an error occurs, it panics the error where err2 handlers can catch it if needed.
Types ¶
This section is empty.