Check crash file exists before reading

Summary:
File watcher gives deletion events as well, so this is needed to avoid
attempting to read nonexistent files.

We're getting ENOENT errors from this code, and I suspect this is the reason.

Reviewed By: nikoant

Differential Revision: D19329894

fbshipit-source-id: 46734ea17874e1448d67cc086b363e0abdf07258
This commit is contained in:
John Knox
2020-01-09 08:22:14 -08:00
committed by Facebook Github Bot
parent 8a136542a3
commit 7bdde77b36

View File

@@ -34,6 +34,7 @@ import fs from 'fs';
import os from 'os';
import util from 'util';
import path from 'path';
import {promisify} from 'util';
import type {Notification} from '../../plugin.tsx';
import type {Store, DeviceLogEntry, OS, Props} from 'flipper';
import {Component} from 'react';
@@ -372,16 +373,26 @@ function addFileWatcherForiOSCrashLogs(
if (!filename || !checkFileExtension) {
return;
}
fs.readFile(path.join(dir, filename), 'utf8', function(err, data) {
if (store.getState().connections.selectedDevice?.os != 'iOS') {
// If the selected device is not iOS don't show crash notifications
const filepath = path.join(dir, filename);
promisify(fs.exists)(filepath).then(exists => {
if (!exists) {
return;
}
if (err) {
console.error(err);
return;
}
parseCrashLogAndUpdateState(store, util.format(data), setPersistedState);
fs.readFile(filepath, 'utf8', function(err, data) {
if (store.getState().connections.selectedDevice?.os != 'iOS') {
// If the selected device is not iOS don't show crash notifications
return;
}
if (err) {
console.error(err);
return;
}
parseCrashLogAndUpdateState(
store,
util.format(data),
setPersistedState,
);
});
});
});
}