Skip to content

Commit

Permalink
Fail on schema problems with context
Browse files Browse the repository at this point in the history
There are currently a few places where an error causes schematic to exit
with an error with no context to resolve the problem:

    schematic: unknown type <nil>

This updates the places where we exit on schema problems to also print
the schema that was being processed.
  • Loading branch information
bernerdschaefer committed Aug 29, 2018
1 parent 013cd60 commit 90ef12d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package schematic

import (
"bytes"
"encoding/json"
"fmt"
"go/format"
"os"
"strings"
"text/template"

Expand Down Expand Up @@ -161,7 +163,7 @@ func (s *Schema) goType(required bool, force bool) (goType string) {
// Resolve JSON reference/pointer
types, err := s.Types()
if err != nil {
panic(err)
fail(s, err)
}
for _, kind := range types {
switch kind {
Expand Down Expand Up @@ -216,11 +218,11 @@ func (s *Schema) goType(required bool, force bool) (goType string) {
case "null":
continue
default:
panic(fmt.Sprintf("unknown type %s", kind))
fail(s, fmt.Errorf("unknown type %s", kind))
}
}
if goType == "" {
panic(fmt.Sprintf("type not found : %s", types))
fail(s, fmt.Errorf("type not found : %s", types))
}
// Types allow null
if contains("null", types) || !(required || force) {
Expand Down Expand Up @@ -371,3 +373,14 @@ func (l *Link) GoType() (string, bool) {
}
return t, true
}

func fail(v interface{}, err error) {
el, _ := json.MarshalIndent(v, " ", " ")

fmt.Printf(
"Error processing schema element:\n %s\n\nFailed with: %s\n",
el,
err,
)
os.Exit(1)
}

0 comments on commit 90ef12d

Please sign in to comment.