github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+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/manifestparser"
    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 actionerror.AssignDropletError:
    33  		return AssignDropletError(e)
    34  	case actionerror.BuildpackNotFoundError:
    35  		return BuildpackNotFoundError(e)
    36  	case actionerror.BuildpackStackChangeError:
    37  		return BuildpackStackChangeError(e)
    38  	case actionerror.CommandLineOptionsWithMultipleAppsError:
    39  		return CommandLineArgsWithMultipleAppsError{}
    40  	case actionerror.DockerPasswordNotSetError:
    41  		return DockerPasswordNotSetError{}
    42  	case actionerror.DomainNotFoundError:
    43  		return DomainNotFoundError(e)
    44  	case manifest.EmptyBuildpacksError:
    45  		return EmptyBuildpacksError(e)
    46  	case actionerror.EmptyArchiveError:
    47  		return EmptyArchiveError(e)
    48  	case actionerror.EmptyDirectoryError:
    49  		return EmptyDirectoryError(e)
    50  	case actionerror.EmptyBuildpackDirectoryError:
    51  		return EmptyBuildpackDirectoryError(e)
    52  	case actionerror.FileChangedError:
    53  		return FileChangedError(e)
    54  	case actionerror.GettingPluginRepositoryError:
    55  		return GettingPluginRepositoryError(e)
    56  	case actionerror.HostnameWithTCPDomainError:
    57  		return HostnameWithTCPDomainError(e)
    58  	case actionerror.HTTPHealthCheckInvalidError:
    59  		return HTTPHealthCheckInvalidError{}
    60  	case actionerror.InvalidBuildpacksError:
    61  		return InvalidBuildpacksError{}
    62  	case actionerror.InvalidHTTPRouteSettings:
    63  		return PortNotAllowedWithHTTPDomainError(e)
    64  	case actionerror.InvalidRouteError:
    65  		return InvalidRouteError(e)
    66  	case actionerror.InvalidTCPRouteSettings:
    67  		return HostAndPathNotAllowedWithTCPDomainError(e)
    68  	case actionerror.IsolationSegmentNotFoundError:
    69  		return IsolationSegmentNotFoundError(e)
    70  	case actionerror.MissingNameError:
    71  		return RequiredNameForPushError{}
    72  	case actionerror.MultipleBuildpacksFoundError:
    73  		return MultipleBuildpacksFoundError(e)
    74  	case actionerror.NoCompatibleBinaryError:
    75  		return NoCompatibleBinaryError{}
    76  	case actionerror.NoDomainsFoundError:
    77  		return NoDomainsFoundError{}
    78  	case actionerror.NoHostnameAndSharedDomainError:
    79  		return NoHostnameAndSharedDomainError{}
    80  	case actionerror.NoMatchingDomainError:
    81  		return NoMatchingDomainError(e)
    82  	case actionerror.NonexistentAppPathError:
    83  		return FileNotFoundError(e)
    84  	case actionerror.NoOrganizationTargetedError:
    85  		return NoOrganizationTargetedError(e)
    86  	case actionerror.NoSpaceTargetedError:
    87  		return NoSpaceTargetedError(e)
    88  	case actionerror.NotLoggedInError:
    89  		return NotLoggedInError(e)
    90  	case actionerror.OrganizationNotFoundError:
    91  		return OrganizationNotFoundError(e)
    92  	case actionerror.OrganizationQuotaNotFoundForNameError:
    93  		return OrganizationQuotaNotFoundForNameError(e)
    94  	case actionerror.PasswordGrantTypeLogoutRequiredError:
    95  		return PasswordGrantTypeLogoutRequiredError(e)
    96  	case actionerror.PluginCommandsConflictError:
    97  		return PluginCommandsConflictError(e)
    98  	case actionerror.PluginInvalidError:
    99  		return PluginInvalidError(e)
   100  	case actionerror.PluginNotFoundError:
   101  		return PluginNotFoundError(e)
   102  	case actionerror.ProcessInstanceNotFoundError:
   103  		return ProcessInstanceNotFoundError(e)
   104  	case actionerror.ProcessInstanceNotRunningError:
   105  		return ProcessInstanceNotRunningError(e)
   106  	case actionerror.ProcessNotFoundError:
   107  		return ProcessNotFoundError(e)
   108  	case actionerror.PropertyCombinationError:
   109  		return PropertyCombinationError(e)
   110  	case actionerror.RepositoryNameTakenError:
   111  		return RepositoryNameTakenError(e)
   112  	case actionerror.RepositoryNotRegisteredError:
   113  		return RepositoryNotRegisteredError(e)
   114  	case actionerror.RouteInDifferentSpaceError:
   115  		return RouteInDifferentSpaceError(e)
   116  	case actionerror.RoutePathWithTCPDomainError:
   117  		return RoutePathWithTCPDomainError(e)
   118  	case actionerror.RouterGroupNotFoundError:
   119  		return RouterGroupNotFoundError(e)
   120  	case actionerror.SecurityGroupNotFoundError:
   121  		return SecurityGroupNotFoundError(e)
   122  	case actionerror.ServiceInstanceNotFoundError:
   123  		return ServiceInstanceNotFoundError(e)
   124  	case actionerror.ServiceInstanceNotShareableError:
   125  		return ServiceInstanceNotShareableError{
   126  			FeatureFlagEnabled:          e.FeatureFlagEnabled,
   127  			ServiceBrokerSharingEnabled: e.ServiceBrokerSharingEnabled,
   128  		}
   129  	case actionerror.ServiceInstanceNotSharedToSpaceError:
   130  		return ServiceInstanceNotSharedToSpaceError{ServiceInstanceName: e.ServiceInstanceName}
   131  	case actionerror.ServicePlanNotFoundError:
   132  		return ServicePlanNotFoundError(e)
   133  	case actionerror.SharedServiceInstanceNotFoundError:
   134  		return SharedServiceInstanceNotFoundError(e)
   135  	case actionerror.SpaceNotFoundError:
   136  		return SpaceNotFoundError{Name: e.Name}
   137  	case actionerror.SpaceQuotaNotFoundByNameError:
   138  		return SpaceQuotaNotFoundByNameError{Name: e.Name}
   139  	case actionerror.StackNotFoundError:
   140  		return StackNotFoundError(e)
   141  	case actionerror.StagingTimeoutError:
   142  		return StagingTimeoutError(e)
   143  	case actionerror.TaskWorkersUnavailableError:
   144  		return RunTaskError{Message: "Task workers are unavailable."}
   145  	case actionerror.TCPRouteOptionsNotProvidedError:
   146  		return TCPRouteOptionsNotProvidedError{}
   147  	case actionerror.TriggerLegacyPushError:
   148  		return TriggerLegacyPushError{DomainHostRelated: e.DomainHostRelated}
   149  	case actionerror.UploadFailedError:
   150  		return UploadFailedError{Err: ConvertToTranslatableError(e.Err)}
   151  	case actionerror.CommandLineOptionsAndManifestConflictError:
   152  		return CommandLineOptionsAndManifestConflictError{
   153  			ManifestAttribute:  e.ManifestAttribute,
   154  			CommandLineOptions: e.CommandLineOptions,
   155  		}
   156  
   157  	// Wrapped Errors
   158  	case TipDecoratorError:
   159  		e.BaseError = ConvertToTranslatableError(e.BaseError)
   160  		return e
   161  
   162  	// Generic CC Errors
   163  	case ccerror.APINotFoundError:
   164  		return APINotFoundError(e)
   165  	case ccerror.RequestError:
   166  		return APIRequestError(e)
   167  	case ccerror.SSLValidationHostnameError:
   168  		return SSLCertError(e)
   169  	case ccerror.UnverifiedServerError:
   170  		return InvalidSSLCertError(e)
   171  
   172  	// Specific CC Errors
   173  	case ccerror.V2JobFailedError:
   174  		return JobFailedError(e)
   175  	case ccerror.V3JobFailedError:
   176  		return JobFailedError{JobGUID: e.JobGUID, Message: e.Detail}
   177  	case ccerror.JobTimeoutError:
   178  		return JobTimeoutError{JobGUID: e.JobGUID}
   179  	case ccerror.MultiError:
   180  		return MultiError{Messages: e.Details()}
   181  	case ccerror.UnprocessableEntityError:
   182  		if strings.Contains(e.Message, "Task must have a droplet. Specify droplet or assign current droplet to app.") {
   183  			return RunTaskError{Message: "App is not staged."}
   184  		}
   185  
   186  	// JSON Errors
   187  	case *json.SyntaxError:
   188  		return JSONSyntaxError{Err: e}
   189  
   190  	// Manifest Errors
   191  	case manifest.ManifestCreationError:
   192  		return ManifestCreationError(e)
   193  	case manifest.InheritanceFieldError:
   194  		return TriggerLegacyPushError{InheritanceRelated: true}
   195  	case manifest.GlobalFieldsError:
   196  		return TriggerLegacyPushError{GlobalRelated: e.Fields}
   197  	case manifest.InterpolationError:
   198  		return InterpolationError(e)
   199  
   200  	// ManifestParser Errors
   201  	case manifestparser.InterpolationError:
   202  		return InterpolationError(e)
   203  	case manifestparser.InvalidYAMLError:
   204  		return InvalidYAMLError(e)
   205  
   206  	// Plugin Execution Errors
   207  	case pluginerror.RawHTTPStatusError:
   208  		return DownloadPluginHTTPError{Message: e.Status}
   209  	case pluginerror.SSLValidationHostnameError:
   210  		return DownloadPluginHTTPError(e)
   211  	case pluginerror.UnverifiedServerError:
   212  		return DownloadPluginHTTPError{Message: e.Error()}
   213  
   214  	// SSH Errors
   215  	case ssherror.UnableToAuthenticateError:
   216  		return SSHUnableToAuthenticateError{}
   217  
   218  	// UAA Errors
   219  	case uaa.UnauthorizedError:
   220  		return UnauthorizedError(e)
   221  	case uaa.InsufficientScopeError:
   222  		return UnauthorizedToPerformActionError{}
   223  	case uaa.InvalidAuthTokenError:
   224  		return InvalidRefreshTokenError{}
   225  
   226  	// Other Errors
   227  	case download.RawHTTPStatusError:
   228  		return HTTPStatusError{Status: e.Status}
   229  	}
   230  
   231  	return err
   232  }