             stream = db.saveAttachment req.doc, fileData, (err) ->
                 if err
                     console.log "[Attachment] err: " + JSON.stringify err
-                    next new Error err.error
-                else
+                    form.emit 'error', new Error err.error
                     # We end the request because we expect to have only one
                     # file.
-                    res.send 201, success: true
-                    next()
 
             part.pipe stream
 


    form.on 'close', ->
        log.info 'attachement form fully parsed'


# GET /data/:id/attachments/:name

log =  require('printit')
    date: true
    prefix: 'attachment'

db = require('../helpers/db_connect_helper').db_connect()
deleteFiles = require('../helpers/utils').deleteFiles
