From 19423bf909649d0a2bad763c3604064707183473 Mon Sep 17 00:00:00 2001 From: lilia Date: Thu, 1 Oct 2015 18:21:20 -0700 Subject: [PATCH] Abstract message error handling // FREEBIE --- js/background.js | 8 +----- js/models/messages.js | 58 +++++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/js/background.js b/js/background.js index b080145e9..af0c7c8f3 100644 --- a/js/background.js +++ b/js/background.js @@ -198,13 +198,7 @@ if (ev.proto) { var envelope = ev.proto; var message = initIncomingMessage(envelope.source, envelope.timestamp.toNumber()); - var attributes = {}; - if (e.name === 'IncomingIdentityKeyError') { - attributes = { errors : [e] }; - } else if (e.message !== 'Bad MAC') { - attributes = { errors : [ _.pick(e, ['name', 'message'])]}; - } - message.save(attributes).then(function() { + message.saveErrors(e).then(function() { ConversationController.findOrCreatePrivateById(message.get('conversationId')).then(function(conversation) { conversation.save({ active_at: Date.now(), diff --git a/js/models/messages.js b/js/models/messages.js index c31c40484..dfcade19a 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -142,49 +142,43 @@ return promise.then(function() { this.save({sent: true}); }.bind(this)).catch(function(errors) { - if (!(errors instanceof Array)) { - errors = [errors]; - } - errors.forEach(function(e) { - console.log(e); - console.log(e.reason, e.stack); - }); - this.save({ - sent: true, - errors : errors.map(function(e) { - if (e.constructor === Error) { - return _.pick(e, 'name', 'message', 'code', 'number', 'reason'); - } - return e; - }) - }); + this.set({sent: true}); + this.saveErrors(errors); }.bind(this)); }, + saveErrors: function(errors) { + if (!(errors instanceof Array)) { + errors = [errors]; + } + errors.forEach(function(e) { + console.log(e); + console.log(e.reason, e.stack); + }); + return this.save({ + errors : errors.map(function(e) { + if (e.constructor === Error) { + return _.pick(e, 'name', 'message', 'code', 'number', 'reason'); + } + return e; + }) + }); + }, + resolveConflict: function(number) { var error = this.getKeyConflict(number); if (error) { var promise = new textsecure.ReplayableError(error).replay(); if (this.isIncoming()) { - promise.then(function(dataMessage) { + promise = promise.then(function(dataMessage) { this.handleDataMessage(dataMessage); - this.save('errors', []); - }.bind(this)).catch(function(e) { - //this.save('errors', [_.pick(e, ['name', 'message'])]); - var errors = this.get('errors').concat( - _.pick(e, ['name', 'message']) - ); - this.save('errors', errors); - }.bind(this)); - } else { - promise.then(function() { - var errors = _.reject(this.get('errors'), function(e) { - return e.name === 'OutgoingIdentityKeyError' && - e.number === number; - }); - this.save({sent: true, errors: errors}); }.bind(this)); } + promise.then(function() { + this.save('errors', []); + }.bind(this)).catch(function(e) { + this.saveErrors(e); + }.bind(this)); return promise; } },