yikart/AiToEarn · error · Error

读取文件失败: filePath

Error message

读取文件失败: filePath

What it means

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.).

Source

Thrown at project/aitoearn-electron/electron/plat/utils/index.ts:28

export async function getFileContent(filePath: string): Promise<Buffer> {
  try {
    if (filePath.includes('https://') || filePath.includes('http://')) {
      const res = await requestNet({
        url: filePath,
        isReqFile: true,
      });
      return res.data;
    } else {
      // 确保路径是绝对路径
      const absolutePath = path.resolve(filePath);
      console.log('Reading file:', absolutePath);

      // 读取文件内容
      return await fs.promises.readFile(absolutePath);
    }
  } catch (error) {
    console.error('Failed to read file:', error);
    throw new Error(`读取文件失败: filePath`);
  }
}

/**
 * 获取图片的基本信息
 * @param filePath
 */
export async function getImageBaseInfo(filePath: string) {
  const buffer = await getFileContent(filePath);
  const metadata = await sharp(buffer).metadata();

  return {
    width: metadata.width,
    height: metadata.height,
  };
}

export const CookieToString = (cookies: Electron.Cookie[]) => {

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Verify the file exists with fs.existsSync / access before calling getFileContent.
  2. Resolve relative paths to absolute before calling (path.resolve).
  3. Check the console.error output ('Failed to read file:') for the real errno (ENOENT/EACCES).
  4. Fix the error message to interpolate the real path variable for debuggability.

Example fix

// before
throw new Error(`读取文件失败: filePath`);
// after
throw new Error(`读取文件失败: ${absolutePath} (${error?.code})`);
Defensive patterns

Strategy: validation

Validate before calling

const fs = require('fs');
const path = require('path');
const abs = path.resolve(filePath);
if (!fs.existsSync(abs)) throw new Error(`文件不存在: ${abs}`);
if (!fs.statSync(abs).isFile()) throw new Error(`不是普通文件: ${abs}`);
fs.accessSync(abs, fs.constants.R_OK);

Type guard

function isReadableFile(p) {
  const fs = require('fs');
  try { return fs.existsSync(p) && fs.statSync(p).isFile(); } catch { return false; }
}

Try / catch

try {
  const buf = await getFileContent(filePath);
} catch (e) {
  throw new Error(`读取文件失败 [${filePath}]: ${e.cause?.code ?? e.code ?? 'unknown'}`);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/47e6f5784db39047. Report an issue: GitHub.