Skip to content

Commit ba67ce5

Browse files
fixing error message outputs
1 parent b1c8bac commit ba67ce5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

cmd/validateargs.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ func handleHelpFlag(args []string) {
1818

1919
// ValidatePokemonArgs validates the command line arguments
2020
func ValidatePokemonArgs(args []string) error {
21-
2221
handleHelpFlag(args)
2322

23+
// Check if the number of arguments is less than 3
2424
if len(args) < 3 {
2525
errMessage := errorBorder.Render(
2626
errorColor.Render("Error!"),
@@ -31,23 +31,53 @@ func ValidatePokemonArgs(args []string) error {
3131
return fmt.Errorf("%s", errMessage)
3232
}
3333

34+
// Check if there are too many arguments
3435
if len(args) > 6 {
35-
errMessage := errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments")
36+
errMessage := errorBorder.Render(
37+
errorColor.Render("Error!"),
38+
"\nToo many arguments",
39+
)
3640
return fmt.Errorf("%s", errMessage)
3741
}
3842

43+
// Validate each argument after the Pokémon's name
3944
if len(args) > 3 {
4045
for _, arg := range args[3:] {
46+
// Check for single `-` or `--` which are invalid
47+
if arg == "-" || arg == "--" {
48+
errorTitle := errorColor.Render("Error!")
49+
errorString := fmt.Sprintf(
50+
"\nInvalid argument '%s'. Single '-' or '--' is not allowed.\nPlease use valid flags.",
51+
arg,
52+
)
53+
finalErrorMessage := errorTitle + errorString
54+
renderedError := errorBorder.Render(finalErrorMessage)
55+
return fmt.Errorf("%s", renderedError)
56+
}
57+
58+
// Check if the argument starts with a flag prefix but is invalid
4159
if arg[0] != '-' {
4260
errorTitle := errorColor.Render("Error!")
43-
errorString := fmt.Sprintf("\nInvalid argument '%s'. Only flags are allowed after declaring a Pokémon's name", arg)
61+
errorString := fmt.Sprintf(
62+
"\nInvalid argument '%s'.\nOnly flags are allowed after declaring a Pokémon's name",
63+
arg,
64+
)
4465
finalErrorMessage := errorTitle + errorString
4566
renderedError := errorBorder.Render(finalErrorMessage)
4667
return fmt.Errorf("%s", renderedError)
4768
}
4869
}
4970
}
5071

72+
// Add a check for invalid Pokémon names (e.g., names starting with `-`)
73+
if len(args[2]) > 0 && args[2][0] == '-' {
74+
errMessage := errorBorder.Render(
75+
errorColor.Render("Error!"),
76+
"\nPokémon not found. Perhaps a typo in the name?",
77+
)
78+
return fmt.Errorf("%s", errMessage)
79+
}
80+
5181
return nil
5282
}
5383

connections/connection.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"os"
1313
)
1414

15+
var errorBorder = lipgloss.NewStyle().
16+
BorderStyle(lipgloss.RoundedBorder()).
17+
BorderForeground(lipgloss.Color("#F2055C"))
18+
1519
type PokemonJSONStruct struct {
1620
Name string `json:"name"`
1721
ID int `json:"id"`
@@ -108,7 +112,11 @@ func ApiCallSetup(rawURL string, target interface{}, skipHTTPSCheck bool) error
108112
defer res.Body.Close()
109113

110114
if res.StatusCode == http.StatusNotFound {
111-
fmt.Println(errorColor.Render("Pokémon not found. Perhaps a typo in the name?"))
115+
errMessage := errorBorder.Render(
116+
errorColor.Render("Error!"),
117+
"\nPokémon not found. Perhaps a typo in the name?",
118+
)
119+
fmt.Println(errMessage)
112120

113121
if flag.Lookup("test.v") != nil {
114122
return fmt.Errorf("page not found: 404 error")

0 commit comments

Comments
 (0)