joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 770 forks source link

Wrong error reported when trying to cp into directory which has not yet been created #816

Open gravitypersists opened 1 year ago

gravitypersists commented 1 year ago
// in this case filePath was derived from react-native-image-picker and is the default images provided by ios simulator
const filePath = '/Users/me/Library/Developer/CoreSimulator/Devices/D266C4/data/Containers/Data/Application/6D3A9/tmp/F2D4E641-5AD7-4857-B17C-8AC9716A8A35.jpg'
const destPath = `${RNFetchBlob.fs.dirs.CacheDir}/a/b.jpg`; // doesn't work
// const destPath = `${RNFetchBlob.fs.dirs.CacheDir}/b.jpg`; // does work
RNFetchBlob.fs.cp(filePath, destPath);

// also, this is true:
const exists = await RNFetchBlob.fs.exists(filePath);

Running this code will produce the error: [Error: The file “F2D4E641-5AD7-4857-B17C-8AC9716A8A35.jpg” doesn’t exist.]

This, like cp in bash, will fail when the directory you're copying into doesn't exist. It took me quite a while to realize that, because the error message sent me down a different rabbit hole of trying to understand why my source file was missing (despite fs.exists telling it does exist).

Instead I would expect the error to be Directory "a" doesn't exist or perhaps Failed to cp "b.jpg"