golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.72k stars 1.58k forks source link

proto: Export ErrInvalidUTF8 #1642

Open AndreasBergmeier6176 opened 3 weeks ago

AndreasBergmeier6176 commented 3 weeks ago

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.

puellanivis commented 3 weeks ago

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