lostluck commented on a change in pull request #15206:
URL: https://github.com/apache/beam/pull/15206#discussion_r675788964



##########
File path: sdks/go/pkg/beam/core/runtime/exec/util.go
##########
@@ -33,13 +34,29 @@ func (g *GenID) New() UnitID {
        return UnitID(g.last)
 }
 
+type doFnError struct {
+       doFn string
+       err  error
+       uid  UnitID
+       pid  string
+}
+
+func (e *doFnError) Error() string {
+       return fmt.Sprintf("DoFn[<%d>;<%s>]<%s> returned error:<%s>", e.uid, 
e.pid, e.doFn, e.err)
+}
+
 // callNoPanic calls the given function and catches any panic.
 func callNoPanic(ctx context.Context, fn func(context.Context) error) (err 
error) {
        defer func() {
                if r := recover(); r != nil {
-                       // Top level error is the panic itself, but also 
include the stack trace as the original error.
-                       // Higher levels can then add appropriate context 
without getting pushed down by the stack trace.
-                       err = errors.SetTopLevelMsgf(errors.Errorf("panic: %v 
%s", r, debug.Stack()), "panic: %v", r)
+                       // Check if the panic value is from a failed DoFn, and 
return it without a pancic trace.

Review comment:
        Typo
   ```suggestion
                        // Check if the panic value is from a failed DoFn, and 
return it without a panic trace.
   ```

##########
File path: sdks/go/pkg/beam/core/runtime/exec/util_test.go
##########
@@ -0,0 +1,69 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package exec
+
+import (
+       "context"
+       "strings"
+       "testing"
+
+       "github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
+       "github.com/apache/beam/sdks/go/pkg/beam/util/errorx"
+)
+
+// testSimpleError tests for a simple case that
+// doesn't panic
+func TestCallNoPanic_simple(t *testing.T) {
+       ctx := context.Background()
+       want := errors.New("Simple error.")
+       got := callNoPanic(ctx, func(c context.Context) error { return 
errors.New("Simple error.") })
+
+       if got.Error() != want.Error() {
+               t.Errorf("callNoPanic(<func that returns error>) = %v, want 
%v", got, want)
+       }
+}
+
+// testPanicError tests for the case in which a normal
+// error is passed to panic, resulting in panic trace.
+func TestCallNoPanic_panic(t *testing.T) {
+       ctx := context.Background()
+       got := callNoPanic(ctx, func(c context.Context) error { panic("Panic 
error") })
+       if !strings.Contains(got.Error(), "panic:") {
+               t.Errorf("callNoPanic(<func that panics with a string>) didn't 
panic, got = %v", got)
+       }
+}
+
+// testWrapPanicError tests for the case in which error
+// is passed to panic from DoFn, resulting in
+// formatted error message for DoFn.
+func TestCallNoPanic_wrappedPanic(t *testing.T) {
+       ctx := context.Background()
+       parDoError := &doFnError{
+               doFn: "sumFn",
+               err:  errors.New("SumFn error"),
+               uid:  1,
+               pid:  "Plan ID",
+       }
+       want := "DoFn[<1>;<Plan ID>]<sumFn> returned error:<SumFn error>"
+       var err errorx.GuardedError
+       err.TrySetError(parDoError)
+
+       got := callNoPanic(ctx, func(c context.Context) error { 
panic(parDoError) })
+
+       if strings.Contains(got.Error(), "panic:") {
+               t.Errorf("callNoPanic(<func that panics with a wrapped know 
error>) did not filter panic, want %v, got %v", want, got)

Review comment:
       Typo
   ```suggestion
                t.Errorf("callNoPanic(<func that panics with a wrapped known 
error>) did not filter panic, want %v, got %v", want, got)
   ```

##########
File path: sdks/go/pkg/beam/core/runtime/exec/util.go
##########
@@ -33,13 +34,29 @@ func (g *GenID) New() UnitID {
        return UnitID(g.last)
 }
 
+type doFnError struct {
+       doFn string
+       err  error
+       uid  UnitID
+       pid  string
+}
+
+func (e *doFnError) Error() string {
+       return fmt.Sprintf("DoFn[<%d>;<%s>]<%s> returned error:<%s>", e.uid, 
e.pid, e.doFn, e.err)

Review comment:
       Consider something like this instead `fmt.Sprintf("DoFn[UID:%v, PID:%v, 
Name: %v] failed:\n%v", e.uid, e.pid, e.doFn, e.err)`
   
   * It cuts out extra words and structure (all the <> make it harder to read 
the error)
   * It doesn't assume that an error was returned (it might have been caught & 
converted earlier)
   * It puts the error on it's own line which helps readability, since DoFn 
names will have the full package path.
   * It also allows the more specific custom printing of these types (like 
error) rather than making type assumptions (eg. %d and %s which have input 
requirements...).
     * Specifically Go's fmt package has an additional verb `%v` which can 
print most variables.

##########
File path: sdks/go/pkg/beam/core/runtime/exec/pardo.go
##########
@@ -332,8 +332,14 @@ func (n *ParDo) postInvoke() error {
 
 func (n *ParDo) fail(err error) error {
        n.status = Broken
-       n.err.TrySetError(err)
-       return err
+       parDoError := &doFnError{

Review comment:
       You'll want to add something like the following to the tops of these 
methods (or add a wrapDoFnError function for wrapping the error, consistently)
   
   ```
   if err2, ok := err.(*doFnError); ok {
        return err2
   }
   ```
   This filters out the known error type and prevents it from being wrapped 
over and over again as it's returned up the stack of DoFns.

##########
File path: sdks/go/pkg/beam/core/runtime/exec/util_test.go
##########
@@ -0,0 +1,69 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package exec
+
+import (
+       "context"
+       "strings"
+       "testing"
+
+       "github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
+       "github.com/apache/beam/sdks/go/pkg/beam/util/errorx"
+)
+
+// testSimpleError tests for a simple case that
+// doesn't panic

Review comment:
       Go comments don't have line length restrictions, but tend to aim for 
around ~80 characters. That's not a hard limit. 
   
   In this case it should be a single line.
   
   See also https://golang.org/doc/effective_go#commentary 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to