{"record":{"id":"ea7164f9abed320c","repo":"maotoumao/MusicFree","slug":"atob-failed-the-string-to-be-decoded-is-not-cor","errorCode":null,"errorMessage":"'atob' failed: The string to be decoded is not correctly encoded.","messagePattern":"'atob' failed: The string to be decoded is not correctly encoded\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/base64.ts","lineNumber":37,"sourceCode":"\n            if (charCode > 0xff) {\n                throw new Error(\n                    \"'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.\",\n                );\n            }\n\n            block = (block << 8) | charCode;\n        }\n\n        return output;\n    },\n\n    atob: (input: string = \"\") => {\n        let str = input.replace(/[=]+$/, \"\");\n        let output = \"\";\n\n        if (str.length % 4 == 1) {\n            throw new Error(\n                \"'atob' failed: The string to be decoded is not correctly encoded.\",\n            );\n        }\n        for (\n            let bc = 0, bs = 0, buffer, i = 0;\n            (buffer = str.charAt(i++));\n            ~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4)\n                ? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))\n                : 0\n        ) {\n            buffer = chars.indexOf(buffer);\n        }\n\n        return output;\n    },\n};\n\nexport default Base64;","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/maotoumao/MusicFree/blob/d118b18b3d0c904400f7eea7bf99c0ceec6c1aee/src/utils/base64.ts#L19-L55","documentation":"The pure-JS atob polyfill in src/utils/base64.ts throws this when the input, after stripping trailing '=' padding, has length % 4 === 1 — an impossible base64 length indicating corrupted or truncated input. It guards the decoder from garbage strings.","triggerScenarios":"Calling atob() with a string that is not valid base64: truncated clipboard/import payload, URL-safe base64 (contains '-'/'_') passed to the standard decoder, extra whitespace/newlines shifting length, or decoding a value that was never base64 at all.","commonSituations":"Restoring a backup from a URL where the base64 was cut off or query-param-mangled (+ turned into space); sharing encoded plugin data across apps that url-encode it; hand-edited base64 strings.","solutions":["Validate/repair the input before decoding: strip whitespace, ensure length % 4 !== 1, re-add padding.","If the source uses URL-safe base64, convert '-'→'+', '_'→'/' before atob.","Re-export/copy the original encoded string (it was truncated in transit, so regenerate it).","Wrap atob in try/catch and surface a user-friendly 'data is corrupted' message."],"exampleFix":"// before\nconst decoded = atob(input);\n// after\nconst normalized = input.replace(/\\s+/g, '').replace(/-/g, '+').replace(/_/g, '/');\nconst padded = normalized + '='.repeat((4 - (normalized.length % 4)) % 4);\nconst decoded = atob(padded);","handlingStrategy":"validation","validationCode":"function isProbablyBase64(s) {\n  const str = s.replace(/\\s+/g, '').replace(/[=-]+$/, '');\n  return /^[A-Za-z0-9+/]*$/.test(str) && str.length % 4 !== 1;\n}\nif (!isProbablyBase64(input)) throw new Error('not valid base64');","typeGuard":null,"tryCatchPattern":"try {\n  return atob(input);\n} catch (e) {\n  if (String(e).includes('not correctly encoded')) {\n    throw new Error('备份内容已损坏，请重新导出'); // surface friendly message\n  }\n  throw e;\n}","preventionTips":["Strip whitespace and normalize URL-safe characters (-, _) before decoding.","Validate length % 4 !== 1 and character set before calling atob.","Never hand-edit base64 payloads; re-export instead.","When passing base64 in URLs, use encodeURIComponent to avoid + being read as space."],"tags":["base64","decoding","data-corruption","polyfill"],"backgroundTag":"atob-invalid-base64","analyzedSha":"d118b18b3d0c904400f7eea7bf99c0ceec6c1aee","analyzedAt":"2026-08-30T11:49:12.932Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}