Skip to content

Commit

Permalink
Merge pull request #52 from dmcgowan/fix-panic-notfound
Browse files Browse the repository at this point in the history
Fix panic when type is not found
  • Loading branch information
AkihiroSuda authored Nov 5, 2024
2 parents 30fd8da 21b066a commit 1666bdb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 6 additions & 8 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 284,14 @@ func getTypeByUrl(url string) (reflect.Type, error) {
mu.RUnlock()
mt, err := protoregistry.GlobalTypes.FindMessageByURL(url)
if err != nil {
e := protoregistry.NotFound
if !errors.Is(err, e) {
return nil, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
}

for _, h := range handlers {
if t := h.GetType(url); t != nil {
return t, nil
if errors.Is(err, protoregistry.NotFound) {
for _, h := range handlers {
if t := h.GetType(url); t != nil {
return t, nil
}
}
}
return nil, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
}
empty := mt.New().Interface()
return reflect.TypeOf(empty).Elem(), nil
Expand Down
11 changes: 11 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 18,7 @@ package typeurl

import (
"bytes"
"errors"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -230,3 231,13 @@ func TestProtoFallback(t *testing.T) {
t.Fatalf("expected % v but got % v", expected, ts.AsTime())
}
}

func TestUnmarshalNotFound(t *testing.T) {
_, err := UnmarshalByTypeURL("doesntexist", []byte("{}"))
if err == nil {
t.Fatalf("expected error unmarshalling type which does not exist")
}
if !errors.Is(err, ErrNotFound) {
t.Fatalf("unexpected error unmarshalling type which does not exist: %v", err)
}
}

0 comments on commit 1666bdb

Please sign in to comment.