Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 6935947

Browse files
committed
Catch invalid url errors such as empty or null source or request failed
1 parent 7c18c96 commit 6935947

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/FileSystem.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ export class FileSystem {
211211
} catch (error) {
212212
// File must be manually removed on download error https://github.com/wkh237/react-native-fetch-blob/issues/331
213213
await RNFetchBlob.fs.unlink(path);
214-
throw error;
214+
return {
215+
path: null,
216+
fileName: pathLib.basename(path),
217+
};
215218
}
216219

217220
return {

src/imageCacheHoc.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export default function imageCacheHoc(Image, options = {}) {
130130
);
131131

132132
// Validate input
133-
this._validateImageComponent();
133+
this.InvalidUrl = !this._validateImageComponent();
134134
}
135135

136136
_validateImageComponent() {
@@ -195,6 +195,8 @@ export default function imageCacheHoc(Image, options = {}) {
195195
FileSystem.unlockCacheFile(fileName, this.componentId);
196196
FileSystem.lockCacheFile(nextFileName, this.componentId);
197197

198+
this.InvalidUrl = !this._validateImageComponent();
199+
198200
// Init the image cache logic
199201
await this._loadImage(nextUrl);
200202
}
@@ -208,6 +210,11 @@ export default function imageCacheHoc(Image, options = {}) {
208210
* @private
209211
*/
210212
async _loadImage(url) {
213+
if (this.InvalidUrl) {
214+
this.setState({ localFilePath: null });
215+
return;
216+
}
217+
211218
// Check local fs for file, fallback to network and write file to disk if local file not found.
212219
const permanent = this.props.permanent ? true : false;
213220
let localFilePath = null;
@@ -217,13 +224,15 @@ export default function imageCacheHoc(Image, options = {}) {
217224
console.warn(error); // eslint-disable-line no-console
218225
}
219226

227+
this.InvalidUrl = !localFilePath;
228+
220229
// Check component is still mounted to avoid calling setState() on components that were quickly
221230
// mounted then unmounted before componentDidMount() finishes.
222231
// See: https://github.com/billmalarky/react-native-image-cache-hoc/issues/6#issuecomment-354490597
223232
if (this._isMounted && localFilePath) {
224233
this.setState({ localFilePath });
225234

226-
if (this.props.onLoadFinished) {
235+
if (!this.InvalidUrl && this.props.onLoadFinished) {
227236
Image.getSize(
228237
Platform.OS === 'ios' ? localFilePath : 'file://' + localFilePath,
229238
(width, height) => {
@@ -249,7 +258,7 @@ export default function imageCacheHoc(Image, options = {}) {
249258

250259
render() {
251260
// If media loaded, render full image component, else render placeholder.
252-
if (this.state.localFilePath) {
261+
if (this.state.localFilePath && !this.InvalidUrl) {
253262
// Build platform specific file resource uri.
254263
const localFileUri =
255264
Platform.OS == 'ios' ? this.state.localFilePath : 'file://' + this.state.localFilePath; // Android requires the traditional 3 prefixed slashes file:/// in a localhost absolute file uri.

0 commit comments

Comments
 (0)