{"record":{"id":"5268e1bb3e806b56","repo":"pot-app/pot-desktop","slug":"get-file-id-error-json-stringify-result","errorCode":null,"errorMessage":"Get file_id Error: ${JSON.stringify(result)}","messagePattern":"Get file_id Error: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/window/Config/pages/Backup/utils/aliyun.jsx","lineNumber":265,"sourceCode":"            Authorization: `Bearer ${token}`,\n        },\n        body: Body.json({\n            drive_id,\n            parent_file_id: dir_id,\n            name: name,\n            type: 'file',\n            check_name_mode: 'refuse',\n        }),\n    });\n    if (res.ok) {\n        const result = res.data;\n        if (result['file_id']) {\n            const file_id = result['file_id'];\n            const upload_id = result['upload_id'];\n            const upload_url = result['part_info_list'][0]['upload_url'];\n            return { file_id, upload_id, upload_url };\n        } else {\n            throw new Error(`Get file_id Error: ${JSON.stringify(result)}`);\n        }\n    } else {\n        const result = res.data;\n        if (result['message']) {\n            throw new Error(result['message']);\n        } else {\n            throw new Error(`Get accessToken Error: ${JSON.stringify(result)}`);\n        }\n    }\n}\n\nasync function getFileByPath(token, drive_id, name) {\n    const res = await fetch('https://openapi.alipan.com/adrive/v1.0/openFile/get_by_path', {\n        method: 'POST',\n        headers: {\n            Authorization: `Bearer ${token}`,\n        },\n        body: Body.json({","sourceCodeStart":247,"sourceCodeEnd":283,"githubUrl":"https://github.com/pot-app/pot-desktop/blob/594d32ede96acd106b0256deaa8bb440ffcdff40/src/window/Config/pages/Backup/utils/aliyun.jsx#L247-L283","documentation":"createFile posts to openFile/create to reserve a file and expects the response to contain file_id, upload_id and part_info_list[0].upload_url. When res.ok is true but result['file_id'] is missing, it throws this error with the raw JSON. This means the API returned a non-error response that did not match the expected create-response schema (e.g. an exist:true dedup response without part_info_list, or a partial body).","triggerScenarios":"The openFile/create call returns 200 but without file_id — notably when the file already exists (response contains exist:true and file_id under a different field or rapid_upload succeeded but code assumes new file); response body schema drift; check_name_mode/parent_file_id errors surfaced without a proper file_id.","commonSituations":"Uploading a file that already exists in the target dir so the API returns an 'exist' payload; API version change altering the create response; passing name with illegal characters so the API returns a body lacking file_id.","solutions":["Log JSON.stringify(result) to see the actual body; check for exist/rapid_upload fields and handle the already-exists case","Handle result['exist'] === true by fetching the existing file's file_id (getFileByPath) instead of treating it as an error","Ensure part_info_list handling is guarded: access upload_url only after confirming file_id exists","Pin/verify the API docs version for openFile/create; update response parsing to the current schema"],"exampleFix":"// before\nif (result['file_id']) { ... } else {\n    throw new Error(`Get file_id Error: ${JSON.stringify(result)}`);\n}\n// after\nif (result['exist'] && result['file_id']) {\n    return { file_id: result['file_id'], upload_id: null, upload_url: null, exist: true };\n}\nif (result['file_id']) { ... }\nthrow new Error(`createFile: unexpected response ${JSON.stringify(result)}`);","handlingStrategy":"type-guard","validationCode":"// before relying on createFile's result\nconst created = await createFile(token, drive_id, dir_id, name); // may throw\nif (!created || typeof created.file_id !== 'string' || !created.upload_url) {\n    throw new Error(`createFile returned incomplete upload info: ${JSON.stringify(created)}`);\n}","typeGuard":"function isCreateFileResult(r) {\n    return r != null && typeof r === 'object'\n        && typeof r.file_id === 'string' && r.file_id.length > 0\n        && (r.upload_url == null || typeof r.upload_url === 'string');\n}","tryCatchPattern":"try {\n    const { file_id, upload_id, upload_url } = await createFile(token, drive_id, dir_id, name);\n} catch (e) {\n    const m = String(e.message);\n    if (m.startsWith('Get file_id Error')) {\n        // inspect embedded JSON for exist/duplicate handling\n        const body = safeParse(m.slice('Get file_id Error: '.length));\n        if (body && body.exist && body.file_id) return { file_id: body.file_id, exist: true };\n    }\n    throw e;\n}","preventionTips":["Handle the already-exists / rapid-upload response shape explicitly instead of assuming a fresh-create body","Keep API response parsing in sync with the current alipan openAPI docs for openFile/create","Guard part_info_list[0] access — empty part_info_list on existing files will throw separately","Log the raw body before throwing so schema drift is immediately visible"],"tags":["aliyun","alipan","api-error","upload","schema"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"594d32ede96acd106b0256deaa8bb440ffcdff40","analyzedAt":"2026-09-02T18:12:21.811Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}