Skip to content

Commit 90ef12d

Browse files
Fail on schema problems with context
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.
1 parent 013cd60 commit 90ef12d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

gen.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package schematic
33

44
import (
55
"bytes"
6+
"encoding/json"
67
"fmt"
78
"go/format"
9+
"os"
810
"strings"
911
"text/template"
1012

@@ -161,7 +163,7 @@ func (s *Schema) goType(required bool, force bool) (goType string) {
161163
// Resolve JSON reference/pointer
162164
types, err := s.Types()
163165
if err != nil {
164-
panic(err)
166+
fail(s, err)
165167
}
166168
for _, kind := range types {
167169
switch kind {
@@ -216,11 +218,11 @@ func (s *Schema) goType(required bool, force bool) (goType string) {
216218
case "null":
217219
continue
218220
default:
219-
panic(fmt.Sprintf("unknown type %s", kind))
221+
fail(s, fmt.Errorf("unknown type %s", kind))
220222
}
221223
}
222224
if goType == "" {
223-
panic(fmt.Sprintf("type not found : %s", types))
225+
fail(s, fmt.Errorf("type not found : %s", types))
224226
}
225227
// Types allow null
226228
if contains("null", types) || !(required || force) {
@@ -371,3 +373,14 @@ func (l *Link) GoType() (string, bool) {
371373
}
372374
return t, true
373375
}
376+
377+
func fail(v interface{}, err error) {
378+
el, _ := json.MarshalIndent(v, " ", " ")
379+
380+
fmt.Printf(
381+
"Error processing schema element:\n %s\n\nFailed with: %s\n",
382+
el,
383+
err,
384+
)
385+
os.Exit(1)
386+
}

0 commit comments

Comments
 (0)