...

Source file src/edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/format/error.go

Documentation: edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/format

     1  package format
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"edge-infra.dev/pkg/edge/api/graph/model"
     8  )
     9  
    10  type actionType int
    11  
    12  const (
    13  	applyAction actionType = iota
    14  	deleteAction
    15  )
    16  
    17  // GenerateDeleteOutput creates a formatted string for displaying deletion
    18  // errors to the user
    19  func GenerateDeleteOutput(inputType string, errors []*model.OperatorInterventionErrorResponse) string {
    20  	return generateOutput(deleteAction, inputType, errors)
    21  }
    22  
    23  // GenerateApplyOutput creates a formatted string for displaying errors that
    24  // occur during create and update mutations to the user
    25  func GenerateApplyOutput(inputType string, errors []*model.OperatorInterventionErrorResponse) string {
    26  	return generateOutput(applyAction, inputType, errors)
    27  }
    28  
    29  func generateOutput(action actionType, inputType string, errors []*model.OperatorInterventionErrorResponse) string {
    30  	var pastAction string
    31  	var presentAction string
    32  	switch action {
    33  	case applyAction:
    34  		pastAction = "applied"
    35  		presentAction = "applying"
    36  	case deleteAction:
    37  		pastAction = "deleted"
    38  		presentAction = "deleting"
    39  	}
    40  
    41  	if len(errors) == 0 {
    42  		return fmt.Sprintf("\nOI %s %s\n", inputType, pastAction)
    43  	}
    44  
    45  	var sb strings.Builder
    46  
    47  	sb.WriteString(fmt.Sprintf("\nErrors occurred when %s OI %s: \n", presentAction, inputType))
    48  
    49  	for _, e := range errors {
    50  		sb.WriteString(formatError(e))
    51  		sb.WriteString("\n")
    52  	}
    53  
    54  	sb.WriteString(fmt.Sprintf("\nOI %s not %s.\n", inputType, pastAction))
    55  
    56  	return sb.String()
    57  }
    58  
    59  // generates an error message string corresponding to the particular error
    60  func formatError(e *model.OperatorInterventionErrorResponse) string {
    61  	resp := fmt.Sprintf("\tError: %s", e.Type)
    62  
    63  	var args []string
    64  	if e.Role != nil {
    65  		args = append(args, fmt.Sprintf("Role: %q", *e.Role))
    66  	}
    67  	if e.Privilege != nil {
    68  		args = append(args, fmt.Sprintf("Privilege: %q", *e.Privilege))
    69  	}
    70  	if e.Command != nil {
    71  		args = append(args, fmt.Sprintf("Command: %q", *e.Command))
    72  	}
    73  
    74  	if len(args) != 0 {
    75  		resp = fmt.Sprintf("%s. Details: ", resp)
    76  	}
    77  
    78  	resp = fmt.Sprintf("%s%s", resp, strings.Join(args, ", "))
    79  
    80  	return resp
    81  }
    82  

View as plain text