URIError
In JavaScript, URIError is the native error type that the engine throws when a global URI function gets text it can’t encode or decode, such as a stray % passed to decodeURIComponent(). It extends Error, so it carries a message, a name, and a stack trace. A query string that holds a discount code like 50%off is enough to trigger it:
> decodeURIComponent("50%off")
Uncaught URIError: URI malformed
at decodeURIComponent (<anonymous>)
The % must start an escape sequence of two hexadecimal digits, and %of isn’t one, so decoding fails.
When JavaScript Throws URIError
Only four global functions throw URIError, and they throw it at run time:
decodeURI()anddecodeURIComponent(): Input with a%that isn’t followed by two hexadecimal digits, or with escape sequences that don’t form valid UTF-8, such as text from the legacyescape()functionencodeURI()andencodeURIComponent(): A string that holds a lone surrogate, half of a UTF-16 surrogate pair, often left behind when code slices a string in the middle of an emoji
The URL constructor and URLSearchParams don’t throw URIError. An invalid URL makes new URL() throw a TypeError, and URLSearchParams keeps a malformed escape as plain text. Code can also throw URIError on purpose, though that’s rare.
URIError takes the same arguments as the Error constructor, a message and an optional options object with a cause. It works with or without new and returns a new error either way.
Catching URIError
Say a server decodes a search term from a URL that a user typed by hand. This function wraps the call in a try statement, falls back to the raw text when decoding fails, and rethrows any other error:
> function safeDecode(text) {
… try {
… return decodeURIComponent(text);
… } catch (error) {
… if (error instanceof URIError) {
… return text;
… }
… throw error;
… }
… }
undefined
> safeDecode("50%25off")
'50%off'
> safeDecode("50%off")
'50%off'
The instanceof check fails for errors from another realm, such as an iframe or a node:vm context, while error.name and Error.isError() still work.
Common Messages
V8 uses a single message for every URIError, so the cause depends on which function threw it:
| Message (V8) | Usual Cause | Fix |
|---|---|---|
URI malformed |
decodeURIComponent() or decodeURI() on a literal %, as in 50%off |
Encode the text with encodeURIComponent(), which turns % into %25 |
URI malformed |
Decoding bytes that aren’t valid UTF-8, such as caf%E9 from escape() |
Encode with encodeURIComponent() instead of escape() |
URI malformed |
encodeURIComponent() or encodeURI() on a string with a lone surrogate |
Slice strings by code point, or call .toWellFormed() before encoding |
Firefox and Safari word the same failures differently, and Safari uses separate messages for encoding and decoding:
| V8 | Firefox (SpiderMonkey) | Safari (JavaScriptCore) |
|---|---|---|
URI malformed (decoding) |
malformed URI sequence |
URI error |
URI malformed (encoding) |
malformed URI sequence |
String contained an illegal UTF-16 sequence. |
Most of these errors come from decoding text that was never encoded, so the usual fix is to encode it with encodeURIComponent() at the point where it enters the URL.
Related Resources
Course
JavaScript Errors and Debugging for Pythonistas
Port a food bank's donation import to JavaScript, and learn to catch, throw, and tell apart errors, check for the bad data JavaScript doesn't throw on, and find what went wrong.
For additional information on related topics, take a look at the following resources:
- JavaScript Strings for Pythonistas (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026