Skip to content

Commit 3901142

Browse files
committed
use 2 layer approach to have concistency in the code
Signed-off-by: RayyanSeliya <[email protected]>
1 parent 6f7a6e6 commit 3901142

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

cmd/describe.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"encoding/xml"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -84,17 +85,7 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er
8485
return err
8586
}
8687
if !f.Initialized() {
87-
return fmt.Errorf(`no function found in current directory.
88-
You need to be inside a function directory to get the function description.
89-
90-
Try this:
91-
func create --language go myfunction Create a new function
92-
cd myfunction Go into the function directory
93-
func describe Show function description
94-
95-
Or if you have an existing function:
96-
cd path/to/your/function Go to your function directory
97-
func describe Show function description`)
88+
return formatError(fn.NewErrNotInitialized(f.Root))
9889
}
9990
details, err = client.Describe(cmd.Context(), "", "", f)
10091
if err != nil {
@@ -106,6 +97,32 @@ Or if you have an existing function:
10697
return
10798
}
10899

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+
109126
// CLI Configuration (parameters)
110127
// ------------------------------
111128

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(), "no function found in current directory") {
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)