[node.js] fs.readFileSync ()에 대한 파일을 캡처하지 않는 방법은 무엇입니까?

node.js 내에서 readFile () 은 오류를 캡처하는 방법을 보여 주지만, 오류 처리에 관한 readFileSync () 함수에 대한 주석은 없습니다 . 따라서 파일이 없을 때 readFileSync ()를 사용하려고하면 오류가 발생 Error: ENOENT, no such file or directory합니다.

발생하는 예외를 어떻게 캡처합니까? doco는 어떤 예외가 발생했는지 명시하지 않으므로 어떤 예외를 포착해야하는지 모르겠습니다. try / catch 문의 일반적인 ‘모든 가능한 예외 예외 처리’스타일을 좋아하지 않습니다. 이 경우 파일이 존재하지 않을 때 발생하는 특정 예외를 포착하고 readFileSync를 수행하려고합니다.

연결 시도를 제공하기 전에 시작시에만 동기화 기능을 수행하므로 동기화 기능을 사용해서는 안되는 의견은 필요하지 않습니다.



답변

기본적으로 fs.readFileSync파일을 찾을 수 없으면 오류가 발생합니다. 이 오류는 Error프로토 타입에서 발생하며를 사용하여 발생 throw하므로 catch하는 유일한 방법은 try / catch블록을 사용하는 것입니다.

var fileContents;
try {
  fileContents = fs.readFileSync('foo.bar');
} catch (err) {
  // Here you get the error when the file was not found,
  // but you also get any other error
}

불행히도 프로토 타입 체인을보고 어떤 오류가 발생했는지 감지 할 수 없습니다.

if (err instanceof Error)

할 수있는 최선의 방법이며 대부분의 오류에 해당됩니다. 따라서 나는 당신이 그 code속성으로 가서 그 가치를 확인 하는 것이 좋습니다 .

if (err.code === 'ENOENT') {
  console.log('File not found!');
} else {
  throw err;
}

이 방법으로이 특정 오류 만 처리하고 다른 모든 오류를 다시 발생시킵니다.

또는 오류 message속성에 액세스 하여 자세한 오류 메시지를 확인할 수도 있습니다. 이 경우 다음과 같습니다.

ENOENT, no such file or directory 'foo.bar'

도움이 되었기를 바랍니다.


답변

나는 이것을 처리하는이 방법을 선호합니다. 파일이 동 기적으로 존재하는지 확인할 수 있습니다.

var file = 'info.json';
var content = '';

// Check that the file exists locally
if(!fs.existsSync(file)) {
  console.log("File not found");
}

// The file *does* exist
else {
  // Read the file and do anything you want
  content = fs.readFileSync(file, 'utf-8');
}

참고 : 프로그램도 파일을 삭제하면 주석에 언급 된대로 경쟁 조건이 있습니다. 그러나 파일을 삭제하지 않고 파일을 쓰거나 덮어 쓰면 완전히 괜찮습니다.


답변

오류를 파악한 다음 어떤 유형의 오류인지 확인해야합니다.

try {
  var data = fs.readFileSync(...)
} catch (err) {
  // If the type is not what you want, then just throw the error again.
  if (err.code !== 'ENOENT') throw err;

  // Handle a file-not-found error
}


답변

이 시나리오에는 즉시 호출 된 람다를 사용합니다.

const config = (() => {
  try {
    return JSON.parse(fs.readFileSync('config.json'));
  } catch (error) {
    return {};
  }
})();

async 버전:

const config = await (async () => {
  try {
    return JSON.parse(await fs.readFileAsync('config.json'));
  } catch (error) {
    return {};
  }
})();


답변

NodeJS에있는 유일한 스레드를 차단하지 않으려면 Async를 대신 사용해보십시오 . 이 예를 확인하십시오.

const util = require('util');
const fs = require('fs');
const path = require('path');
const readFileAsync = util.promisify(fs.readFile);

const readContentFile = async (filePath) => {
  // Eureka, you are using good code practices here!
  const content = await readFileAsync(path.join(__dirname, filePath), {
    encoding: 'utf8'
  })
  return content;
}

나중에이 비동기 함수를 다른 함수의 try / catch와 함께 사용할 수 있습니다.

const anyOtherFun = async () => {
  try {
    const fileContent = await readContentFile('my-file.txt');
  } catch (err) {
    // Here you get the error when the file was not found,
    // but you also get any other error
  }
}

행복한 코딩!


답변

JavaScript try… catch 메커니즘을 사용하여 비동기 API로 생성 된 오류를 가로 챌 수 없습니다. 초보자에게는 일반적인 실수는 오류 우선 콜백에서 throw를 사용하는 것입니다.

// THIS WILL NOT WORK:
const fs = require('fs');

try {
  fs.readFile('/some/file/that/does-not-exist', (err, data) => {
    // Mistaken assumption: throwing here...
    if (err) {
      throw err;
    }
  });
} catch (err) {
  // This will not catch the throw!
  console.error(err);
}

fs.readFile ()에 전달 된 콜백 함수가 비동기 적으로 호출되기 때문에 작동하지 않습니다. 콜백이 호출 될 때 try … catch 블록을 포함한 주변 코드가 이미 종료되었습니다. 콜백 내에 오류가 발생하면 대부분의 경우 Node.js 프로세스가 중단 될 수 있습니다. 도메인이 사용 가능하거나 핸들러가 process.on ( ‘uncaughtException’)에 등록 된 경우 이러한 오류가 인터셉트 될 수 있습니다.

참조 :
https://nodejs.org/api/errors.html


답변