Always reject Promises with Error instances

This commit is contained in:
Renée Kooi 2017-09-22 12:22:35 +02:00
parent ecefc7eed7
commit 8a3b411409
No known key found for this signature in database
GPG key ID: 30516CF2A8E63718
5 changed files with 15 additions and 15 deletions

View file

@ -296,8 +296,8 @@ class Uppy {
.then(() => this.opts.onBeforeFileAdded(file, this.getState().files))
return beforeFileAdded.catch((err) => {
this.info(err, 'error', 5000)
return Promise.reject(`onBeforeFileAdded: ${err}`)
this.info(err.message, 'error', 5000)
return Promise.reject(new Error(`onBeforeFileAdded: ${err}`))
}).then(() => {
return Utils.getFileType(file).then((fileType) => {
const updatedFiles = Object.assign({}, this.state.files)
@ -335,7 +335,7 @@ class Uppy {
}
const isFileAllowed = this.checkRestrictions(newFile)
if (!isFileAllowed) return Promise.reject('File not allowed')
if (!isFileAllowed) return Promise.reject(new Error('File not allowed'))
updatedFiles[fileID] = newFile
this.setState({files: updatedFiles})
@ -935,15 +935,15 @@ class Uppy {
upload (forceUpload) {
const isMinNumberOfFilesReached = this.checkMinNumberOfFiles()
if (!isMinNumberOfFilesReached) {
return Promise.reject('Minimum number of files has not been reached')
return Promise.reject(new Error('Minimum number of files has not been reached'))
}
const beforeUpload = Promise.resolve()
.then(() => this.opts.onBeforeUpload(this.state.files))
return beforeUpload.catch((err) => {
this.info(err, 'error', 5000)
return Promise.reject(`onBeforeUpload: ${err}`)
this.info(err.message, 'error', 5000)
return Promise.reject(new Error(`onBeforeUpload: ${err}`))
}).then(() => {
const waitingFileIDs = []
Object.keys(this.state.files).forEach((fileID) => {

View file

@ -567,7 +567,7 @@ describe('src/Core', () => {
throw new Error('File was allowed through')
})
.catch(e => {
expect(e).toEqual('File not allowed')
expect(e.message).toEqual('File not allowed')
})
})
})
@ -720,7 +720,7 @@ describe('src/Core', () => {
name: 'foo2.jpg',
type: 'image/jpg',
data: utils.dataURItoFile(sampleImageDataURI, {})
})).rejects.toMatch('File not allowed').then(() => {
})).rejects.toMatchObject({ message: 'File not allowed' }).then(() => {
expect(core.state.info.message).toEqual('You can only upload 1 file')
})
})
@ -740,7 +740,7 @@ describe('src/Core', () => {
name: 'foo2.jpg',
type: 'image/jpg',
data: utils.dataURItoFile(sampleImageDataURI, {})
})).rejects.toMatch('File not allowed').then(() => {
})).rejects.toMatchObject({ message: 'File not allowed' }).then(() => {
expect(core.state.info.message).toEqual('You can only upload: image/gif, image/png')
})
})
@ -758,7 +758,7 @@ describe('src/Core', () => {
name: 'foo.jpg',
type: 'image/jpg',
data: utils.dataURItoFile(sampleImageDataURI, {})
})).rejects.toMatch('File not allowed').then(() => {
})).rejects.toMatchObject({ message: 'File not allowed' }).then(() => {
expect(core.state.info.message).toEqual('This file exceeds maximum allowed size of 1.2 KB')
})
})

View file

@ -367,7 +367,7 @@ function copyToClipboard (textToCopy, fallbackString) {
const magicCopyFailed = (err) => {
document.body.removeChild(textArea)
window.prompt(fallbackString, textToCopy)
return reject('Oops, unable to copy displayed fallback prompt: ' + err)
return reject(new Error('Oops, unable to copy displayed fallback prompt: ' + err))
}
try {

View file

@ -143,7 +143,7 @@ module.exports = class Tus10 extends Plugin {
optsTus.onError = (err) => {
this.core.log(err)
this.core.emit('core:upload-error', file.id, err)
reject('Failed because: ' + err)
reject(new Error('Failed because: ' + err))
}
optsTus.onProgress = (bytesUploaded, bytesTotal) => {

View file

@ -199,7 +199,7 @@ module.exports = class Webcam extends Plugin {
if (!this.webcamActive) {
clearInterval(countDown)
this.captureInProgress = false
return reject('Webcam is not active')
return reject(new Error('Webcam is not active'))
}
if (count > 0) {
@ -234,12 +234,12 @@ module.exports = class Webcam extends Plugin {
this.opts.onBeforeSnapshot().catch((err) => {
this.core.info(err, 'error', 5000)
return Promise.reject(`onBeforeSnapshot: ${err}`)
return Promise.reject(new Error(`onBeforeSnapshot: ${err}`))
}).then(() => {
const video = this.target.querySelector('.UppyWebcam-video')
if (!video) {
this.captureInProgress = false
return Promise.reject('No video element found, likely due to the Webcam tab being closed.')
return Promise.reject(new Error('No video element found, likely due to the Webcam tab being closed.'))
}
const image = this.webcam.getImage(video, opts)