Skip to content

Commit

Permalink
remove try.TryX and try.CheckX functioncs
Browse files Browse the repository at this point in the history
try.Try and try.Check can be used.
This just requires an intermediate error variable.
In many real world cases this is a good way to make code readable.
This aligns the library to community feedback and this proposal:
golang/go#56165
  • Loading branch information
gregwebs committed Oct 12, 2022
1 parent 917eecc commit 7dadbec
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 217 deletions.
17 changes: 9 additions & 8 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ them. The CopyFile example shows how it works:
// Try to open the file. If error occurs now, err will be annotated and
// returned properly thanks to above err3.Returnf.
r := try.Check1(os.Open(src))
r, err := os.Open(src)
try.Check(err)
defer r.Close()
// Try to create a file. If error occurs now, err will be annotated and
// returned properly.
w := try.Try1(os.Create(dst))(try.Cleanup(func() {
w, err := os.Create(dst)
try.Try(err, try.Cleanup(func() {
os.Remove(dst)
})
defer w.Close()
// Try to copy the file. If error occurs now, all previous error handlers
// will be called in the reversed order. And final return error is
// properly annotated in all the cases.
_ = try.Check1(io.Copy(w, r))
_, err = io.Copy(w, r)
try.Check(err)
// All OK, just return nil.
return nil
Expand All @@ -50,14 +53,12 @@ instead of
we can write
b := try.Check1(ioutil.ReadAll(r))
Note that try.ToX functions are as fast as if err != nil statements. Please see
the try package documentation for more information about the error checks.
b, err := ioutil.ReadAll(r)
try.Check(err)
# Stack Tracing
By default, TryX and CheckX will wrap the error so that it has a stack trace
By default, try.Try and try.Check will wrap the error so that it has a stack trace
This can be disabled by setting the `AddStackTrace = false`
# Error handling
Expand Down
104 changes: 69 additions & 35 deletions err3/err3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func recursion(a int) (r int, err error) {
if a == 0 {
return 0, nil
}
s := try.Check1(noThrow())
s, err := noThrow()
try.Check(err)
_ = s
r = try.Check1(recursion(a - 1))
r, err = recursion(a - 1)
try.Check(err)
r += a
return r, nil
}
Expand All @@ -46,7 +47,8 @@ func cleanRecursion(a int) int {
if a == 0 {
return 0
}
s := try.Check1(noThrow())
s, err := noThrow()
try.Check(err)
_ = s
return a + cleanRecursion(a-1)
}
Expand All @@ -70,20 +72,26 @@ func recursionWithErrorCheck(a int) (int, error) {
func errHandlefOnly() (err error) {
defer err3.Handlef(&err, "handle top")
defer err3.Handlef(&err, "handle error")
_ = try.Check1(throw())
_, err = throw()
try.Check(err)
defer err3.Handlef(&err, "handle error")
_ = try.Check1(throw())
_, err = throw()
try.Check(err)
defer err3.Handlef(&err, "handle error")
_ = try.Check1(throw())
_, err = throw()
try.Check(err)
return err
}

func errTry1_Fmt() (err error) {
defer err3.Handlef(&err, "handle top")
// _ = try.Try1(throw())(func(err error) error { return fmt.Errorf("handle error: %v", err) })
_ = try.Try1(throw())(try.Fmt("handle error"))
_ = try.Try1(throw())(try.Fmt("handle error"))
_ = try.Try1(throw())(try.Fmt("handle error"))
_, err = throw()
try.Try(err, try.Fmt("handle error"))

This comment has been minimized.

Copy link
@davidrenne

davidrenne Oct 14, 2022

@gregwebs thanks for the proposal. Looking forward to go with try. Nice work!

_, err = throw()
try.Try(err, try.Fmt("handle error"))
_, err = throw()
try.Try(err, try.Fmt("handle error"))
return err
}

Expand All @@ -92,28 +100,37 @@ func empty() error { return nil }

func errTry1_id() (err error) {
defer err3.Handlef(&err, "handle top")
_ = try.Try1(throw())(errId)
_ = try.Try1(throw())(errId)
_ = try.Try1(throw())(errId)
_, err = throw()
try.Try(err, errId)
_, err = throw()
try.Try(err, errId)
_, err = throw()
try.Try(err, errId)
return err
}

func errHandle_Only() (err error) {
defer err3.Handlef(&err, "handle top")
defer err3.Handle(&err, empty)
_ = try.Check1(throw())
_, err = throw()
try.Check(err)
defer err3.Handle(&err, empty)
_ = try.Check1(throw())
_, err = throw()
try.Check(err)
defer err3.Handle(&err, empty)
_ = try.Check1(throw())
_, err = throw()
try.Check(err)
return err
}

func errTry1_inlineHandler() (err error) {
defer err3.Handlef(&err, "handle top")
_ = try.Try1(throw())(func(err error) error { return fmt.Errorf("handle error: %v", err) })
_ = try.Try1(throw())(func(err error) error { return fmt.Errorf("handle error: %v", err) })
_ = try.Try1(throw())(func(err error) error { return fmt.Errorf("handle error: %v", err) })
_, err = throw()
try.Try(err, func(err error) error { return fmt.Errorf("handle error: %v", err) })
_, err = throw()
try.Try(err, func(err error) error { return fmt.Errorf("handle error: %v", err) })
_, err = throw()
try.Try(err, func(err error) error { return fmt.Errorf("handle error: %v", err) })
return err
}

Expand All @@ -122,17 +139,22 @@ func noErr() error {
}

func TestTry_noError(t *testing.T) {
try.Check1(noThrow())
try.Check2(twoStrNoThrow())
try.Check2(intStrNoThrow())
try.Check3(boolIntStrNoThrow())
_, err := noThrow()
try.Check(err)
_, _, err = twoStrNoThrow()
try.Check(err)
_, _, err = intStrNoThrow()
try.Check(err)
_, _, _, err = boolIntStrNoThrow()
try.Check(err)
}

func TestDefault_Error(t *testing.T) {
var err error
defer err3.Handle(&err, nil)

try.Check1(throw())
_, err = throw()
try.Check(err)

t.Fail() // If everything works we are newer here
}
Expand All @@ -141,7 +163,8 @@ func TestTry_Error(t *testing.T) {
var err error
defer err3.Handle(&err, nil)

try.Check1(throw())
_, err = throw()
try.Check(err)

t.Fail() // If everything works we are newer here
}
Expand Down Expand Up @@ -363,7 +386,8 @@ func TestCatch_Error(t *testing.T) {
//fmt.Printf("error and defer handling:%s\n", err)
})

try.Check1(throw())
_, err := throw()
try.Check(err)

t.Fail() // If everything works we are newer here
}
Expand All @@ -374,15 +398,18 @@ func Example_copyFile() {

// These try.To() checkers are as fast as `if err != nil {}`

r := try.Check1(os.Open(src))
r, err := os.Open(src)
try.Check(err)
defer r.Close()

rmFile := try.Cleanup(func() {
os.Remove(dst)
})
w := try.Try1(os.Create(dst))(rmFile)
w, err := os.Create(dst)
try.Try(err, rmFile)
defer w.Close()
_ = try.Try1(io.Copy(w, r))(rmFile, try.Fmt("copy failure"))
_, err = io.Copy(w, r)
try.Try(err, rmFile, try.Fmt("copy failure"))
return nil
}

Expand All @@ -396,14 +423,16 @@ func Example_copyFile() {
func ExampleHandle() {
var err error
defer err3.Handle(&err, nil)
try.Check1(noThrow())
_, err = noThrow()
try.Check(err)
// Output:
}

func ExampleHandlef() {
annotated := func() (err error) {
defer err3.Handlef(&err, "annotated")
try.Check1(throw())
_, err = throw()
try.Check(err)
return err
}
err := annotated()
Expand All @@ -414,7 +443,8 @@ func ExampleHandlef() {
func ExampleHandlef_format_args() {
annotated := func() (err error) {
defer err3.Handlef(&err, "annotated: %s", "err3")
try.Check1(throw())
_, err = throw()
try.Check(err)
return err
}
err := annotated()
Expand Down Expand Up @@ -452,7 +482,8 @@ func ExampleHandlef_deferStack() {
annotated := func() (err error) {
defer err3.Handlef(&err, "3rd")
defer err3.Handlef(&err, "2nd")
_ = try.Try1(throw())(try.Fmt("1st"))
_, err = throw()
try.Try(err, try.Fmt("1st"))
return err
}
err := annotated()
Expand All @@ -465,7 +496,8 @@ func ExampleHandle_with_handler() {
defer err3.Handle(&err, func() error {
return fmt.Errorf("error with (%d, %d): %v", a, b, err)
})
try.Check1(throw())
_, err = throw()
try.Check(err)
return err
}
err := doSomething(1, 2)
Expand Down Expand Up @@ -508,7 +540,8 @@ func Benchmark_Err_Try1_Fmt(b *testing.B) {

func Benchmark_NoErr_Check1(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = try.Check1(noThrow()) // a slight slow-down
_, err := noThrow() // a slight slow-dow
try.Check(err)
}
}

Expand All @@ -521,7 +554,8 @@ func Benchmark_Noerr3_Check_NilErr(b *testing.B) {

func Benchmark_NoErr_Check2(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = try.Check2(twoStrNoThrow())
_, _, err := twoStrNoThrow()
try.Check(err)
}
}

Expand Down
Loading

0 comments on commit 7dadbec

Please sign in to comment.