Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit b634402

Browse files
LionessTesterwkh237
authored andcommitted
Fix for issue #468, #461, #460 and minor cleanup (#469)
* bump to 0.10.8 * Update PULL_REQUEST_TEMPLATE * Fix #468 "Messy error returns: Sometimes a string, sometimes an Error object" * Cleanup: remove an unused constant and duplicate method definitions * Cleanup: - fix minor errors in JSDoc comments, for example {string]} => {string} - fix parameter name "encode" => "encoding" (more logical, and it says so in the function's JSDoc too) - json-stream.js: split a looooong log message string constant into two parts and fix a typo ("maually"), and the type for objects is "Object" (capitalized) in Flow type annotations * Fix a (Flow) type conflict - fixes issue #461 * NEEDS REVIEW - Attempt to fix some of issue #460 (Error message normalization) Error messages reported by iOS and Android versions should be as similar as possible. Also, within the same system there should be consistency. This patch is an attempt to bring a LITTLE more of this consistency to the error messages. I also fixed some very few minor language issues, like "does not exist" (is the correct English). I tried keeping the changes to a minimum. Background: In my project code I want to know when a file already exists (e.g. after calling fs.createFile), and the only way is to check the error message string that I get. It's bad if they differ between versions (createFileASCII and createFile) and then also between Android and iOS version. At least some core part of the string should be the same, so that I have something to match. Ideally messages should come from a centralized easy (easier) to maintain file (for both iOS and Android), and ideally both systems should have the same errors and messages as far as possible.
1 parent 55009f1 commit b634402

File tree

12 files changed

+88
-89
lines changed

12 files changed

+88
-89
lines changed

.github/PULL_REQUEST_TEMPLATE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Thank you for making a pull request ! Just a gentle reminder :)
22

33
1. If the PR is offering a feature please make the request to our "Feature Branch" 0.11.0
4-
2. Bug fix request to "Bug Fix Branch" 0.10.8
4+
2. Bug fix request to "Bug Fix Branch" 0.10.9
55
3. Correct README.md can directly to master

android.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
1313

1414
/**
1515
* Send an intent to open the file.
16-
* @param {string]} path Path of the file to be open.
16+
* @param {string} path Path of the file to be open.
1717
* @param {string} mime MIME type string
1818
* @return {Promise}
1919
*/

android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+27-24
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static public void writeFile(String path, String encoding, String data, final bo
7878
data = normalizePath(data);
7979
File src = new File(data);
8080
if(!src.exists()) {
81-
promise.reject("RNfetchBlob writeFileError", "source file : " + data + "not exists");
81+
promise.reject("RNfetchBlob writeFile error", "source file : " + data + " does not exist");
8282
fout.close();
8383
return ;
8484
}
@@ -100,7 +100,7 @@ static public void writeFile(String path, String encoding, String data, final bo
100100
fout.close();
101101
promise.resolve(written);
102102
} catch (Exception e) {
103-
promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
103+
promise.reject("RNFetchBlob writeFile error", e.getLocalizedMessage());
104104
}
105105
}
106106

@@ -127,7 +127,7 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
127127
os.close();
128128
promise.resolve(data.size());
129129
} catch (Exception e) {
130-
promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
130+
promise.reject("RNFetchBlob writeFile error", e.getLocalizedMessage());
131131
}
132132
}
133133

@@ -309,7 +309,8 @@ else if(resolved == null) {
309309
buffer = null;
310310

311311
} catch (Exception err) {
312-
emitStreamEvent(streamId, "warn", "Failed to convert data to "+encoding+" encoded string, this might due to the source data is not able to convert using this encoding.");
312+
emitStreamEvent(streamId, "warn", "Failed to convert data to " + encoding +
313+
" encoded string, this might due to the source data is not able to convert using this encoding.");
313314
err.printStackTrace();
314315
}
315316
}
@@ -324,7 +325,7 @@ else if(resolved == null) {
324325
public void writeStream(String path, String encoding, boolean append, Callback callback) {
325326
File dest = new File(path);
326327
if(!dest.exists() || dest.isDirectory()) {
327-
callback.invoke("write stream error: target path `" + path + "` may not exists or it's a folder");
328+
callback.invoke("target path `" + path + "` may not exist or it is a folder");
328329
return;
329330
}
330331
try {
@@ -336,7 +337,7 @@ public void writeStream(String path, String encoding, boolean append, Callback c
336337
this.writeStreamInstance = fs;
337338
callback.invoke(null, streamId);
338339
} catch(Exception err) {
339-
callback.invoke("write stream error: failed to create write stream at path `"+path+"` "+ err.getLocalizedMessage());
340+
callback.invoke("failed to create write stream at path `" + path + "` " + err.getLocalizedMessage());
340341
}
341342

342343
}
@@ -433,12 +434,13 @@ static void deleteRecursive(File fileOrDirectory) {
433434
static void mkdir(String path, Callback callback) {
434435
File dest = new File(path);
435436
if(dest.exists()) {
436-
callback.invoke("mkdir error: failed to create folder at `" + path + "` folder already exists");
437+
callback.invoke("mkdir failed, folder already exists at " + path);
437438
return;
438439
}
439440
dest.mkdirs();
440441
callback.invoke();
441442
}
443+
442444
/**
443445
* Copy file to destination path
444446
* @param path Source path
@@ -454,7 +456,7 @@ static void cp(String path, String dest, Callback callback) {
454456
try {
455457

456458
if(!isPathExists(path)) {
457-
callback.invoke("cp error: source file at path`" + path + "` not exists");
459+
callback.invoke("source file at path`" + path + "` does not exist");
458460
return;
459461
}
460462
if(!new File(dest).exists())
@@ -495,7 +497,7 @@ static void cp(String path, String dest, Callback callback) {
495497
static void mv(String path, String dest, Callback callback) {
496498
File src = new File(path);
497499
if(!src.exists()) {
498-
callback.invoke("mv error: source file at path `" + path + "` does not exists");
500+
callback.invoke("source file at path `" + path + "` does not exist");
499501
return;
500502
}
501503
src.renameTo(new File(dest));
@@ -535,7 +537,7 @@ static void ls(String path, Callback callback) {
535537
path = normalizePath(path);
536538
File src = new File(path);
537539
if (!src.exists() || !src.isDirectory()) {
538-
callback.invoke("ls error: failed to list path `" + path + "` for it is not exist or it is not a folder");
540+
callback.invoke("failed to list path `" + path + "` for it is not exist or it is not a folder");
539541
return;
540542
}
541543
String[] files = new File(path).list();
@@ -559,7 +561,7 @@ public static void slice(String src, String dest, int start, int end, String enc
559561
src = normalizePath(src);
560562
File source = new File(src);
561563
if(!source.exists()) {
562-
promise.reject("RNFetchBlob.slice error", "source file : " + src + " not exists");
564+
promise.reject("RNFetchBlob slice error", "source file : " + src + " does not exist");
563565
return;
564566
}
565567
long size = source.length();
@@ -585,7 +587,7 @@ public static void slice(String src, String dest, int start, int end, String enc
585587
promise.resolve(dest);
586588
} catch (Exception e) {
587589
e.printStackTrace();
588-
promise.reject(e.getLocalizedMessage());
590+
promise.reject("RNFetchBlob slice error", e.getLocalizedMessage());
589591
}
590592
}
591593

@@ -597,18 +599,18 @@ static void lstat(String path, final Callback callback) {
597599
protected Integer doInBackground(String ...args) {
598600
WritableArray res = Arguments.createArray();
599601
if(args[0] == null) {
600-
callback.invoke("lstat error: the path specified for lstat is either `null` or `undefined`.");
602+
callback.invoke("the path specified for lstat is either `null` or `undefined`.");
601603
return 0;
602604
}
603605
File src = new File(args[0]);
604606
if(!src.exists()) {
605-
callback.invoke("lstat error: failed to list path `" + args[0] + "` for it is not exist or it is not a folder");
607+
callback.invoke("failed to lstat path `" + args[0] + "` because it does not exist or it is not a folder");
606608
return 0;
607609
}
608610
if(src.isDirectory()) {
609611
String [] files = src.list();
610612
for(String p : files) {
611-
res.pushMap(statFile ( src.getPath() + "/" + p));
613+
res.pushMap(statFile(src.getPath() + "/" + p));
612614
}
613615
}
614616
else {
@@ -630,7 +632,7 @@ static void stat(String path, Callback callback) {
630632
path = normalizePath(path);
631633
WritableMap result = statFile(path);
632634
if(result == null)
633-
callback.invoke("stat error: failed to list path `" + path + "` for it is not exist or it is not a folder", null);
635+
callback.invoke("failed to stat path `" + path + "` because it does not exist or it is not a folder", null);
634636
else
635637
callback.invoke(null, result);
636638
} catch(Exception err) {
@@ -709,23 +711,24 @@ static void createFile(String path, String data, String encoding, Callback callb
709711
String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
710712
File src = new File(orgPath);
711713
if(!src.exists()) {
712-
callback.invoke("RNfetchBlob writeFileError", "source file : " + data + "not exists");
714+
callback.invoke("source file : " + data + " does not exist");
713715
return ;
714716
}
715717
FileInputStream fin = new FileInputStream(src);
716718
OutputStream ostream = new FileOutputStream(dest);
717-
byte [] buffer = new byte [10240];
719+
byte[] buffer = new byte[10240];
718720
int read = fin.read(buffer);
719-
while(read > 0) {
721+
while (read > 0) {
720722
ostream.write(buffer, 0, read);
721723
read = fin.read(buffer);
722724
}
723725
fin.close();
724726
ostream.close();
725-
}
726-
else {
727+
} else {
727728
if (!created) {
728-
callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists, or the file already exists. If you intended to overwrite the existing file use fs.writeFile instead.");
729+
callback.invoke("failed to create new file at path `" + path + "` because its parent path " +
730+
"may not exist, or the file already exists. If you intended to overwrite the " +
731+
"existing file use fs.writeFile instead.");
729732
return;
730733
}
731734
OutputStream ostream = new FileOutputStream(dest);
@@ -747,12 +750,12 @@ static void createFileASCII(String path, ReadableArray data, Callback callback)
747750
try {
748751
File dest = new File(path);
749752
if(dest.exists()) {
750-
callback.invoke("create file error: failed to create file at path `" + path + "`, file already exists.");
753+
callback.invoke("failed to create new file at path `" + path + "`, file already exists.");
751754
return;
752755
}
753756
boolean created = dest.createNewFile();
754757
if(!created) {
755-
callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");
758+
callback.invoke("failed to create new file at path `" + path + "` because its parent path may not exist");
756759
return;
757760
}
758761
OutputStream ostream = new FileOutputStream(dest);

class/RNFetchBlobSession.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@ import {
99
} from 'react-native'
1010

1111
const RNFetchBlob = NativeModules.RNFetchBlob
12-
const emitter = DeviceEventEmitter
1312

1413
let sessions = {}
1514

1615
export default class RNFetchBlobSession {
1716

18-
add : (path:string) => RNFetchBlobSession;
19-
remove : (path:string) => RNFetchBlobSession;
20-
dispose : () => Promise;
21-
list : () => Array<string>;
2217
name : string;
2318

2419
static getSession(name:string):any {
@@ -50,7 +45,7 @@ export default class RNFetchBlobSession {
5045

5146
remove(path:string):RNFetchBlobSession {
5247
let list = sessions[this.name]
53-
for(let i in list) {
48+
for(let i of list) {
5449
if(list[i] === path) {
5550
sessions[this.name].splice(i, 1)
5651
break;
@@ -67,7 +62,7 @@ export default class RNFetchBlobSession {
6762
return new Promise((resolve, reject) => {
6863
RNFetchBlob.removeSession(sessions[this.name], (err) => {
6964
if(err)
70-
reject(err)
65+
reject(new Error(err))
7166
else {
7267
delete sessions[this.name]
7368
resolve()

class/RNFetchBlobWriteStream.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import {
99
} from 'react-native'
1010

1111
const RNFetchBlob = NativeModules.RNFetchBlob
12-
const emitter = DeviceEventEmitter
1312

1413
export default class RNFetchBlobWriteStream {
1514

1615
id : string;
1716
encoding : string;
18-
append : bool;
17+
append : boolean;
1918

20-
constructor(streamId:string, encoding:string, append:string) {
19+
constructor(streamId:string, encoding:string, append:boolean) {
2120
this.id = streamId
2221
this.encoding = encoding
2322
this.append = append
@@ -28,17 +27,17 @@ export default class RNFetchBlobWriteStream {
2827
try {
2928
let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk'
3029
if(this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) {
31-
reject('ascii input data must be an Array')
30+
reject(new Error('ascii input data must be an Array'))
3231
return
3332
}
3433
RNFetchBlob[method](this.id, data, (error) => {
3534
if(error)
36-
reject(error)
35+
reject(new Error(error))
3736
else
3837
resolve()
3938
})
4039
} catch(err) {
41-
reject(err)
40+
reject(new Error(err))
4241
}
4342
})
4443
}
@@ -50,7 +49,7 @@ export default class RNFetchBlobWriteStream {
5049
resolve()
5150
})
5251
} catch (err) {
53-
reject(err)
52+
reject(new Error(err))
5453
}
5554
})
5655
}

fs.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
} from './types'
2020

2121
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
22-
const emitter = DeviceEventEmitter
22+
2323
const dirs = {
2424
DocumentDir : RNFetchBlob.DocumentDir,
2525
CacheDir : RNFetchBlob.CacheDir,
@@ -83,13 +83,13 @@ function createFile(path:string, data:string, encoding: 'base64' | 'ascii' | 'ut
8383
* Create write stream to a file.
8484
* @param {string} path Target path of file stream.
8585
* @param {string} encoding Encoding of input data.
86-
* @param {bool} append A flag represent if data append to existing ones.
87-
* @return {Promise<WriteStream>} A promise resolves a `WriteStream` object.
86+
* @param {boolean} [append] A flag represent if data append to existing ones.
87+
* @return {Promise<RNFetchBlobWriteStream>} A promise resolves a `WriteStream` object.
8888
*/
8989
function writeStream(
9090
path : string,
9191
encoding : 'utf8' | 'ascii' | 'base64',
92-
append? : ?bool,
92+
append? : ?boolean,
9393
):Promise<RNFetchBlobWriteStream> {
9494
if(!path)
9595
throw Error('RNFetchBlob could not open file stream with empty `path`')
@@ -110,6 +110,7 @@ function writeStream(
110110
* @param {string} path The file path.
111111
* @param {string} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
112112
* @param {boolean} bufferSize Size of stream buffer.
113+
* @param {number} [tick=10] Interval in milliseconds between reading chunks of data
113114
* @return {RNFetchBlobStream} RNFetchBlobStream stream instance.
114115
*/
115116
function readStream(
@@ -154,7 +155,7 @@ function pathForAppGroup(groupName:string):Promise {
154155
* @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
155156
* @return {Promise<Array<number> | string>}
156157
*/
157-
function readFile(path:string, encoding:string, bufferSize:?number):Promise<any> {
158+
function readFile(path:string, encoding:string):Promise<any> {
158159
if(typeof path !== 'string')
159160
return Promise.reject(new Error('Invalid argument "path" '))
160161
return RNFetchBlob.readFile(path, encoding)
@@ -170,7 +171,7 @@ function readFile(path:string, encoding:string, bufferSize:?number):Promise<any>
170171
function writeFile(path:string, data:string | Array<number>, encoding:?string):Promise {
171172
encoding = encoding || 'utf8'
172173
if(typeof path !== 'string')
173-
return Promise.reject('Invalid argument "path" ')
174+
return Promise.reject(new Error('Invalid argument "path" '))
174175
if(encoding.toLocaleLowerCase() === 'ascii') {
175176
if(!Array.isArray(data))
176177
return Promise.reject(new Error(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`))
@@ -187,7 +188,7 @@ function writeFile(path:string, data:string | Array<number>, encoding:?string):P
187188
function appendFile(path:string, data:string | Array<number>, encoding:?string):Promise {
188189
encoding = encoding || 'utf8'
189190
if(typeof path !== 'string')
190-
return Promise.reject('Invalid argument "path" ')
191+
return Promise.reject(new Error('Invalid argument "path" '))
191192
if(encoding.toLocaleLowerCase() === 'ascii') {
192193
if(!Array.isArray(data))
193194
return Promise.reject(new Error(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`))
@@ -224,7 +225,7 @@ function stat(path:string):Promise<RNFetchBlobFile> {
224225

225226
/**
226227
* Android only method, request media scanner to scan the file.
227-
* @param {Array<Object<string, string>>} Array contains Key value pairs with key `path` and `mime`.
228+
* @param {Array<Object<string, string>>} pairs Array contains Key value pairs with key `path` and `mime`.
228229
* @return {Promise}
229230
*/
230231
function scanFile(pairs:any):Promise {
@@ -302,10 +303,9 @@ function unlink(path:string):Promise {
302303
/**
303304
* Check if file exists and if it is a folder.
304305
* @param {string} path Path to check
305-
* @return {Promise<bool, bool>}
306+
* @return {Promise<boolean, boolean>}
306307
*/
307-
function exists(path:string):Promise<bool, bool> {
308-
308+
function exists(path:string):Promise<boolean, boolean> {
309309
return new Promise((resolve, reject) => {
310310
try {
311311
RNFetchBlob.exists(path, (exist) => {
@@ -358,7 +358,7 @@ function df():Promise<{ free : number, total : number }> {
358358
return new Promise((resolve, reject) => {
359359
RNFetchBlob.df((err, stat) => {
360360
if(err)
361-
reject(err)
361+
reject(new Error(err))
362362
else
363363
resolve(stat)
364364
})

0 commit comments

Comments
 (0)