Quellcode durchsuchen

Fix fs API error message

Ben Hsieh vor 8 Jahren
Ursprung
Commit
378820435c
1 geänderte Dateien mit 21 neuen und 20 gelöschten Zeilen
  1. 21
    20
      src/fs.js

+ 21
- 20
src/fs.js Datei anzeigen

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