From 326ca45e63f2c2d09be6f87fd72851b22c21e13a Mon Sep 17 00:00:00 2001 From: Kevin Su Date: Tue, 7 Mar 2023 03:29:40 +0800 Subject: [PATCH] Truncate execution error message only when size exceeds limit (#533) * Bump propeller Signed-off-by: Kevin Su * Fixed slice bounds out of range Signed-off-by: Kevin Su * Fixed slice bounds out of range Signed-off-by: Kevin Su * update go.mod Signed-off-by: Kevin Su * update go.mod Signed-off-by: Kevin Su * update Signed-off-by: Kevin Su * Add tests Signed-off-by: Kevin Su * lint Signed-off-by: Kevin Su --------- Signed-off-by: Kevin Su --- pkg/repositories/transformers/execution.go | 4 +++- .../transformers/node_execution.go | 4 +++- .../transformers/task_execution.go | 4 +++- .../transformers/task_execution_test.go | 24 +++++++++++++++++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pkg/repositories/transformers/execution.go b/pkg/repositories/transformers/execution.go index 5362e088d..e92fbc589 100644 --- a/pkg/repositories/transformers/execution.go +++ b/pkg/repositories/transformers/execution.go @@ -328,7 +328,9 @@ func FromExecutionModel(executionModel models.Execution, opts *ExecutionTransfor } if closure.GetError() != nil && opts != nil && opts.TrimErrorMessage && len(closure.GetError().Message) > 0 { trimmedErrOutputResult := closure.GetError() - trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen] + if len(trimmedErrOutputResult.Message) > trimmedErrMessageLen { + trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen] + } closure.OutputResult = &admin.ExecutionClosure_Error{ Error: trimmedErrOutputResult, } diff --git a/pkg/repositories/transformers/node_execution.go b/pkg/repositories/transformers/node_execution.go index 093e70d3d..8d95cab97 100644 --- a/pkg/repositories/transformers/node_execution.go +++ b/pkg/repositories/transformers/node_execution.go @@ -286,7 +286,9 @@ func FromNodeExecutionModel(nodeExecutionModel models.NodeExecution, opts *Execu } if closure.GetError() != nil && opts != nil && opts.TrimErrorMessage && len(closure.GetError().Message) > 0 { trimmedErrOutputResult := closure.GetError() - trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen] + if len(trimmedErrOutputResult.Message) > trimmedErrMessageLen { + trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen] + } closure.OutputResult = &admin.NodeExecutionClosure_Error{ Error: trimmedErrOutputResult, } diff --git a/pkg/repositories/transformers/task_execution.go b/pkg/repositories/transformers/task_execution.go index 92c066413..849874e80 100644 --- a/pkg/repositories/transformers/task_execution.go +++ b/pkg/repositories/transformers/task_execution.go @@ -407,7 +407,9 @@ func FromTaskExecutionModel(taskExecutionModel models.TaskExecution, opts *Execu } if closure.GetError() != nil && opts != nil && opts.TrimErrorMessage && len(closure.GetError().Message) > 0 { trimmedErrOutputResult := closure.GetError() - trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen] + if len(trimmedErrOutputResult.Message) > trimmedErrMessageLen { + trimmedErrOutputResult.Message = trimmedErrOutputResult.Message[0:trimmedErrMessageLen] + } closure.OutputResult = &admin.TaskExecutionClosure_Error{ Error: trimmedErrOutputResult, } diff --git a/pkg/repositories/transformers/task_execution_test.go b/pkg/repositories/transformers/task_execution_test.go index 19ad2d051..0ce13cc94 100644 --- a/pkg/repositories/transformers/task_execution_test.go +++ b/pkg/repositories/transformers/task_execution_test.go @@ -585,7 +585,7 @@ func TestFromTaskExecutionModel_Error(t *testing.T) { Phase: core.WorkflowExecution_FAILED, OutputResult: &admin.ExecutionClosure_Error{Error: execErr}, }) - taskExecution, err := FromTaskExecutionModel(models.TaskExecution{ + taskExecutionModel := models.TaskExecution{ TaskExecutionKey: models.TaskExecutionKey{ TaskKey: models.TaskKey{ Project: "project", @@ -606,7 +606,8 @@ func TestFromTaskExecutionModel_Error(t *testing.T) { InputURI: "input uri", Duration: duration, Closure: closureBytes, - }, &ExecutionTransformerOptions{ + } + taskExecution, err := FromTaskExecutionModel(taskExecutionModel, &ExecutionTransformerOptions{ TrimErrorMessage: true, }) @@ -614,6 +615,25 @@ func TestFromTaskExecutionModel_Error(t *testing.T) { expectedExecErr.Message = string(make([]byte, trimmedErrMessageLen)) assert.Nil(t, err) assert.True(t, proto.Equal(expectedExecErr, taskExecution.Closure.GetError())) + + extraShortErrMsg := string(make([]byte, 10)) + execErr = &core.ExecutionError{ + Code: "CODE", + Message: extraShortErrMsg, + Kind: core.ExecutionError_USER, + } + closureBytes, _ = proto.Marshal(&admin.ExecutionClosure{ + Phase: core.WorkflowExecution_FAILED, + OutputResult: &admin.ExecutionClosure_Error{Error: execErr}, + }) + taskExecutionModel.Closure = closureBytes + taskExecution, err = FromTaskExecutionModel(taskExecutionModel, &ExecutionTransformerOptions{ + TrimErrorMessage: true, + }) + expectedExecErr = execErr + expectedExecErr.Message = string(make([]byte, 10)) + assert.Nil(t, err) + assert.True(t, proto.Equal(expectedExecErr, taskExecution.Closure.GetError())) } func TestFromTaskExecutionModels(t *testing.T) {