Skip to content

Commit 76b8bec

Browse files
authored
Improve error message when func describe is run outside function directory (#3027)
* improve error message for func describe and guide users correctly Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> * Improve the description formatting using backticks Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> * Fix the failing test updated describe_test.go to include new description Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> * use 2 layer approach to have concistency in the code Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com> --------- Signed-off-by: RayyanSeliya <rayyanseliya786@gmail.com>
1 parent ae35132 commit 76b8bec

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

cmd/describe.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er
8585
return err
8686
}
8787
if !f.Initialized() {
88-
return errors.New("function not found at this path and no name provided")
88+
return formatError(fn.NewErrNotInitialized(f.Root))
8989
}
9090
details, err = client.Describe(cmd.Context(), "", "", f)
9191
if err != nil {
@@ -97,6 +97,32 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er
9797
return
9898
}
9999

100+
// formatError wraps ErrNotInitialized with user-friendly guidance
101+
func formatError(err error) error {
102+
var errNotInitialized *fn.ErrNotInitialized
103+
if errors.As(err, &errNotInitialized) {
104+
return fmt.Errorf(`%s
105+
106+
No function found in provided path (current directory or via --path).
107+
You need to be in a function directory (or use --path).
108+
109+
Try this:
110+
func create --language go myfunction Create a new function
111+
cd myfunction Go into the function directory
112+
func describe Show function description
113+
114+
Or if you have an existing function:
115+
cd path/to/your/function Go to your function directory
116+
func describe Show function description
117+
118+
Or use --path to describe from anywhere:
119+
func describe --path /path/to/function
120+
121+
For more information try 'func describe --help'`, errNotInitialized.Error())
122+
}
123+
return err
124+
}
125+
100126
// CLI Configuration (parameters)
101127
// ------------------------------
102128

cmd/describe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestDescribe_Default(t *testing.T) {
2424
if err == nil {
2525
t.Fatal("describing a nonexistent function should error")
2626
}
27-
if !strings.Contains(err.Error(), "function not found at this path and no name provided") {
27+
if !strings.Contains(err.Error(), "No function found in provided path") {
2828
t.Fatalf("Unexpected error text returned: %v", err)
2929
}
3030
if describer.DescribeInvoked {

0 commit comments

Comments
 (0)