github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/internal/fnruntime/fnerrors.go (about)

     1  // Copyright 2021 The kpt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fnruntime
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  )
    21  
    22  const (
    23  	FnExecErrorTruncateLines = 4
    24  	// FnExecErrorIndentation is the number of spaces at the beginning of each
    25  	// line of function failure messages.
    26  	FnExecErrorIndentation = 2
    27  )
    28  
    29  // ExecError implements an error type that stores information about function failure.
    30  type ExecError struct {
    31  	// OriginalErr is the original error returned from function runtime
    32  	OriginalErr error
    33  
    34  	// TruncateOutput indicates should error message be truncated
    35  	TruncateOutput bool
    36  
    37  	// Stderr is the content written to function stderr
    38  	Stderr string `yaml:"stderr,omitempty"`
    39  
    40  	// ExitCode is the exit code returned from function
    41  	ExitCode int `yaml:"exitCode,omitempty"`
    42  }
    43  
    44  // String returns string representation of the failure.
    45  func (fe *ExecError) String() string {
    46  	var b strings.Builder
    47  
    48  	errLines := &MultiLineFormatter{
    49  		Title:          "Stderr",
    50  		Lines:          strings.Split(fe.Stderr, "\n"),
    51  		UseQuote:       true,
    52  		TruncateOutput: fe.TruncateOutput,
    53  	}
    54  	b.WriteString(errLines.String())
    55  	b.WriteString(fmt.Sprintf("  Exit Code: %d\n", fe.ExitCode))
    56  	return b.String()
    57  }
    58  
    59  func (fe *ExecError) Error() string {
    60  	return fe.String()
    61  }