fix(attachments): Fix error handling when saving uploads to disk

- Fixes sentry 11705
This commit is contained in:
Juan Tejada 2016-03-22 18:56:52 -07:00
parent f3250e5395
commit 7f73e0455f

View file

@ -46,9 +46,7 @@ class FileUploadStore extends NylasStore
return unless change.objectClass is Message.name and change.type is 'unpersist' return unless change.objectClass is Message.name and change.type is 'unpersist'
change.objects.forEach (message) => change.objects.forEach (message) =>
messageDir = path.join(UPLOAD_DIR, message.clientId) @_deleteUploadsForClientId(message.clientId)
for upload in message.uploads
@_deleteUpload(upload)
_onSelectAttachment: ({messageClientId}) -> _onSelectAttachment: ({messageClientId}) ->
@_verifyId(messageClientId) @_verifyId(messageClientId)
@ -75,11 +73,10 @@ class FileUploadStore extends NylasStore
_onRemoveAttachment: (upload) -> _onRemoveAttachment: (upload) ->
return Promise.resolve() unless upload return Promise.resolve() unless upload
@_applySessionChanges upload.messageClientId, (uploads) -> @_applySessionChanges upload.messageClientId, (uploads) ->
_.reject(uploads, _.matcher({id: upload.id})) _.reject(uploads, _.matcher({id: upload.id}))
@_deleteUpload(upload)
@_deleteUpload(upload).catch(@_onAttachFileError) .catch(@_onAttachFileError)
_onAttachFileError: (error) -> _onAttachFileError: (error) ->
NylasEnv.showErrorDialog(error.message) NylasEnv.showErrorDialog(error.message)
@ -126,13 +123,17 @@ class FileUploadStore extends NylasStore
readStream.pipe(writeStream) readStream.pipe(writeStream)
_deleteUpload: (upload) => _deleteUpload: (upload) =>
# Delete the upload file
fs.unlinkAsync(upload.targetPath).then -> fs.unlinkAsync(upload.targetPath).then ->
# Delete the containing folder
fs.rmdirAsync(upload.targetDir).then -> fs.rmdirAsync(upload.targetDir).then ->
# Try to remove the directory for the associated message if this was the
# last upload
fs.rmdir path.join(UPLOAD_DIR, upload.messageClientId), (err) -> fs.rmdir path.join(UPLOAD_DIR, upload.messageClientId), (err) ->
# Will fail if it's not empty, which is fine. # Will fail if it's not empty, which is fine.
Promise.resolve(upload) Promise.resolve(upload)
.catch -> .catch (err) ->
Promise.reject(new Error("Error cleaning up file #{upload.filename}")) Promise.reject(new Error("Error deleting file upload #{upload.filename}:\n\n#{err.message}"))
_deleteUploadsForClientId: (messageClientId) => _deleteUploadsForClientId: (messageClientId) =>
rimraf = require('rimraf') rimraf = require('rimraf')