Browse Source

core: Still accept a string as user-provided rejections

Renée Kooi 7 years ago
parent
commit
cd7b035976
3 changed files with 23 additions and 6 deletions
  1. 6 4
      src/core/Core.js
  2. 14 0
      src/core/Core.test.js
  3. 3 2
      src/plugins/Webcam/index.js

+ 6 - 4
src/core/Core.js

@@ -296,8 +296,9 @@ class Uppy {
       .then(() => this.opts.onBeforeFileAdded(file, this.getState().files))
 
     return beforeFileAdded.catch((err) => {
-      this.info(err.message, 'error', 5000)
-      return Promise.reject(new Error(`onBeforeFileAdded: ${err}`))
+      const message = typeof err === 'object' ? err.message : err
+      this.info(message, 'error', 5000)
+      return Promise.reject(new Error(`onBeforeFileAdded: ${message}`))
     }).then(() => {
       return Utils.getFileType(file).then((fileType) => {
         const updatedFiles = Object.assign({}, this.state.files)
@@ -942,8 +943,9 @@ class Uppy {
       .then(() => this.opts.onBeforeUpload(this.state.files))
 
     return beforeUpload.catch((err) => {
-      this.info(err.message, 'error', 5000)
-      return Promise.reject(new Error(`onBeforeUpload: ${err}`))
+      const message = typeof err === 'object' ? err.message : err
+      this.info(message, 'error', 5000)
+      return Promise.reject(new Error(`onBeforeUpload: ${message}`))
     }).then(() => {
       const waitingFileIDs = []
       Object.keys(this.state.files).forEach((fileID) => {

+ 14 - 0
src/core/Core.test.js

@@ -570,6 +570,20 @@ describe('src/Core', () => {
           expect(e.message).toEqual('File not allowed')
         })
     })
+
+    it('should work with restriction errors that are not Error class instances', () => {
+      const core = new Core({
+        onBeforeFileAdded () {
+          return Promise.reject('a plain string') // eslint-disable-line prefer-promise-reject-errors
+        }
+      })
+      return expect(core.addFile({
+        source: 'jest',
+        name: 'foo.jpg',
+        type: 'image/jpg',
+        data: null
+      })).rejects.toMatchObject({ message: 'onBeforeFileAdded: a plain string' })
+    })
   })
 
   describe('uploading a file', () => {})

+ 3 - 2
src/plugins/Webcam/index.js

@@ -233,8 +233,9 @@ module.exports = class Webcam extends Plugin {
     this.captureInProgress = true
 
     this.opts.onBeforeSnapshot().catch((err) => {
-      this.core.info(err, 'error', 5000)
-      return Promise.reject(new Error(`onBeforeSnapshot: ${err}`))
+      const message = typeof err === 'object' ? err.message : err
+      this.core.info(message, 'error', 5000)
+      return Promise.reject(new Error(`onBeforeSnapshot: ${message}`))
     }).then(() => {
       const video = this.target.querySelector('.UppyWebcam-video')
       if (!video) {