From 8e80ae980dee0b41cb0e2506ac92dcddfe1216e6 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Thu, 6 May 2021 07:17:01 -0700 Subject: [PATCH] Improve error handling for corrupt images/videos Summary: We can't fix the underlying issue but it's quite frustrating when you get an error from Preview or QuickTime after the file was pulled. This improves the messaging a little. Reviewed By: mweststrate Differential Revision: D28096087 fbshipit-source-id: 0428fa821a225a02e81574f16d066085e8775251 --- .../app/src/chrome/ScreenCaptureButtons.tsx | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/desktop/app/src/chrome/ScreenCaptureButtons.tsx b/desktop/app/src/chrome/ScreenCaptureButtons.tsx index 76b61076b..6c169dbf3 100644 --- a/desktop/app/src/chrome/ScreenCaptureButtons.tsx +++ b/desktop/app/src/chrome/ScreenCaptureButtons.tsx @@ -10,6 +10,7 @@ import {Button as AntButton, message} from 'antd'; import React, {useState, useEffect, useCallback} from 'react'; import path from 'path'; +import fs from 'fs-extra'; import open from 'open'; import {capture, CAPTURE_LOCATION, getFileName} from '../utils/screenshot'; import {CameraOutlined, VideoCameraOutlined} from '@ant-design/icons'; @@ -20,10 +21,27 @@ async function openFile(path: string | null) { return; } + let fileStat; + try { + fileStat = await fs.stat(path); + } catch (err) { + message.error(`Couldn't open captured file: ${path}: ${err}`); + return; + } + + // Rather randomly chosen. Some FSs still reserve 8 bytes for empty files. + // If this doesn't reliably catch "corrupt" files, you might want to increase this. + if (fileStat.size <= 8) { + message.error( + 'Screencap file retrieved from device appears to be corrupt. Your device may not support screen recording. Sometimes restarting your device can help.', + ); + return; + } + try { await open(path); } catch (e) { - console.error(`Opening ${path} failed with error ${e}.`); + console.warn(`Opening ${path} failed with error ${e}.`); } }