From aa0964a12eef0c996612f9750c68ae45ed6054ea Mon Sep 17 00:00:00 2001 From: Alexandre GUIOT--VALENTIN Date: Sun, 17 Apr 2022 17:49:54 +0200 Subject: [PATCH] Better handling of input file errors (#187) * Handle error when no input is provided * Improve error when input file can't be read --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a067a86..c9a854a 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "golang.org/x/term" "os" "path" + "io/fs" "runtime/pprof" "strings" ) @@ -50,7 +51,13 @@ func main() { filePath = os.Args[1] f, err := os.Open(os.Args[1]) if err != nil { - panic(err) + switch err.(type) { + case *fs.PathError: + fmt.Println(err) + os.Exit(1) + default: + panic(err) + } } dec = json.NewDecoder(f) args = os.Args[2:] @@ -59,6 +66,10 @@ func main() { dec = json.NewDecoder(os.Stdin) args = os.Args[1:] } + if dec == nil { + fmt.Println("No input provided. Usage: `fx data.json` or `curl ... | fx`") + os.Exit(1) + } dec.UseNumber() jsonObject, err := parse(dec) if err != nil {