protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

proto: Export ErrInvalidUTF8

Open AndreasBergmeier6176 opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. In Golang UTF8 strings are not enforced. Thus, very often some kind of byte sequence is passed through all layers of an application until it finally fails because protobuf eventually checks for UTF-8. This library then returns errInvalidUTF8.

There seem to be 2 solutions to this problem:

  1. Checking for UTF-8 on upper layers before passing string down. This would degrade the happy-path unnecessarily and as a consequence is rarely done.

  2. React upon the error. Currently, this requires matching the error string. Which is neither idiomatic nor is there any test in this repo to ensure that the string stays the same.

Describe the solution you'd like To enable errors.Is(err, encoding.ErrInvalidUTF8) I would propose to simply export the error type as well as an error instance while also introducing tests to ensure that said Is works. Exporting might look like this: var ErrInvalidUTF8 = impl.ErrInvalidUTF8{}.

Describe alternatives you've considered There would be a way to introduce a UTF8String - but this would make the library more non-idiomatic.

AndreasBergmeier6176 avatar Aug 20 '24 06:08 AndreasBergmeier6176

Well, this solution would be more idiomatic than the PR linking to this issue…

puellanivis avatar Aug 20 '24 07:08 puellanivis

I proposed golang/go#70547 which would address this in a more ecosystem-wide manner since invalid UTF-8 is not unique to protobuf, but many formats and protocols.

dsnet avatar Dec 05 '24 19:12 dsnet