From 166b7a63b1f958af9cf90581a53d22c5177167db Mon Sep 17 00:00:00 2001 From: yenda Date: Tue, 24 Sep 2019 18:45:33 +0200 Subject: [PATCH] throw errors on dangerous behaviours - if multiaccount settings are saved on top of an empty map or nil, this means something went wrong, the state of the app is unstable, and actually saving will result in loss of data. It should never happen, but if it does, throw and error and abort. - sometimes two fxs are merged when they shouldn't, this is caused by bugs and should never happen, but if it does, throw an error with arguments for both effects to help localize the error --- src/status_im/multiaccounts/update/core.cljs | 31 +++++++++++++------- src/status_im/utils/fx.cljs | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/status_im/multiaccounts/update/core.cljs b/src/status_im/multiaccounts/update/core.cljs index 82ccd6189ad3..83e2f246b14f 100644 --- a/src/status_im/multiaccounts/update/core.cljs +++ b/src/status_im/multiaccounts/update/core.cljs @@ -5,7 +5,8 @@ [status-im.transport.message.contact :as message.contact] [status-im.transport.message.protocol :as protocol] [status-im.utils.fx :as fx] - [status-im.utils.types :as types])) + [status-im.utils.types :as types] + [taoensso.timbre :as log])) (fx/defn multiaccount-update-message [{:keys [db] :as cofx}] (let [multiaccount (:multiaccount db) @@ -53,11 +54,15 @@ :params ["multiaccount" (types/serialize new-multiaccount)] :on-success on-success}]} {:keys [name photo-path prefered-name]} new-multiaccount-fields] - (if (or name photo-path prefered-name) - (fx/merge cofx - fx - (send-multiaccount-update)) - fx))) + (if (empty? current-multiaccount) + ;; NOTE: this should never happen, but if it does this is a critical error + ;; and it is better to crash than risk having an unstable state + (throw (js/Error. (str ":multiaccount is currently empty, which means something went wrong when trying to update it with: " fx))) + (if (or name photo-path prefered-name) + (fx/merge cofx + fx + (send-multiaccount-update)) + fx)))) (fx/defn clean-seed-phrase "A helper function that removes seed phrase from storage." @@ -72,8 +77,12 @@ settings {:keys [on-success] :or {on-success #()}}] (let [new-multiaccount (assoc multiaccount :settings settings)] - {:db (assoc db :multiaccount new-multiaccount) - ::json-rpc/call - [{:method "settings_saveConfig" - :params ["multiaccount" (types/serialize new-multiaccount)] - :on-success on-success}]})) + (if (empty? multiaccount) + ;; NOTE: this should never happen, but if it does this is a critical error + ;; and it is better to crash than risk having an unstable state + (throw (js/Error. (str ":multiaccount is currently empty, which means something went wrong when trying to call `update-settings` with: the following settings" settings))) + {:db (assoc db :multiaccount new-multiaccount) + ::json-rpc/call + [{:method "settings_saveConfig" + :params ["multiaccount" (types/serialize new-multiaccount)] + :on-success on-success}]}))) diff --git a/src/status_im/utils/fx.cljs b/src/status_im/utils/fx.cljs index f0792b96a592..c6473f06679d 100644 --- a/src/status_im/utils/fx.cljs +++ b/src/status_im/utils/fx.cljs @@ -33,7 +33,7 @@ (if (get merged-fx k) (if (mergeable-keys k) (update merged-fx k into v) - (do (log/error "Merging fx with common-key: " k v) + (do (log/error "Merging fx with common-key: " k v (get merged-fx k)) (reduced {:merging-fx-with-common-keys k}))) (assoc merged-fx k v)))) fx