               @emit 'messageSent', m
               delete @_pending[message.reply_to]
           else
-            @emit 'error', message.error
+            @emit 'error', if message.error? then message.error else message
             # TODO: resend?
         else
           if message.type not in ["file_created", "file_shared", "file_unshared", "file_comment", "file_public", "file_comment_edited", "file_comment_deleted", "file_change", "file_deleted", "star_added", "star_removed"]


      else
        if message.reply_to
          if message.ok
            console.log "Message "+message.reply_to+" was sent"
            if @_pending[message.reply_to]
              m = @_pending[message.reply_to]

        @emit 'open'
        @connected = true

        # start pings
        @_pongTimeout = setInterval =>
          if not @connected then return

          @_send {"type": "ping"}
          if @_lastPong? and Date.now() - @_lastPong > 10000
            console.log "Last pong is too old: %d", (Date.now() - @_lastPong) / 1000
            @disconnect()
        , 5000

      @ws.on 'message', (data, flags) =>
        # flags.binary will be set if a binary data is received
        # flags.masked will be set if the data was masked
