From 7bdde77b3643f80ba8f540651206408f5beba498 Mon Sep 17 00:00:00 2001 From: John Knox Date: Thu, 9 Jan 2020 08:22:14 -0800 Subject: [PATCH] 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 --- src/plugins/crash_reporter/index.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/plugins/crash_reporter/index.js b/src/plugins/crash_reporter/index.js index 2db1e5386..ca5e5dff7 100644 --- a/src/plugins/crash_reporter/index.js +++ b/src/plugins/crash_reporter/index.js @@ -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, + ); + }); }); }); }