Browse Source

Fix fs API error message

Ben Hsieh 8 years ago
parent
commit
378820435c
1 changed files with 21 additions and 20 deletions
  1. 21
    20
      src/fs.js

+ 21
- 20
src/fs.js View File

61
   return new Promise((resolve, reject) => {
61
   return new Promise((resolve, reject) => {
62
     let handler = (err) => {
62
     let handler = (err) => {
63
       if(err)
63
       if(err)
64
-      reject(err)
64
+        reject(new Error(err))
65
       else
65
       else
66
-      resolve()
66
+        resolve()
67
     }
67
     }
68
     if(encoding.toLowerCase() === 'ascii') {
68
     if(encoding.toLowerCase() === 'ascii') {
69
       if(Array.isArray(data))
69
       if(Array.isArray(data))
70
         RNFetchBlob.createFileASCII(path, data, handler)
70
         RNFetchBlob.createFileASCII(path, data, handler)
71
       else
71
       else
72
-        reject('`data` of ASCII file must be an array contains numbers')
72
+        reject(new Error('`data` of ASCII file must be an array contains numbers'))
73
     }
73
     }
74
     else {
74
     else {
75
       RNFetchBlob.createFile(path, data, encoding, handler)
75
       RNFetchBlob.createFile(path, data, encoding, handler)
96
   return new Promise((resolve, reject) => {
96
   return new Promise((resolve, reject) => {
97
     RNFetchBlob.writeStream(path, encoding || 'base64', append || false, (err, streamId:string) => {
97
     RNFetchBlob.writeStream(path, encoding || 'base64', append || false, (err, streamId:string) => {
98
       if(err)
98
       if(err)
99
-        reject(err)
99
+        reject(new Error(err))
100
       else
100
       else
101
         resolve(new RNFetchBlobWriteStream(streamId, encoding))
101
         resolve(new RNFetchBlobWriteStream(streamId, encoding))
102
     })
102
     })
129
   return new Promise((resolve, reject) => {
129
   return new Promise((resolve, reject) => {
130
     RNFetchBlob.mkdir(path, (err, res) => {
130
     RNFetchBlob.mkdir(path, (err, res) => {
131
       if(err)
131
       if(err)
132
-        reject(err)
132
+        reject(new Error(err))
133
       else
133
       else
134
         resolve()
134
         resolve()
135
     })
135
     })
145
  */
145
  */
146
 function readFile(path:string, encoding:string, bufferSize:?number):Promise<any> {
146
 function readFile(path:string, encoding:string, bufferSize:?number):Promise<any> {
147
   if(typeof path !== 'string')
147
   if(typeof path !== 'string')
148
-    return Promise.reject('Invalid argument "path" ')
148
+    return Promise.reject(new Error('Invalid argument "path" '))
149
   return RNFetchBlob.readFile(path, encoding)
149
   return RNFetchBlob.readFile(path, encoding)
150
 }
150
 }
151
 
151
 
162
     return Promise.reject('Invalid argument "path" ')
162
     return Promise.reject('Invalid argument "path" ')
163
   if(encoding.toLocaleLowerCase() === 'ascii') {
163
   if(encoding.toLocaleLowerCase() === 'ascii') {
164
     if(!Array.isArray(data))
164
     if(!Array.isArray(data))
165
-      Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
165
+      Promise.reject(new Error(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`))
166
     else
166
     else
167
       return RNFetchBlob.writeFileArray(path, data, false);
167
       return RNFetchBlob.writeFileArray(path, data, false);
168
   } else {
168
   } else {
169
     if(typeof data !== 'string')
169
     if(typeof data !== 'string')
170
-      Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
170
+      Promise.reject(new Error(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`))
171
     else
171
     else
172
       return RNFetchBlob.writeFile(path, encoding, data, false);
172
       return RNFetchBlob.writeFile(path, encoding, data, false);
173
   }
173
   }
179
     return Promise.reject('Invalid argument "path" ')
179
     return Promise.reject('Invalid argument "path" ')
180
   if(encoding.toLocaleLowerCase() === 'ascii') {
180
   if(encoding.toLocaleLowerCase() === 'ascii') {
181
     if(!Array.isArray(data))
181
     if(!Array.isArray(data))
182
-      Promise.reject(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`)
182
+      Promise.reject(new Error(`Expected "data" is an Array when encoding is "ascii", however got ${typeof data}`))
183
     else
183
     else
184
       return RNFetchBlob.writeFileArray(path, data, true);
184
       return RNFetchBlob.writeFileArray(path, data, true);
185
   } else {
185
   } else {
186
     if(typeof data !== 'string')
186
     if(typeof data !== 'string')
187
-      Promise.reject(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`)
187
+      Promise.reject(new Error(`Expected "data" is a String when encoding is "utf8" or "base64", however got ${typeof data}`))
188
     else
188
     else
189
       return RNFetchBlob.writeFile(path, encoding, data, true);
189
       return RNFetchBlob.writeFile(path, encoding, data, true);
190
   }
190
   }
199
   return new Promise((resolve, reject) => {
199
   return new Promise((resolve, reject) => {
200
     RNFetchBlob.stat(path, (err, stat) => {
200
     RNFetchBlob.stat(path, (err, stat) => {
201
       if(err)
201
       if(err)
202
-        reject(err)
202
+        reject(new Error(err))
203
       else
203
       else
204
         resolve(stat)
204
         resolve(stat)
205
     })
205
     })
215
   return new Promise((resolve, reject) => {
215
   return new Promise((resolve, reject) => {
216
     RNFetchBlob.scanFile(pairs, (err) => {
216
     RNFetchBlob.scanFile(pairs, (err) => {
217
       if(err)
217
       if(err)
218
-        reject(err)
218
+        reject(new Error(err))
219
       else
219
       else
220
         resolve()
220
         resolve()
221
     })
221
     })
226
   return new Promise((resolve, reject) => {
226
   return new Promise((resolve, reject) => {
227
     RNFetchBlob.cp(path, dest, (err, res) => {
227
     RNFetchBlob.cp(path, dest, (err, res) => {
228
       if(err)
228
       if(err)
229
-        reject(err)
229
+        reject(new Error(err))
230
       else
230
       else
231
         resolve(res)
231
         resolve(res)
232
     })
232
     })
237
   return new Promise((resolve, reject) => {
237
   return new Promise((resolve, reject) => {
238
     RNFetchBlob.mv(path, dest, (err, res) => {
238
     RNFetchBlob.mv(path, dest, (err, res) => {
239
       if(err)
239
       if(err)
240
-        reject(err)
240
+        reject(new Error(err))
241
       else
241
       else
242
         resolve(res)
242
         resolve(res)
243
     })
243
     })
248
   return new Promise((resolve, reject) => {
248
   return new Promise((resolve, reject) => {
249
     RNFetchBlob.lstat(path, (err, stat) => {
249
     RNFetchBlob.lstat(path, (err, stat) => {
250
       if(err)
250
       if(err)
251
-        reject(err)
251
+        reject(new Error(err))
252
       else
252
       else
253
         resolve(stat)
253
         resolve(stat)
254
     })
254
     })
259
   return new Promise((resolve, reject) => {
259
   return new Promise((resolve, reject) => {
260
     RNFetchBlob.ls(path, (err, res) => {
260
     RNFetchBlob.ls(path, (err, res) => {
261
       if(err)
261
       if(err)
262
-        reject(err)
262
+        reject(new Error(err))
263
       else
263
       else
264
         resolve(res)
264
         resolve(res)
265
     })
265
     })
274
 function unlink(path:string):Promise {
274
 function unlink(path:string):Promise {
275
   return new Promise((resolve, reject) => {
275
   return new Promise((resolve, reject) => {
276
     RNFetchBlob.unlink(path, (err) => {
276
     RNFetchBlob.unlink(path, (err) => {
277
-      if(err)
278
-        reject(err)
277
+      if(err) {
278
+        reject(new Error(err))
279
+      }
279
       else
280
       else
280
         resolve()
281
         resolve()
281
     })
282
     })
295
         resolve(exist)
296
         resolve(exist)
296
       })
297
       })
297
     } catch(err) {
298
     } catch(err) {
298
-      reject(err)
299
+      reject(new Error(err))
299
     }
300
     }
300
   })
301
   })
301
 
302
 
331
         resolve(isDir)
332
         resolve(isDir)
332
       })
333
       })
333
     } catch(err) {
334
     } catch(err) {
334
-      reject(err)
335
+      reject(new Error(err))
335
     }
336
     }
336
   })
337
   })
337
 
338