github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/translatableerror/convert_to_translatable_error.go (about)

     1  package translatableerror
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/plugin/pluginerror"
    11  	"code.cloudfoundry.org/cli/api/uaa"
    12  	"code.cloudfoundry.org/cli/util/clissh/ssherror"
    13  	"code.cloudfoundry.org/cli/util/download"
    14  	"code.cloudfoundry.org/cli/util/manifest"
    15  	"code.cloudfoundry.org/cli/util/v6manifestparser"
    16  	log "github.com/sirupsen/logrus"
    17  )
    18  
    19  func ConvertToTranslatableError(err error) error {
    20  	log.WithField("err", fmt.Sprintf("%#v", err)).Debugf("convert to translatable error")
    21  
    22  	switch e := err.(type) {
    23  	// Action Errors
    24  	case actionerror.AddPluginRepositoryError:
    25  		return AddPluginRepositoryError(e)
    26  	case actionerror.ApplicationNotFoundError:
    27  		return ApplicationNotFoundError(e)
    28  	case actionerror.ApplicationNotStartedError:
    29  		return ApplicationNotStartedError(e)
    30  	case actionerror.AppNotFoundInManifestError:
    31  		return AppNotFoundInManifestError(e)
    32  	case v6manifestparser.AppNotInManifestError:
    33  		return AppNotFoundInManifestError(e)
    34  	case actionerror.AssignDropletError:
    35  		return AssignDropletError(e)
    36  	case actionerror.BuildpackNotFoundError:
    37  		return BuildpackNotFoundError(e)
    38  	case actionerror.BuildpackStackChangeError:
    39  		return BuildpackStackChangeError(e)
    40  	case actionerror.CommandLineOptionsWithMultipleAppsError:
    41  		return CommandLineArgsWithMultipleAppsError{}
    42  	case actionerror.DockerPasswordNotSetError:
    43  		return DockerPasswordNotSetError{}
    44  	case actionerror.DomainNotFoundError:
    45  		return DomainNotFoundError(e)
    46  	case manifest.EmptyBuildpacksError:
    47  		return EmptyBuildpacksError(e)
    48  	case actionerror.EmptyArchiveError:
    49  		return EmptyDirectoryError(e)
    50  	case actionerror.EmptyDirectoryError:
    51  		return EmptyDirectoryError(e)
    52  	case actionerror.EmptyBuildpackDirectoryError:
    53  		return EmptyBuildpackDirectoryError(e)
    54  	case actionerror.FileChangedError:
    55  		return FileChangedError(e)
    56  	case actionerror.GettingPluginRepositoryError:
    57  		return GettingPluginRepositoryError(e)
    58  	case actionerror.HostnameWithTCPDomainError:
    59  		return HostnameWithTCPDomainError(e)
    60  	case actionerror.HTTPHealthCheckInvalidError:
    61  		return HTTPHealthCheckInvalidError{}
    62  	case actionerror.InvalidBuildpacksError:
    63  		return InvalidBuildpacksError{}
    64  	case actionerror.InvalidHTTPRouteSettings:
    65  		return PortNotAllowedWithHTTPDomainError(e)
    66  	case actionerror.InvalidRouteError:
    67  		return InvalidRouteError(e)
    68  	case actionerror.InvalidTCPRouteSettings:
    69  		return HostAndPathNotAllowedWithTCPDomainError(e)
    70  	case actionerror.IsolationSegmentNotFoundError:
    71  		return IsolationSegmentNotFoundError(e)
    72  	case actionerror.MissingNameError:
    73  		return AppNameOrManifestRequiredError{}
    74  	case actionerror.MultipleBuildpacksFoundError:
    75  		return MultipleBuildpacksFoundError(e)
    76  	case actionerror.NoCompatibleBinaryError:
    77  		return NoCompatibleBinaryError{}
    78  	case actionerror.NoDomainsFoundError:
    79  		return NoDomainsFoundError{}
    80  	case actionerror.NoHostnameAndSharedDomainError:
    81  		return NoHostnameAndSharedDomainError{}
    82  	case actionerror.NoMatchingDomainError:
    83  		return NoMatchingDomainError(e)
    84  	case actionerror.NonexistentAppPathError:
    85  		return FileNotFoundError(e)
    86  	case v6manifestparser.InvalidManifestApplicationPathError:
    87  		return FileNotFoundError(e)
    88  	case actionerror.NoOrganizationTargetedError:
    89  		return NoOrganizationTargetedError(e)
    90  	case actionerror.NoSpaceTargetedError:
    91  		return NoSpaceTargetedError(e)
    92  	case actionerror.NotLoggedInError:
    93  		return NotLoggedInError(e)
    94  	case actionerror.OrganizationNotFoundError:
    95  		return OrganizationNotFoundError(e)
    96  	case actionerror.QuotaNotFoundForNameError:
    97  		return QuotaNotFoundForNameError(e)
    98  	case actionerror.PasswordGrantTypeLogoutRequiredError:
    99  		return PasswordGrantTypeLogoutRequiredError(e)
   100  	case actionerror.PluginCommandsConflictError:
   101  		return PluginCommandsConflictError(e)
   102  	case actionerror.PluginInvalidError:
   103  		return PluginInvalidError(e)
   104  	case actionerror.PluginNotFoundError:
   105  		return PluginNotFoundError(e)
   106  	case actionerror.ProcessInstanceNotFoundError:
   107  		return ProcessInstanceNotFoundError(e)
   108  	case actionerror.ProcessInstanceNotRunningError:
   109  		return ProcessInstanceNotRunningError(e)
   110  	case actionerror.ProcessNotFoundError:
   111  		return ProcessNotFoundError(e)
   112  	case actionerror.PropertyCombinationError:
   113  		return PropertyCombinationError(e)
   114  	case actionerror.RepositoryNameTakenError:
   115  		return RepositoryNameTakenError(e)
   116  	case actionerror.RepositoryNotRegisteredError:
   117  		return RepositoryNotRegisteredError(e)
   118  	case actionerror.RouteInDifferentSpaceError:
   119  		return RouteInDifferentSpaceError(e)
   120  	case actionerror.RoutePathWithTCPDomainError:
   121  		return RoutePathWithTCPDomainError(e)
   122  	case actionerror.RouterGroupNotFoundError:
   123  		return RouterGroupNotFoundError(e)
   124  	case actionerror.SecurityGroupNotFoundError:
   125  		return SecurityGroupNotFoundError(e)
   126  	case actionerror.ServiceInstanceNotFoundError:
   127  		return ServiceInstanceNotFoundError(e)
   128  	case actionerror.ServiceInstanceNotShareableError:
   129  		return ServiceInstanceNotShareableError{
   130  			FeatureFlagEnabled:          e.FeatureFlagEnabled,
   131  			ServiceBrokerSharingEnabled: e.ServiceBrokerSharingEnabled,
   132  		}
   133  	case actionerror.ServiceInstanceNotSharedToSpaceError:
   134  		return ServiceInstanceNotSharedToSpaceError{ServiceInstanceName: e.ServiceInstanceName}
   135  	case actionerror.ServicePlanNotFoundError:
   136  		return ServicePlanNotFoundError(e)
   137  	case actionerror.SharedServiceInstanceNotFoundError:
   138  		return SharedServiceInstanceNotFoundError(e)
   139  	case actionerror.SpaceNotFoundError:
   140  		return SpaceNotFoundError{Name: e.Name}
   141  	case actionerror.StackNotFoundError:
   142  		return StackNotFoundError(e)
   143  	case actionerror.StagingFailedError:
   144  		return StagingFailedError{Message: e.Reason}
   145  	case actionerror.StagingTimeoutError:
   146  		return StagingTimeoutError(e)
   147  	case actionerror.TaskWorkersUnavailableError:
   148  		return RunTaskError{Message: "Task workers are unavailable."}
   149  	case actionerror.TCPRouteOptionsNotProvidedError:
   150  		return TCPRouteOptionsNotProvidedError{}
   151  	case actionerror.TriggerLegacyPushError:
   152  		return TriggerLegacyPushError{DomainHostRelated: e.DomainHostRelated}
   153  	case actionerror.UploadFailedError:
   154  		return UploadFailedError{Err: ConvertToTranslatableError(e.Err)}
   155  	case actionerror.CommandLineOptionsAndManifestConflictError:
   156  		return CommandLineOptionsAndManifestConflictError{
   157  			ManifestAttribute:  e.ManifestAttribute,
   158  			CommandLineOptions: e.CommandLineOptions,
   159  		}
   160  
   161  	// Wrapped Errors
   162  	case TipDecoratorError:
   163  		e.BaseError = ConvertToTranslatableError(e.BaseError)
   164  		return e
   165  
   166  	// Generic CC Errors
   167  	case ccerror.APINotFoundError:
   168  		return APINotFoundError(e)
   169  	case ccerror.RequestError:
   170  		return APIRequestError(e)
   171  	case ccerror.SSLValidationHostnameError:
   172  		return SSLCertError(e)
   173  	case ccerror.UnverifiedServerError:
   174  		return InvalidSSLCertError{URL: e.URL, SuggestedCommand: "api"}
   175  
   176  	// Specific CC Errors
   177  	case ccerror.V2JobFailedError:
   178  		return JobFailedError(e)
   179  	case ccerror.V3JobFailedError:
   180  		return JobFailedError{JobGUID: e.JobGUID, Message: e.Detail}
   181  	case ccerror.JobTimeoutError:
   182  		return JobTimeoutError{JobGUID: e.JobGUID}
   183  	case ccerror.MultiError:
   184  		return MultiError{Messages: e.Details()}
   185  	case ccerror.UnprocessableEntityError:
   186  		if strings.Contains(e.Message, "Task must have a droplet. Specify droplet or assign current droplet to app.") {
   187  			return RunTaskError{Message: "App is not staged."}
   188  		}
   189  
   190  	// JSON Errors
   191  	case *json.SyntaxError:
   192  		return JSONSyntaxError{Err: e}
   193  
   194  	// Manifest Errors
   195  	case manifest.ManifestCreationError:
   196  		return ManifestCreationError(e)
   197  	case manifest.InheritanceFieldError:
   198  		return TriggerLegacyPushError{InheritanceRelated: true}
   199  	case manifest.GlobalFieldsError:
   200  		return TriggerLegacyPushError{GlobalRelated: e.Fields}
   201  	case manifest.InterpolationError:
   202  		return InterpolationError(e)
   203  
   204  	// ManifestParser Errors
   205  	case v6manifestparser.InterpolationError:
   206  		return InterpolationError(e)
   207  	case v6manifestparser.InvalidYAMLError:
   208  		return InvalidYAMLError(e)
   209  
   210  	// Plugin Execution Errors
   211  	case pluginerror.RawHTTPStatusError:
   212  		return DownloadPluginHTTPError{Message: e.Status}
   213  	case pluginerror.SSLValidationHostnameError:
   214  		return DownloadPluginHTTPError(e)
   215  	case pluginerror.UnverifiedServerError:
   216  		return DownloadPluginHTTPError{Message: e.Error()}
   217  
   218  	// SSH Errors
   219  	case ssherror.UnableToAuthenticateError:
   220  		return SSHUnableToAuthenticateError{}
   221  
   222  	// UAA Errors
   223  	case uaa.UnauthorizedError:
   224  		return UnauthorizedError(e)
   225  	case uaa.AccountLockedError:
   226  		return AccountLockedError(e)
   227  	case uaa.InsufficientScopeError:
   228  		return UnauthorizedToPerformActionError{}
   229  	case uaa.InvalidAuthTokenError:
   230  		return InvalidRefreshTokenError{}
   231  
   232  	// Other Errors
   233  	case download.RawHTTPStatusError:
   234  		return HTTPStatusError{Status: e.Status}
   235  	}
   236  
   237  	return err
   238  }