diff --git a/cmd/describe.go b/cmd/describe.go index 129c561923..30bed1feb3 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -37,7 +37,7 @@ the current directory or from the directory specified with --path. Aliases: []string{"info", "desc"}, PreRunE: bindEnv("output", "path", "namespace", "verbose"), RunE: func(cmd *cobra.Command, args []string) error { - return runDescribe(cmd, args, newClient) + return wrapDescribeError(runDescribe(cmd, args, newClient)) }, } diff --git a/cmd/describe_test.go b/cmd/describe_test.go index 73c53636ae..7631cb3788 100644 --- a/cmd/describe_test.go +++ b/cmd/describe_test.go @@ -164,3 +164,29 @@ func TestDescribe_NameAndPathExclusivity(t *testing.T) { t.Fatal("describer was invoked when conflicting flags were provided") } } + +// TestDescribe_WrapsNotInitialized ensures that describe command wraps +// ErrNotInitialized with CLI-specific guidance via wrapDescribeError. +func TestDescribe_WrapsNotInitialized(t *testing.T) { + _ = FromTempDirectory(t) + describer := mock.NewDescriber() + + cmd := NewDescribeCmd(NewTestClient(fn.WithDescribers(describer))) + cmd.SetArgs([]string{}) + err := cmd.Execute() + + if err == nil { + t.Fatal("expected an error when describing a non-existent function") + } + var cliNotInit *ErrNotInitialized + if !errors.As(err, &cliNotInit) { + t.Fatalf("expected ErrNotInitialized, got %T: %v", err, err) + } + if cliNotInit.Cmd != "describe" { + t.Fatalf("expected Cmd 'describe', got '%v'", cliNotInit.Cmd) + } + if !strings.Contains(err.Error(), "func describe") { + t.Fatalf("expected error to contain 'func describe' guidance, got: %v", err) + } + +} diff --git a/cmd/invoke.go b/cmd/invoke.go index 657edd9b55..12a9c03ad8 100644 --- a/cmd/invoke.go +++ b/cmd/invoke.go @@ -108,7 +108,7 @@ EXAMPLES "data", "content-type", "request-type", "file", "insecure", "confirm", "verbose", "extension"), RunE: func(cmd *cobra.Command, args []string) error { - return runInvoke(cmd, args, newClient) + return wrapInvokeError(runInvoke(cmd, args, newClient)) }, } @@ -163,7 +163,7 @@ func runInvoke(cmd *cobra.Command, _ []string, newClient ClientFactory) (err err } if !f.Initialized() { - return fmt.Errorf("no function found in current directory.\nYou need to be inside a function directory to invoke it.\n\nTry this:\n func create --language go myfunction Create a new function\n cd myfunction Go into the function directory\n func invoke Now you can invoke it\n\nOr if you have an existing function:\n cd path/to/your/function Go to your function directory\n func invoke Invoke the function") + return NewErrNotInitializedFromPath(f.Root, "invoke") } // If extensions were provided, ensure the format is cloudevent, otherwise return an error. diff --git a/cmd/invoke_test.go b/cmd/invoke_test.go index ce4fbb3f3c..2b21074846 100644 --- a/cmd/invoke_test.go +++ b/cmd/invoke_test.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "os" + "strings" "sync/atomic" "testing" "time" @@ -115,3 +116,25 @@ func TestInvokeExtensionsMapMalformed(t *testing.T) { }) } } + +// TestInvoke_WrapsNotInitialized ensures invoke wraps uninitialized errors +// through the CLI error wrapping layer instead of inline fmt.Errorf. +func TestInvoke_WrapsNotInitialized(t *testing.T) { + _ = FromTempDirectory(t) // empty dir, no function + cmd := NewInvokeCmd(NewTestClient()) + cmd.SetArgs([]string{}) + err := cmd.Execute() + if err == nil { + t.Fatal("expected error when invoking from empty directory") + } + var cliNotInit *ErrNotInitialized + if !errors.As(err, &cliNotInit) { + t.Fatalf("expected ErrNotInitialized, got %T: %v", err, err) + } + if cliNotInit.Cmd != "invoke" { + t.Fatalf("expected Cmd 'invoke', got '%v'", cliNotInit.Cmd) + } + if !strings.Contains(err.Error(), "func invoke") { + t.Fatalf("expected error to contain 'func invoke' guidance, got: %v", err) + } +}