Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Fix utf-8 encoding issues with trimmed error messages #569

Merged
merged 9 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions pkg/repositories/transformers/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package transformers
import (
"context"
"fmt"
"strings"
"time"

"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
Expand All @@ -16,6 +13,8 @@ import (
"github.com/flyteorg/flytestdlib/storage"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"strings"
"time"

"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -344,9 +343,8 @@ func FromExecutionModel(ctx context.Context, executionModel models.Execution, op
}
if closure.GetError() != nil && opts != nil && opts.TrimErrorMessage && len(closure.GetError().Message) > 0 {
trimmedErrOutputResult := closure.GetError()
if len(trimmedErrOutputResult.Message) > trimmedErrMessageLen {
trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen]
}
trimmedErrMessage := TrimErrorMessage(trimmedErrOutputResult.GetMessage())
trimmedErrOutputResult.Message = trimmedErrMessage
closure.OutputResult = &admin.ExecutionClosure_Error{
Error: trimmedErrOutputResult,
}
Expand Down Expand Up @@ -409,3 +407,11 @@ func FromExecutionModels(ctx context.Context, executionModels []models.Execution
}
return executions, nil
}

// TrimErrorMessage return the smallest possible trimmed error message >= trimmedErrMessageLen bytes in length that still forms a valid utf-8 string
func TrimErrorMessage(errMsg string) string {
if len(errMsg) <= trimmedErrMessageLen {
return errMsg
}
return strings.ToValidUTF8(errMsg[:trimmedErrMessageLen], "")
}
32 changes: 32 additions & 0 deletions pkg/repositories/transformers/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"
"time"
"unicode/utf8"

"github.com/flyteorg/flyteadmin/pkg/common"

Expand Down Expand Up @@ -602,6 +603,30 @@ func TestFromExecutionModel_Error(t *testing.T) {
assert.True(t, proto.Equal(expectedExecErr, execution.Closure.GetError()))
}

func TestFromExecutionModel_ValidUTF8TrimmedErrorMsg(t *testing.T) {
errMsg := "[1/1] currentAttempt done. Last Error: USER:: │\n│ ❱ 760 │ │ │ │ return __callback(*args, **kwargs) │\n││"

executionClosureBytes, _ := proto.Marshal(&admin.ExecutionClosure{
Phase: core.WorkflowExecution_FAILED,
OutputResult: &admin.ExecutionClosure_Error{Error: &core.ExecutionError{Message: errMsg}},
})
executionModel := models.Execution{
ExecutionKey: models.ExecutionKey{
Project: "project",
Domain: "domain",
Name: "name",
},
Phase: core.WorkflowExecution_FAILED.String(),
Closure: executionClosureBytes,
}
execution, err := FromExecutionModel(context.TODO(), executionModel, &ExecutionTransformerOptions{
TrimErrorMessage: true,
})
assert.NoError(t, err)
errMsgAreValidUTF8 := utf8.Valid([]byte(execution.GetClosure().GetError().GetMessage()))
assert.True(t, errMsgAreValidUTF8)
}

func TestFromExecutionModel_OverwriteNamespace(t *testing.T) {
abortCause := "abort cause"
executionClosureBytes, _ := proto.Marshal(&admin.ExecutionClosure{
Expand Down Expand Up @@ -875,3 +900,10 @@ func TestUpdateExecutionModelStateChangeDetails(t *testing.T) {
assert.False(t, strings.Contains(err.Error(), "Failed to unmarshal execution closure"))
})
}

func TestTrimErrorMessage(t *testing.T) {
errMsg := "[1/1] currentAttempt done. Last Error: USER:: │\n│ ❱ 760 │ │ │ │ return __callback(*args, **kwargs) │\n││"
trimmedErrMessage := TrimErrorMessage(errMsg)
errMsgAreValidUTF8 := utf8.Valid([]byte(trimmedErrMessage))
assert.True(t, errMsgAreValidUTF8)
}
6 changes: 3 additions & 3 deletions pkg/repositories/transformers/node_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@ func FromNodeExecutionModel(nodeExecutionModel models.NodeExecution, opts *Execu
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal closure")
}

if closure.GetError() != nil && opts != nil && opts.TrimErrorMessage && len(closure.GetError().Message) > 0 {
trimmedErrOutputResult := closure.GetError()
if len(trimmedErrOutputResult.Message) > trimmedErrMessageLen {
trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen]
}
trimmedErrMessage := TrimErrorMessage(trimmedErrOutputResult.GetMessage())
trimmedErrOutputResult.Message = trimmedErrMessage
closure.OutputResult = &admin.NodeExecutionClosure_Error{
Error: trimmedErrOutputResult,
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/repositories/transformers/task_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,8 @@ func FromTaskExecutionModel(taskExecutionModel models.TaskExecution, opts *Execu
}
if closure.GetError() != nil && opts != nil && opts.TrimErrorMessage && len(closure.GetError().Message) > 0 {
trimmedErrOutputResult := closure.GetError()
if len(trimmedErrOutputResult.Message) > trimmedErrMessageLen {
trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen]
}
trimmedErrMessage := TrimErrorMessage(trimmedErrOutputResult.GetMessage())
trimmedErrOutputResult.Message = trimmedErrMessage
closure.OutputResult = &admin.TaskExecutionClosure_Error{
Error: trimmedErrOutputResult,
}
Expand Down