Skip to content

Commit

Permalink
lexgen error handling tweaks (#476)
Browse files Browse the repository at this point in the history
Working on a separate patch to explicitly handle `ErrUnrecognizedType`
in automod firehose consumer, and reviewed this file.
  • Loading branch information
bnewbold authored Dec 15, 2023
2 parents 5a20c86 + e7061ca commit d8b9f66
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lex/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ func (ts *TypeSchema) writeTypeDefinition(name string, w io.Writer) error {
pf("}\n\n")
}
default:
return fmt.Errorf("%s has unrecognized type type %s", name, ts.Type)
return fmt.Errorf("%s has unrecognized type: %s", name, ts.Type)
}

return nil
Expand Down Expand Up @@ -1344,7 +1344,7 @@ func (ts *TypeSchema) writeTypeMethods(name string, w io.Writer) error {

return fmt.Errorf("%q unsupported for marshaling", name)
default:
return fmt.Errorf("%q has unrecognized type type %s", name, ts.Type)
return fmt.Errorf("%q has unrecognized type: %s", name, ts.Type)
}
}

Expand Down
8 changes: 4 additions & 4 deletions lex/util/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func RegisterType(id string, val cbg.CBORMarshaler) {
func NewFromType(typ string) (interface{}, error) {
t, ok := lexTypesMap[typ]
if !ok {
return nil, fmt.Errorf("unknown type: %q", typ)
return nil, fmt.Errorf("%w: %q", ErrUnrecognizedType, typ)
}
v := reflect.New(t)
return v.Interface(), nil
Expand All @@ -48,7 +48,7 @@ func JsonDecodeValue(b []byte) (any, error) {

t, ok := lexTypesMap[tstr]
if !ok {
return nil, fmt.Errorf("unrecognized type: %q", tstr)
return nil, fmt.Errorf("%w: %q", ErrUnrecognizedType, tstr)
}

val := reflect.New(t)
Expand All @@ -66,7 +66,7 @@ type CBOR interface {
cbg.CBORMarshaler
}

var ErrUnrecognizedType = fmt.Errorf("unrecognized type")
var ErrUnrecognizedType = fmt.Errorf("unrecognized lexicon type")

func CborDecodeValue(b []byte) (CBOR, error) {
tstr, err := CborTypeExtract(b)
Expand All @@ -76,7 +76,7 @@ func CborDecodeValue(b []byte) (CBOR, error) {

t, ok := lexTypesMap[tstr]
if !ok {
return nil, fmt.Errorf("handling %q: %w", tstr, ErrUnrecognizedType)
return nil, fmt.Errorf("%w: %q", ErrUnrecognizedType, tstr)
}

val := reflect.New(t)
Expand Down

0 comments on commit d8b9f66

Please sign in to comment.