From f4dceaa83797c23b35da310faffdfd22f4354a4c Mon Sep 17 00:00:00 2001 From: Mars Hall Date: Tue, 22 Feb 2022 17:57:15 -0800 Subject: [PATCH] Give error messages context, and output the generated code to align with error line numbers --- cmd/schematic/schematic.go | 4 ++-- gen.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/schematic/schematic.go b/cmd/schematic/schematic.go index eb411e4..c880bce 100644 --- a/cmd/schematic/schematic.go +++ b/cmd/schematic/schematic.go @@ -65,13 +65,13 @@ func main() { var s schematic.Schema d := json.NewDecoder(i) if err := d.Decode(&s); err != nil { - log.Fatal(err) + log.Fatal(fmt.Errorf("Error decoding schema JSON: %w", err)) } code, err := s.Generate() if err != nil { fmt.Fprintf(os.Stderr, "%s\n", code) - log.Fatal(err) + log.Fatal(fmt.Errorf("Error generating Go code (output above): %w", err)) } fmt.Fprintln(o, string(code)) diff --git a/gen.go b/gen.go index d6824d5..ddc8696 100644 --- a/gen.go +++ b/gen.go @@ -75,7 +75,7 @@ func (s *Schema) Generate() ([]byte, error) { } if !context.Definition.AreTitleLinksUnique() { - return nil, fmt.Errorf("duplicate titles detected for %s", context.Name) + return nil, fmt.Errorf("Duplicate %s.links.title detected in the schema. Links must have distinct titles.", context.Name) } templates.ExecuteTemplate(&buf, "struct.tmpl", context) @@ -88,7 +88,7 @@ func (s *Schema) Generate() ([]byte, error) { // Format sources clean, err := format.Source(bytes) if err != nil { - return buf.Bytes(), err + return bytes, fmt.Errorf("Error formatting Go source: %w", err) } return clean, nil }