Closed stevenatkin closed 7 years ago
We depend on the UTF-8 decode operation defined in the Encoding spec. The default error mode for decoders is defined to be replacement
instead of fatal
, which means we shouldn't throw. Replacement characters will be inserted instead.
Is my reading correct, @annevk? That matches at least Chrome's implementation.
Would we want to explicitly override the error mode? I'm inclined not to, since our closest sibling method on Body
doesn't either.
Yeah, we generally don't do that in the platform. Are the raw bytes exposed somewhere? If they are you can already get this kind of error handling if you want by using a TextDecoder
object.
Yes, they're exposed. The following would work:
self.addEventListener('push', event => {
const decoder = new TextDecoder('utf-8', {
fatal: true
});
try {
decoder.decode(event.data.arrayBuffer());
} catch (e) {
/* do something with |e| */
}
});
Let me close this based on prior art and the availability of a way to get the exceptions if necessary. Happy to reconsider if you disagree!
https://www.w3.org/TR/push-api/#pushmessagedata-interface
You should add support for an exception to the text() method. The text() method converts the bytes to UTF-8 using UTF-8 decode. If the UTF-8 byte sequence is malformed, then the text() method should throw an exception.