From 992a8e9aef2b1f002f651ee6dcb0f1a2591b271b Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 3 Feb 2021 23:27:47 +0100 Subject: [PATCH] :sparkles: Improve posible race condition handling on user registration. --- backend/src/app/rpc/mutations/profile.clj | 27 +++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index 9f852ad3f..113f2c344 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -170,15 +170,24 @@ active? (if demo? true is-active) props (db/tjson (or props {})) password (derive-password password)] - (-> (db/insert! conn :profile - {:id id - :fullname fullname - :email (str/lower email) - :password password - :props props - :is-active active? - :is-demo demo?}) - (update :props db/decode-transit-pgobject)))) + (try + (-> (db/insert! conn :profile + {:id id + :fullname fullname + :email (str/lower email) + :password password + :props props + :is-active active? + :is-demo demo?}) + (update :props db/decode-transit-pgobject)) + (catch org.postgresql.util.PSQLException e + (let [state (.getSQLState e)] + (if (not= state "23505") + (throw e) + (ex/raise :type :validation + :code :email-already-exists + :cause e))))))) + (defn- create-profile-relations [conn profile]