URIError: URI malformed
错误信息
URI malformed
快速摘要
你通过 malformed URI 字符串到 decodeURIComponent() — 它包含无效百分比-encoded 序列.
为什么发生
字符串包含 % 不 followed 通过 two 有效 hex digits
字符串有 不完整百分比-encoded 序列
Trying 到 decode 字符串那个是 不 properly encoded
最小示例
✗ 错误代码
const result = decodeURIComponent("%GG");✓ 修复代码
try {
const result = decodeURIComponent("%GG");
} catch (e) {
console.log("Invalid URI");
}%GG 是不 有效 hex 序列. Wrap in try/catch 到 handle malformed URIs gracefully.
如何诊断
Log 字符串是 decoded 到 inspect 它
检查 for malformed % 序列 (e.g., %GG, %2)
验证字符串是 actually URL-encoded 之前 decoding
如何修复
Fix malformed 百分比-encoded 序列
Wrap decodeURIComponent in try/catch 到 handle 无效输入
Ensure 字符串是 properly encoded 之前 decoding
如何预防
总是 use encodeURIComponent/decodeURIComponent consistently
验证 URI 字符串之前 decoding