{"record":{"id":"47e6f5784db39047","repo":"yikart/AiToEarn","slug":"filepath","errorCode":null,"errorMessage":"读取文件失败: filePath","messagePattern":"读取文件失败: filePath","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"project/aitoearn-electron/electron/plat/utils/index.ts","lineNumber":28,"sourceCode":"export async function getFileContent(filePath: string): Promise<Buffer> {\n  try {\n    if (filePath.includes('https://') || filePath.includes('http://')) {\n      const res = await requestNet({\n        url: filePath,\n        isReqFile: true,\n      });\n      return res.data;\n    } else {\n      // 确保路径是绝对路径\n      const absolutePath = path.resolve(filePath);\n      console.log('Reading file:', absolutePath);\n\n      // 读取文件内容\n      return await fs.promises.readFile(absolutePath);\n    }\n  } catch (error) {\n    console.error('Failed to read file:', error);\n    throw new Error(`读取文件失败: filePath`);\n  }\n}\n\n/**\n * 获取图片的基本信息\n * @param filePath\n */\nexport async function getImageBaseInfo(filePath: string) {\n  const buffer = await getFileContent(filePath);\n  const metadata = await sharp(buffer).metadata();\n\n  return {\n    width: metadata.width,\n    height: metadata.height,\n  };\n}\n\nexport const CookieToString = (cookies: Electron.Cookie[]) => {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-electron/electron/plat/utils/index.ts#L10-L46","documentation":"getFileContent in electron/plat/utils reads a file from an absolute path and, when fs.promises.readFile fails, throws '读取文件失败: filePath'. The template uses the literal string 'filePath' instead of the interpolated variable, so the error does not tell you which file failed — it only signals the OS-level read failed (ENOENT, EACCES, EISDIR, etc.).","triggerScenarios":"Calling getFileContent (directly or via imageRes/fileRes/buffer helpers) with a path that does not exist, is a directory, lacks read permission, or whose parent directories are missing.","commonSituations":"Passing a relative path that wasn't resolved to absolute; file deleted/moved after selection; incorrect drive-letter or path separators on Windows; permission-restricted locations.","solutions":["Verify the file exists with fs.existsSync / access before calling getFileContent.","Resolve relative paths to absolute before calling (path.resolve).","Check the console.error output ('Failed to read file:') for the real errno (ENOENT/EACCES).","Fix the error message to interpolate the real path variable for debuggability."],"exampleFix":"// before\nthrow new Error(`读取文件失败: filePath`);\n// after\nthrow new Error(`读取文件失败: ${absolutePath} (${error?.code})`);","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst path = require('path');\nconst abs = path.resolve(filePath);\nif (!fs.existsSync(abs)) throw new Error(`文件不存在: ${abs}`);\nif (!fs.statSync(abs).isFile()) throw new Error(`不是普通文件: ${abs}`);\nfs.accessSync(abs, fs.constants.R_OK);","typeGuard":"function isReadableFile(p) {\n  const fs = require('fs');\n  try { return fs.existsSync(p) && fs.statSync(p).isFile(); } catch { return false; }\n}","tryCatchPattern":"try {\n  const buf = await getFileContent(filePath);\n} catch (e) {\n  throw new Error(`读取文件失败 [${filePath}]: ${e.cause?.code ?? e.code ?? 'unknown'}`);\n}","preventionTips":["Resolve relative paths to absolute before file operations.","Check existence and readability before reading.","Fix the error message in utils to interpolate the actual path — the current literal 'filePath' hides the culprit.","Beware Windows paths: normalize separators and drive letters."],"tags":["filesystem","nodejs","file-read"],"backgroundTag":"file-read-failure","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}