CKTIS / JavaScript
URIError — URI encoding or decoding failure
URIError is a built-in JavaScript error type indicating malformed data during URI encoding or decoding. Learn its typical causes, how to construct it, and how to handle it selectively.
Version: Introduced in ECMAScript 3 (1999). URIError is an ECMAScript built-in, not a Web API.
Syntax
new URIError(message)
URIError(message)URIError is a standard ECMAScript error type that inherits from Error, not a Web API interface. It does not mean any invalid address: it concerns failures in URI encoding and decoding operations.
decodeURI and decodeURIComponent throw URIError for malformed percent escapes, such as %ZZ, or byte sequences that do not encode valid UTF-8. encodeURI and encodeURIComponent throw it for an unpaired UTF-16 surrogate. The Component variants handle individual URI components; the other variants account for the role of reserved characters in a complete URI.
new URIError(message) creates an object; creating it alone does not throw an exception. Calling URIError(message) without new also creates an object. To throw your own error, use throw new URIError(message). The name property defaults to "URIError"; automatically generated message text depends on the implementation.
Wrap decoding of untrusted data in try...catch. In typical code running within one realm, check error instanceof URIError and rethrow other exceptions. This check can fail for an error from another realm, such as another frame. The example decodes valid text and handles an incomplete UTF-8 sequence without relying on message text.
Examples
for (const input of ["hello%20world", "%E0%A4"]) {
try {
console.log(decodeURIComponent(input));
} catch (error) {
if (!(error instanceof URIError)) throw error;
console.log(error.name);
}
}Result
hello world URIError
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.