Skip to content

proposal: utf8: add ErrInvalid #70547

@dsnet

Description

@dsnet

Proposal Details

I propose the addition of the following sentinel error to the utf8 package:

// ErrInvalid indicates that invalid UTF-8 was encountered within a text string.
// Packages that return this error should usually wrap this error value within
// some local error type to provide further semantic context regarding where
// this error occurred.
var ErrInvalid = errors.New("invalid UTF-8")

Many higher-level formats are built on top of UTF-8 (e.g., XML, JSON, protobuf, etc.) where encountering an UTF-8 encoding problem is a possibility. In many cases such an error is not fatal and processing can still continue such that a function that returns a typical (T, error) result may provide a sensible while also returning an error that matches utf8.ErrInvalid. Even if it is fatal, it is often useful for metrics reporting purposes to specially identify invalid UTF-8 as a particular class of errors.

This could replace internal error values used by the "encoding/json/v2" prototype and also within the protobuf module (e.g., golang/protobuf#1228).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions