github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/command/translatableerror/translate_test.go (about)

     1  package translatableerror_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"text/template"
     7  
     8  	. "code.cloudfoundry.org/cli/command/translatableerror"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/ginkgo/extensions/table"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Translatable Errors", func() {
    16  	translateFunc := func(s string, vars ...interface{}) string {
    17  		formattedTemplate, err := template.New("test template").Parse(s)
    18  		Expect(err).ToNot(HaveOccurred())
    19  		formattedTemplate.Option("missingkey=error")
    20  
    21  		var buffer bytes.Buffer
    22  		if len(vars) > 0 {
    23  			err = formattedTemplate.Execute(&buffer, vars[0])
    24  			Expect(err).ToNot(HaveOccurred())
    25  
    26  			return buffer.String()
    27  		} else {
    28  			return s
    29  		}
    30  	}
    31  
    32  	DescribeTable("translates error",
    33  		func(e error) {
    34  			err, ok := e.(TranslatableError)
    35  			Expect(ok).To(BeTrue())
    36  			err.Translate(translateFunc)
    37  		},
    38  
    39  		Entry("AddPluginRepositoryError", AddPluginRepositoryError{}),
    40  		Entry("APINotFoundError", APINotFoundError{}),
    41  		Entry("APIRequestError", APIRequestError{}),
    42  		Entry("ApplicationNotFoundError", ApplicationNotFoundError{}),
    43  		Entry("AppNotFoundInManifestError", AppNotFoundInManifestError{}),
    44  		Entry("ArgumentCombinationError", ArgumentCombinationError{}),
    45  		Entry("AssignDropletError", AssignDropletError{}),
    46  		Entry("BadCredentialsError", UnauthorizedError{}),
    47  		Entry("BuildpackNotFoundError", BuildpackNotFoundError{}),
    48  		Entry("BuildpackStackChangeError", BuildpackStackChangeError{}),
    49  		Entry("CFNetworkingEndpointNotFoundError", CFNetworkingEndpointNotFoundError{}),
    50  		Entry("CommandLineArgsWithMultipleAppsError", CommandLineArgsWithMultipleAppsError{}),
    51  		Entry("CommandLineOptionsAndManifestConflictError", CommandLineOptionsAndManifestConflictError{}),
    52  		Entry("DockerPasswordNotSetError", DockerPasswordNotSetError{}),
    53  		Entry("DownloadPluginHTTPError", DownloadPluginHTTPError{}),
    54  		Entry("EmptyDirectoryError", EmptyDirectoryError{}),
    55  		Entry("EmptyBuildpacksError", EmptyBuildpacksError{}),
    56  		Entry("FetchingPluginInfoFromRepositoriesError", FetchingPluginInfoFromRepositoriesError{}),
    57  		Entry("FileChangedError", FileChangedError{}),
    58  		Entry("FileNotFoundError", FileNotFoundError{}),
    59  		Entry("GettingPluginRepositoryError", GettingPluginRepositoryError{}),
    60  		Entry("HealthCheckTypeUnsupportedError", HealthCheckTypeUnsupportedError{SupportedTypes: []string{"some-type", "another-type"}}),
    61  		Entry("HostAndPathNotAllowedWithTCPDomainError", HostAndPathNotAllowedWithTCPDomainError{}),
    62  		Entry("HostnameWithTCPDomainError", HostnameWithTCPDomainError{}),
    63  		Entry("HTTPHealthCheckInvalidError", HTTPHealthCheckInvalidError{}),
    64  		Entry("HTTPStatusError", HTTPStatusError{Status: "some status"}),
    65  		Entry("InvalidChecksumError", InvalidChecksumError{}),
    66  		Entry("InvalidRouteError", InvalidRouteError{}),
    67  		Entry("InvalidSSLCertError", InvalidSSLCertError{}),
    68  		Entry("IsolationSegmentNotFoundError", IsolationSegmentNotFoundError{}),
    69  		Entry("JobFailedError", JobFailedError{}),
    70  		Entry("JobTimeoutError", JobTimeoutError{}),
    71  		Entry("JSONSyntaxError", JSONSyntaxError{Err: errors.New("some-error")}),
    72  		Entry("LifecycleMinimumAPIVersionNotMetError", LifecycleMinimumAPIVersionNotMetError{}),
    73  		Entry("ManifestCreationError", ManifestCreationError{}),
    74  		Entry("ManifestFileNotFoundInDirectoryError", ManifestFileNotFoundInDirectoryError{}),
    75  		Entry("MinimumCFAPIVersionNotMetError", MinimumCFAPIVersionNotMetError{}),
    76  		Entry("MinimumCLIVersionNotMetError", MinimumCLIVersionNotMetError{}),
    77  		Entry("MissingCredentialsError", MissingCredentialsError{}),
    78  		Entry("MultiError", MultiError{}),
    79  		Entry("NetworkPolicyProtocolOrPortNotProvidedError", NetworkPolicyProtocolOrPortNotProvidedError{}),
    80  		Entry("NoAPISetError", NoAPISetError{}),
    81  		Entry("NoCompatibleBinaryError", NoCompatibleBinaryError{}),
    82  		Entry("NoDomainsFoundError", NoDomainsFoundError{}),
    83  		Entry("NoMatchingDomainError", NoMatchingDomainError{}),
    84  		Entry("NoOrganizationTargetedError", NoOrganizationTargetedError{}),
    85  		Entry("NoPluginRepositoriesError", NoPluginRepositoriesError{}),
    86  		Entry("NoSpaceTargetedError", NoSpaceTargetedError{}),
    87  		Entry("NotLoggedInError", NotLoggedInError{}),
    88  		Entry("OrgNotFoundError", OrganizationNotFoundError{}),
    89  		Entry("QuotaNotFoundForNameError", QuotaNotFoundForNameError{}),
    90  		Entry("ParseArgumentError", ParseArgumentError{}),
    91  		Entry("PasswordGrantTypeLogoutRequiredError", PasswordGrantTypeLogoutRequiredError{}),
    92  		Entry("PluginAlreadyInstalledError", PluginAlreadyInstalledError{}),
    93  		Entry("PluginBinaryRemoveFailedError", PluginBinaryRemoveFailedError{}),
    94  		Entry("PluginBinaryUninstallError", PluginBinaryUninstallError{}),
    95  		Entry("PluginCommandsConflictError", PluginCommandsConflictError{}),
    96  		Entry("PluginInvalidError", PluginInvalidError{Err: errors.New("invalid error")}),
    97  		Entry("PluginInvalidError", PluginInvalidError{}),
    98  		Entry("PluginNotFoundError", PluginNotFoundError{}),
    99  		Entry("PluginNotFoundInRepositoryError", PluginNotFoundInRepositoryError{}),
   100  		Entry("PluginNotFoundOnDiskOrInAnyRepositoryError", PluginNotFoundOnDiskOrInAnyRepositoryError{}),
   101  		Entry("PortNotAllowedWithHTTPDomainError", PortNotAllowedWithHTTPDomainError{}),
   102  		Entry("ProcessInstanceNotFoundError", ProcessInstanceNotFoundError{ProcessType: "some-process", InstanceIndex: 1}),
   103  		Entry("ProcessInstanceNotRunningError", ProcessInstanceNotRunningError{ProcessType: "some-process", InstanceIndex: 1}),
   104  		Entry("PropertyCombinationError", PropertyCombinationError{Properties: []string{"property-1", "property-2"}}),
   105  		Entry("RepositoryNameTakenError", RepositoryNameTakenError{}),
   106  		Entry("RequiredArgumentError", RequiredArgumentError{}),
   107  		Entry("RequiredFlagsError", RequiredFlagsError{}),
   108  		Entry("RouteInDifferentSpaceError", RouteInDifferentSpaceError{}),
   109  		Entry("RoutePathWithTCPDomainError", RoutePathWithTCPDomainError{}),
   110  		Entry("RunTaskError", RunTaskError{}),
   111  		Entry("SecurityGroupNotFoundError", SecurityGroupNotFoundError{}),
   112  		Entry("ServiceInstanceNotShareableError", ServiceInstanceNotShareableError{}),
   113  		Entry("ServiceInstanceNotFoundError", ServiceInstanceNotFoundError{}),
   114  		Entry("SharedServiceInstanceNotFoundError", SharedServiceInstanceNotFoundError{}),
   115  		Entry("SpaceNotFoundError", SpaceNotFoundError{}),
   116  		Entry("SSHUnableToAuthenticateError", SSHUnableToAuthenticateError{}),
   117  		Entry("SSLCertError", SSLCertError{}),
   118  		Entry("StackNotFoundError with name", SpaceNotFoundError{Name: "steve"}),
   119  		Entry("StackNotFoundError without name", SpaceNotFoundError{}),
   120  		Entry("StagingFailedError", StagingFailedError{}),
   121  		Entry("StagingFailedNoAppDetectedError", StagingFailedNoAppDetectedError{}),
   122  		Entry("StagingTimeoutError", StagingTimeoutError{}),
   123  		Entry("StartupTimeoutError", StartupTimeoutError{}),
   124  		Entry("ThreeRequiredArgumentsError", ThreeRequiredArgumentsError{}),
   125  		Entry("TriggerLegacyPushError", TriggerLegacyPushError{}),
   126  		Entry("UnsupportedURLSchemeError", UnsupportedURLSchemeError{}),
   127  		Entry("UploadFailedError", UploadFailedError{Err: JobFailedError{}}),
   128  		Entry("V3APIDoesNotExistError", V3APIDoesNotExistError{}),
   129  	)
   130  
   131  	Describe("PluginInvalidError", func() {
   132  		When("the wrapped error is nil", func() {
   133  			It("does not concatenate the nil error in the returned Error()", func() {
   134  				err := PluginInvalidError{}
   135  				Expect(err.Error()).To(Equal("File is not a valid cf CLI plugin binary."))
   136  			})
   137  		})
   138  
   139  		When("the wrapped error is not nil", func() {
   140  			It("does prepends the error message in the returned Error()", func() {
   141  				err := PluginInvalidError{Err: errors.New("ello")}
   142  				Expect(err.Error()).To(Equal("ello\nFile is not a valid cf CLI plugin binary."))
   143  			})
   144  		})
   145  	})
   146  })