Skip to content

Commit

Permalink
Merge pull request #109 from Clever/INFRANG-5529-truncate-errors
Browse files Browse the repository at this point in the history
Bugfix: Limit length of error fields per AWS limits.
  • Loading branch information
taylor-sutton authored Aug 21, 2023
2 parents f5d3f8b + 7e62ec6 commit a47f8ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions VERSION
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
v0.7.1
Add team tag to activities
v0.7.2
Truncate error messages to fit within AWS limits.
23 changes: 21 additions & 2 deletions cmd/sfncli/error_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sfn"
Expand All @@ -26,11 +27,15 @@ type TaskFailureError interface {
func (t TaskRunner) sendTaskFailure(err TaskFailureError) error {
t.logger.ErrorD("send-task-failure", logger.M{"name": err.ErrorName(), "cause": err.ErrorCause()})

// Limits from https://docs.aws.amazon.com/step-functions/latest/apireference/API_SendTaskFailure.html
const maxErrorLength = 256
const maxCauseLength = 32768

// don't use SendTaskFailureWithContext, since the failure itself could be from the parent
// context being cancelled, but we still want to report to AWS the failure of the task.
_, sendErr := t.sfnapi.SendTaskFailure(&sfn.SendTaskFailureInput{
Error: aws.String(err.ErrorName()),
Cause: aws.String(err.ErrorCause()),
Error: aws.String(truncateString(err.ErrorName(), maxErrorLength, "[truncated]")),
Cause: aws.String(truncateString(err.ErrorCause(), maxCauseLength, "[truncated]")),
TaskToken: &t.taskToken,
})
if sendErr != nil {
Expand All @@ -39,6 +44,20 @@ func (t TaskRunner) sendTaskFailure(err TaskFailureError) error {
return err
}

// Returns its input truncated to maxLength, with the ability to replace the end to indicate truncation.
//
// For example, truncateString(s, l, "") just truncates to length l. But truncateString(s, l, "xy") will
// first truncate to length l, then replace the last two characters with "xy"
func truncateString(s string, maxLength int, truncationIndicatorSuffix string) string {
if len(s) <= maxLength {
return s
}
// when we cut out some number of bytes from the end, we may be cutting in the middle of a multi-byte unicode char
// if so, we can use ToValidUTF8 to trim it a teeny bit further to eliminate the whole char.
// (Note, this does mean invalid UTF8 inputs will see more changes than expected, but we won't worry about that)
return strings.ToValidUTF8(s[:maxLength-len(truncationIndicatorSuffix)], "") + truncationIndicatorSuffix
}

// TaskFailureUnknown is used for any error that is unexpected or not understood completely.
type TaskFailureUnknown struct {
error
Expand Down

0 comments on commit a47f8ae

Please sign in to comment.