Skip to content

Commit

Permalink
Type URLs can have any host and even include URI scheme (#636)
Browse files Browse the repository at this point in the history
This corrects an issue where it was previously possible that the
`ErrorDetail.Type()` method would return a URL instead of the
type name.
  • Loading branch information
jhump authored Nov 15, 2023
1 parent ab889c2 commit 2542e8a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
8 changes: 6 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (d *ErrorDetail) Type() string {
// than plain type names, but there aren't any descriptor registries
// deployed. With the current state of the `Any` code, it's not possible to
// build a useful type registry either. To hide this from users, we should
// trim the static hostname that `Any` adds to the type name.
// trim the URL prefix is added to the type name.
//
// If we ever want to support remote registries, we can add an explicit
// `TypeURL` method.
return strings.TrimPrefix(d.pb.GetTypeUrl(), defaultAnyResolverPrefix)
return typeNameFromURL(d.pb.GetTypeUrl())
}

// Bytes returns a copy of the Protobuf-serialized detail.
Expand Down Expand Up @@ -409,3 +409,7 @@ func asMaxBytesError(err error, tmpl string, args ...any) *Error {
prefix := fmt.Sprintf(tmpl, args...)
return errorf(CodeResourceExhausted, "%s: exceeded %d byte http.MaxBytesReader limit", prefix, maxBytesErr.Limit)
}

func typeNameFromURL(url string) string {
return url[strings.LastIndexByte(url, '/')+1:]
}
42 changes: 42 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,45 @@ func TestErrorIs(t *testing.T) {
assert.False(t, errors.Is(connectErr, NewError(CodeUnavailable, err)))
assert.True(t, errors.Is(connectErr, connectErr))
}

func TestTypeNameFromURL(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
url string
typeName string
}{
{
name: "no-prefix",
url: "foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "standard-prefix",
url: defaultAnyResolverPrefix + "foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "different-hostname",
url: "abc.com/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "additional-path-elements",
url: defaultAnyResolverPrefix + "abc/def/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
{
name: "full-url",
url: "https://abc.com/abc/def/foo.bar.Baz",
typeName: "foo.bar.Baz",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, typeNameFromURL(testCase.url), testCase.typeName)
})
}
}
2 changes: 1 addition & 1 deletion protocol_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ func (d *connectWireDetail) MarshalJSON() ([]byte, error) {
Value string `json:"value"`
Debug json.RawMessage `json:"debug,omitempty"`
}{
Type: strings.TrimPrefix(d.pb.GetTypeUrl(), defaultAnyResolverPrefix),
Type: typeNameFromURL(d.pb.GetTypeUrl()),
Value: base64.RawStdEncoding.EncodeToString(d.pb.GetValue()),
}
// Try to produce debug info, but expect failure when we don't have
Expand Down

0 comments on commit 2542e8a

Please sign in to comment.