From b6b4eda80b9d32e468b520e3045b0cf4a66716b8 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 17 May 2023 13:21:47 -0400 Subject: [PATCH] Adding proper error handling. --- cmd/obp/cmd/validate.go | 4 ++-- validate.go | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index eec470f..3ca0cf8 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -31,10 +31,10 @@ var validateCmd = &cobra.Command{ } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { schema := cmd.Flag("schema").Value.String() target := cmd.Flag("target").Value.String() - obp.Validate(schema, target) + return obp.Validate(schema, target) }, } diff --git a/validate.go b/validate.go index a49caf0..4f51e28 100644 --- a/validate.go +++ b/validate.go @@ -2,7 +2,6 @@ package obp import ( "fmt" - "log" "os" "github.com/santhosh-tekuri/jsonschema/v5" @@ -15,23 +14,22 @@ func Validate(schemaURL, filename string) error { target, err := os.Open(filename) if err != nil { - log.Fatalf("could not open target file: %s\n", err) + return fmt.Errorf("could not open target file: %w", err) } dec := yaml.NewDecoder(target) err = dec.Decode(&m) if err != nil { - log.Fatalf("error decoding YAML: %s\n", err) + return fmt.Errorf("error decoding YAML: %w", err) } compiler := jsonschema.NewCompiler() schema, err := compiler.Compile(schemaURL) if err != nil { - log.Fatalf("error compiling schema: %s\n", err) + return fmt.Errorf("error compiling schema: %w", err) } if err := schema.Validate(m); err != nil { - log.Fatalf("error validating: %#v\n", err) + return fmt.Errorf("error validating target %q: %w", filename, err) } - fmt.Println("validation successfull") return nil }