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
This commit is contained in:
Pascal Hartig
2021-05-06 07:17:01 -07:00
committed by Facebook GitHub Bot
parent f5b5e9be9f
commit 8e80ae980d

View File

@@ -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}.`);
}
}