Skip to content

Commit

Permalink
fix: consistent error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Oct 22, 2023
1 parent 606e9d7 commit 7902577
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 6 additions & 6 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func boolTag(f reflect.StructField, tag string) bool {
} else if v == "false" {
return false
} else {
panic("invalid bool tag '" + tag + "' for field '" + f.Name + "': " + v)
panic(fmt.Errorf("invalid bool tag '%s' for field '%s': %v", tag, f.Name, v))
}
}
return false
Expand All @@ -209,7 +209,7 @@ func intTag(f reflect.StructField, tag string) *int {
if i, err := strconv.Atoi(v); err == nil {
return &i
} else {
panic("invalid int tag '" + tag + "' for field '" + f.Name + "': " + v + " (" + err.Error() + ")")
panic(fmt.Errorf("invalid int tag '%s' for field '%s': %v (%w)", tag, f.Name, v, err))
}
}
return nil
Expand All @@ -220,7 +220,7 @@ func floatTag(f reflect.StructField, tag string) *float64 {
if i, err := strconv.ParseFloat(v, 64); err == nil {
return &i
} else {
panic("invalid float tag '" + tag + "' for field '" + f.Name + "': " + v + " (" + err.Error() + ")")
panic(fmt.Errorf("invalid float tag '%s' for field '%s': %v (%w)", tag, f.Name, v, err))
}
}
return nil
Expand All @@ -243,7 +243,7 @@ func jsonTagValue(f reflect.StructField, t reflect.Type, value string) any {

var v any
if err := json.Unmarshal([]byte(value), &v); err != nil {
panic("invalid tag for field '" + f.Name + "': " + err.Error())
panic(fmt.Errorf("invalid tag for field '%s': %w", f.Name, err))
}

vv := reflect.ValueOf(v)
Expand All @@ -256,14 +256,14 @@ func jsonTagValue(f reflect.StructField, t reflect.Type, value string) any {
tmp := reflect.MakeSlice(t, 0, vv.Len())
for i := 0; i < vv.Len(); i++ {
if !vv.Index(i).Elem().Type().ConvertibleTo(t.Elem()) {
panic(fmt.Errorf("unable to convert %v to %v: %w", vv.Index(i).Interface(), t.Elem(), ErrSchemaInvalid))
panic(fmt.Errorf("unable to convert %v to %v for field '%s': %w", vv.Index(i).Interface(), t.Elem(), f.Name, ErrSchemaInvalid))

Check warning on line 259 in schema.go

View check run for this annotation

Codecov / codecov/patch

schema.go#L259

Added line #L259 was not covered by tests
}

tmp = reflect.Append(tmp, vv.Index(i).Elem().Convert(t.Elem()))
}
v = tmp.Interface()
} else if !tv.ConvertibleTo(deref(t)) {
panic(fmt.Errorf("unable to convert %v to %v: %w", tv, t, ErrSchemaInvalid))
panic(fmt.Errorf("unable to convert %v to %v for field '%s': %w", tv, t, f.Name, ErrSchemaInvalid))
}

converted := reflect.ValueOf(v).Convert(deref(t))
Expand Down
11 changes: 9 additions & 2 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func TestSchema(t *testing.T) {
}`,
},
{
name: "fiel-pointer-example",
name: "field-pointer-example",
input: struct {
Int *int64 `json:"int" example:"123"`
Str *string `json:"str" example:"foo"`
Expand Down Expand Up @@ -420,14 +420,21 @@ func TestSchema(t *testing.T) {
}{},
panics: `invalid tag for field 'Value': invalid character 'b' looking for beginning of value`,
},
{
name: "panic-json-type",
input: struct {
Value int `json:"value" example:"true"`
}{},
panics: `unable to convert bool to int for field 'Value': schema is invalid`,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
r := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)

if c.panics != "" {
assert.PanicsWithValue(t, c.panics, func() {
assert.PanicsWithError(t, c.panics, func() {
r.Schema(reflect.TypeOf(c.input), false, "")
})
} else {
Expand Down

0 comments on commit 7902577

Please sign in to comment.