URIError: URI malformed
Error message
URI malformed
Quick summary
You passed a malformed URI string to decodeURIComponent() — it contains invalid percent-encoded sequences.
Why this happens
The string contains a % not followed by two valid hex digits
The string has an incomplete percent-encoded sequence
Trying to decode a string that was not properly encoded
Minimal example
const result = decodeURIComponent("%GG");try {
const result = decodeURIComponent("%GG");
} catch (e) {
console.log("Invalid URI");
}%GG is not a valid hex sequence. Wrap in try/catch to handle malformed URIs gracefully.
How to diagnose
Log the string being decoded to inspect it
Check for malformed % sequences (e.g., %GG, %2)
Verify the string was actually URL-encoded before decoding
How to fix
Fix the malformed percent-encoded sequence
Wrap decodeURIComponent in try/catch to handle invalid input
Ensure the string is properly encoded before decoding
How to prevent
Always use encodeURIComponent/decodeURIComponent consistently
Validate URI strings before decoding
Related Resources
Related Lessons
- Data Types
Learn about URI encoding.