-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TextEncoder.encode TypeError: Invalid argument should give more details #8201
Comments
Actually we should be more spec compliant here... new TextEncoder().encode({}) Results in the following in Chrome:
It is only values that are not coercible to strings that throw. For example: new TextEncoder().encode(Symbol("a")); Results in:
We should leave the type enforcement up to TypeScript. |
Thanks for the quick reply. Yea that seems reasonable. Definitely agree that the type checking should be left up to typescript. Just to be clear the details about types and such was more of an illustration of a tricky debugging situation where a more verbose error would have been nice, and not meant to put any emphasis on type checking. Since you labeled this as good first issue I thought I'd point readers to the |
Well, I don't think we shouldn't worry about Lines 596 to 609 in 2a36e2f
But that isn't as important as it is that we should try to coerce to a string in deno/op_crates/web/08_text_encoding.js Lines 1063 to 1082 in 2a36e2f
Like something on 1064 that just does |
I'll take a look at this - better TypeError doesn't sound correct but @kitsonk 's fix actually seems to generate the correct behaviour (of coercing to string). In the web:
I think Deno should behave like the WHATWG spec requires - but still use |
I believe that the
TypeError
returned byTextEncoder.encode
is too ambiguous. I think for any other method which isn't dealing with text encoding an ambiguous error message likeInvalid argument
would be okay. However text encoding is full of many hairy input situations, and more details to narrow down which bad input situation you are experiencing would be very helpful.Here is a short story highlighting where just a little more detail in the error message could have helped a lot:
I had an error where some code before a call to
encode()
was accidentally returning objects instead of strings. It was hard for me to find because I even had Typescript annotations on the argument I was passing toencode()
asserting that the argument should be a string, like so:However type checks were passed and the error occurred.
This small self contained example effectively reproduces passing an object to
encode()
:The resulting error is something like:
Due to the ambiguaty of the error, and my confidence is what turned out to be false assumptions about the type of an argument, I went down a long rabbit hole trying to figure out if my program was producing any invalid unicode glyphs or something like that. I think it would be helpful for
encode()
to throw an error more likeTypeError: Invalid argument, must be a string
.The text was updated successfully, but these errors were encountered: