Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
}

Expand Down
26 changes: 26 additions & 0 deletions cmd/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
4 changes: 2 additions & 2 deletions cmd/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
}

Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions cmd/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"strings"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
}
Loading