mirror of
https://github.com/status-im/status-react.git
synced 2025-01-11 19:44:47 +00:00
[#6228]: API: requesting to connect to a node that is not available crashes the application
Signed-off-by: Aleksandr Pantiukhov <alwxndr@gmail.com>
This commit is contained in:
parent
df9a59e255
commit
76a724a2e7
@ -39,6 +39,7 @@
|
|||||||
[status-im.ui.screens.navigation :as navigation]
|
[status-im.ui.screens.navigation :as navigation]
|
||||||
[status-im.utils.fx :as fx]
|
[status-im.utils.fx :as fx]
|
||||||
[status-im.utils.handlers :as handlers]
|
[status-im.utils.handlers :as handlers]
|
||||||
|
[status-im.utils.utils :as utils]
|
||||||
[taoensso.timbre :as log]))
|
[taoensso.timbre :as log]))
|
||||||
|
|
||||||
;; init module
|
;; init module
|
||||||
@ -336,8 +337,10 @@
|
|||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:network.ui/connect-network-pressed
|
:network.ui/connect-network-pressed
|
||||||
(fn [cofx [_ network]]
|
(fn [cofx [_ network-id]]
|
||||||
(network/connect cofx {:network network})))
|
(network/connect cofx {:network-id network-id
|
||||||
|
:on-failure (fn [{:keys [reason]} _]
|
||||||
|
(utils/show-popup "Error" (str reason)))})))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
:network.ui/delete-network-pressed
|
:network.ui/delete-network-pressed
|
||||||
|
@ -40,12 +40,15 @@
|
|||||||
[{:keys [cofx data]}]
|
[{:keys [cofx data]}]
|
||||||
(network/connect
|
(network/connect
|
||||||
cofx
|
cofx
|
||||||
{:network (:id data)
|
{:network-id (:id data)
|
||||||
:on-success (fn [network _]
|
:on-success (fn [{:keys [network-id client-version]} _]
|
||||||
{:dev-server/respond [200 {:message "Network has been connected."
|
{:dev-server/respond [200 {:message "Network has been connected."
|
||||||
:network-id network}]})
|
:network-id network-id
|
||||||
:on-failure (fn [_ _]
|
:client-version client-version}]})
|
||||||
{:dev-server/respond [400 {:message "The network id you provided doesn't exist."}]})}))
|
:on-failure (fn [{:keys [network-id reason]} _]
|
||||||
|
{:dev-server/respond [400 {:message "Cannot connect the network."
|
||||||
|
:network-id network-id
|
||||||
|
:reason reason}]})}))
|
||||||
|
|
||||||
(defmethod process-request! [:DELETE "network" nil]
|
(defmethod process-request! [:DELETE "network" nil]
|
||||||
[{:keys [cofx data]}]
|
[{:keys [cofx data]}]
|
||||||
|
@ -9,7 +9,10 @@
|
|||||||
[status-im.utils.ethereum.core :as ethereum]
|
[status-im.utils.ethereum.core :as ethereum]
|
||||||
[status-im.ui.screens.navigation :as navigation]
|
[status-im.ui.screens.navigation :as navigation]
|
||||||
[status-im.fleet.core :as fleet-core]
|
[status-im.fleet.core :as fleet-core]
|
||||||
[status-im.utils.fx :as fx]))
|
[status-im.utils.fx :as fx]
|
||||||
|
[status-im.utils.handlers :as handlers]
|
||||||
|
[status-im.utils.http :as http]
|
||||||
|
[status-im.utils.types :as types]))
|
||||||
|
|
||||||
(def url-regex
|
(def url-regex
|
||||||
#"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)")
|
#"https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6})?\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)")
|
||||||
@ -26,6 +29,11 @@
|
|||||||
{:value value
|
{:value value
|
||||||
:error (string/blank? value)})
|
:error (string/blank? value)})
|
||||||
|
|
||||||
|
(defn validate-network-id [{:keys [value]}]
|
||||||
|
{:value value
|
||||||
|
:error (and (not (string/blank? value))
|
||||||
|
(= (int value) 0))})
|
||||||
|
|
||||||
(defn validate-url [{:keys [value]}]
|
(defn validate-url [{:keys [value]}]
|
||||||
{:value value
|
{:value value
|
||||||
:error (not (valid-rpc-url? value))})
|
:error (not (valid-rpc-url? value))})
|
||||||
@ -34,7 +42,8 @@
|
|||||||
(-> manage
|
(-> manage
|
||||||
(update :url validate-url)
|
(update :url validate-url)
|
||||||
(update :name validate-string)
|
(update :name validate-string)
|
||||||
(update :chain validate-string)))
|
(update :chain validate-string)
|
||||||
|
(update :network-id validate-network-id)))
|
||||||
|
|
||||||
(defn valid-manage? [manage]
|
(defn valid-manage? [manage]
|
||||||
(->> (validate-manage manage)
|
(->> (validate-manage manage)
|
||||||
@ -97,24 +106,60 @@
|
|||||||
{:db (assoc db :networks/manage (validate-manage default-manage))
|
{:db (assoc db :networks/manage (validate-manage default-manage))
|
||||||
:dispatch [:navigate-to :edit-network]})
|
:dispatch [:navigate-to :edit-network]})
|
||||||
|
|
||||||
(fx/defn connect [{:keys [db now] :as cofx} {:keys [network on-success on-failure]}]
|
(fx/defn connect-success [{:keys [db now] :as cofx} {:keys [network-id on-success client-version]}]
|
||||||
(if (get-in db [:account/account :networks network])
|
|
||||||
(let [current-network (get-in db [:account/account :networks (:network db)])]
|
(let [current-network (get-in db [:account/account :networks (:network db)])]
|
||||||
(if (ethereum/network-with-upstream-rpc? current-network)
|
(if (ethereum/network-with-upstream-rpc? current-network)
|
||||||
(fx/merge cofx
|
(fx/merge cofx
|
||||||
#(action-handler on-success network %)
|
#(action-handler on-success {:network-id network-id :client-version client-version} %)
|
||||||
(accounts.update/account-update
|
(accounts.update/account-update
|
||||||
{:network network
|
{:network network-id
|
||||||
:last-updated now}
|
:last-updated now}
|
||||||
{:success-event [:accounts.update.callback/save-settings-success]}))
|
{}))
|
||||||
(fx/merge cofx
|
(fx/merge cofx
|
||||||
{:ui/show-confirmation {:title (i18n/label :t/close-app-title)
|
{:ui/show-confirmation {:title (i18n/label :t/close-app-title)
|
||||||
:content (i18n/label :t/close-app-content)
|
:content (i18n/label :t/close-app-content)
|
||||||
:confirm-button-text (i18n/label :t/close-app-button)
|
:confirm-button-text (i18n/label :t/close-app-button)
|
||||||
:on-accept #(re-frame/dispatch [:network.ui/save-non-rpc-network-pressed network])
|
:on-accept #(re-frame/dispatch [:network.ui/save-non-rpc-network-pressed network-id])
|
||||||
:on-cancel nil}}
|
:on-cancel nil}}
|
||||||
#(action-handler on-success network %))))
|
#(action-handler on-success {:network-id network-id :client-version client-version} %)))))
|
||||||
(action-handler on-failure)))
|
|
||||||
|
(defn connect-failure [{:keys [network-id on-failure reason]}]
|
||||||
|
(action-handler on-failure
|
||||||
|
{:network-id network-id :reason reason}
|
||||||
|
nil))
|
||||||
|
|
||||||
|
(fx/defn connect [{:keys [db]} {:keys [network-id on-success on-failure]}]
|
||||||
|
(if-let [config (get-in db [:account/account :networks network-id :config])]
|
||||||
|
{:http-post {:url (get-in config [:UpstreamConfig :URL])
|
||||||
|
:data (types/clj->json {:jsonrpc "2.0"
|
||||||
|
:method "web3_clientVersion"
|
||||||
|
:id 1})
|
||||||
|
:opts {:headers {"Content-Type" "application/json"}}
|
||||||
|
:success-event-creator (fn [{:keys [response-body]}]
|
||||||
|
(let [client-version (:result (http/parse-payload response-body))]
|
||||||
|
[::connect-success {:network-id network-id
|
||||||
|
:on-success on-success
|
||||||
|
:client-version client-version}]))
|
||||||
|
:failure-event-creator (fn [{:keys [response-body status-code]}]
|
||||||
|
(let [reason (if status-code
|
||||||
|
(str "Got a wrong status code: " status-code)
|
||||||
|
(str response-body))]
|
||||||
|
[::connect-failure {:network-id network-id
|
||||||
|
:on-failure on-failure
|
||||||
|
:reason reason}]))}}
|
||||||
|
(connect-failure {:network-id network-id
|
||||||
|
:on-failure on-failure
|
||||||
|
:reason "A network with the specified id doesn't exist"})))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
::connect-success
|
||||||
|
(fn [cofx [_ data]]
|
||||||
|
(connect-success cofx data)))
|
||||||
|
|
||||||
|
(handlers/register-handler-fx
|
||||||
|
::connect-failure
|
||||||
|
(fn [_ [_ data]]
|
||||||
|
(connect-failure data)))
|
||||||
|
|
||||||
(fx/defn delete
|
(fx/defn delete
|
||||||
[{{:account/keys [account]} :db :as cofx} {:keys [network on-success on-failure]}]
|
[{{:account/keys [account]} :db :as cofx} {:keys [network on-success on-failure]}]
|
||||||
|
@ -33,9 +33,9 @@
|
|||||||
{:http-post {:url graphql-url
|
{:http-post {:url graphql-url
|
||||||
:data (types/clj->json {:query (graphql-query (ethereum/naked-address address))})
|
:data (types/clj->json {:query (graphql-query (ethereum/naked-address address))})
|
||||||
:opts {:headers {"Content-Type" "application/json"}}
|
:opts {:headers {"Content-Type" "application/json"}}
|
||||||
:success-event-creator (fn [o]
|
:success-event-creator (fn [{:keys [response-body]}]
|
||||||
[:store-collectibles superrare
|
[:store-collectibles superrare
|
||||||
(get-in (http/parse-payload o) [:data :collectiblesByOwner :collectibles])])
|
(get-in (http/parse-payload response-body) [:data :collectiblesByOwner :collectibles])])
|
||||||
:failure-event-creator (fn [o]
|
:failure-event-creator (fn [{:keys [response-body]}]
|
||||||
[:load-collectibles-failure (http/parse-payload o)])
|
[:load-collectibles-failure (http/parse-payload response-body)])
|
||||||
:timeout-ms 10000}})
|
:timeout-ms 10000}})
|
||||||
|
@ -31,18 +31,22 @@
|
|||||||
ok?' (if valid-response?
|
ok?' (if valid-response?
|
||||||
(and ok? (valid-response? response))
|
(and ok? (valid-response? response))
|
||||||
ok?)]
|
ok?)]
|
||||||
[response-body ok?']))))))
|
{:response-body response-body
|
||||||
(.then (fn [[response ok?]]
|
:ok? ok?'
|
||||||
|
:status-text (.-statusText response)
|
||||||
|
:status-code (.-status response)}))))))
|
||||||
|
(.then (fn [{:keys [ok?] :as data}]
|
||||||
(cond
|
(cond
|
||||||
(and on-success ok?)
|
(and on-success ok?)
|
||||||
(on-success response)
|
(on-success data)
|
||||||
|
|
||||||
(and on-error (not ok?))
|
(and on-error (not ok?))
|
||||||
(on-error response)
|
(on-error data)
|
||||||
|
|
||||||
:else false)))
|
:else false)))
|
||||||
(.catch (or on-error
|
(.catch (fn [error]
|
||||||
(fn [error]
|
(if on-error
|
||||||
|
(on-error {:response-body error})
|
||||||
(utils/show-popup "Error" (str error))))))))
|
(utils/show-popup "Error" (str error))))))))
|
||||||
|
|
||||||
(defn- headers [response]
|
(defn- headers [response]
|
||||||
|
@ -85,6 +85,8 @@
|
|||||||
:name {:value ""
|
:name {:value ""
|
||||||
:error true}
|
:error true}
|
||||||
:chain {:value "mainnet"
|
:chain {:value "mainnet"
|
||||||
|
:error false}
|
||||||
|
:network-id {:value nil
|
||||||
:error false}}}}
|
:error false}}}}
|
||||||
(model/set-input {:db {:networks/manage {:url {:value "something"
|
(model/set-input {:db {:networks/manage {:url {:value "something"
|
||||||
:error true}
|
:error true}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user