Skip to content

Commit

Permalink
handle UI exceptions gracefully, prevent duplicate account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ducki2p committed Jan 3, 2011
1 parent 6f29cd6 commit 10f62ab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/i2conomy/mint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
(throw (IllegalArgumentException.
(str "account " name " does not exist"))))

(defn- throw-duplicate-account-exception [name]
(throw (IllegalArgumentException.
(str "account " name " already exists"))))

(defn- throw-nonexisting-currency-exception [name]
(throw (IllegalArgumentException.
(str "currency " name " does not exist"))))
Expand Down Expand Up @@ -44,9 +48,11 @@
"Creates a new account"
[name]
(dosync
(let [now (java.util.Date.)
account (Account. name (ref (sorted-map)))]
(alter accounts assoc name account))))
(if (not (account-exists? name))
(let [now (java.util.Date.)
account (Account. name (ref (sorted-map)))]
(alter accounts assoc name account))
(throw-duplicate-account-exception name))))

(defn- add-or-set
"Returns the sum of x and y. If x is nil it returns y."
Expand Down
22 changes: 15 additions & 7 deletions src/i2conomy/wallet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
[:body
[:h1 "I2Conomy"]
[:p "Account: " (session-get :account "Unknown")]
(when-let [flash (flash-get :error)]
[:p.error "Error: " flash])
(when-let [flash (flash-get :message)]
[:p "Message: " flash])
[:p.message "Message: " flash])
content])))

(defn view-create-account-input []
Expand All @@ -36,7 +38,7 @@
[:legend "Create Account"]
[:div
[:label {:for "account"} "Account: "]
[:input {:type "text" :id "account" :name "account" :value "duck"}]]]
[:input {:type "text" :id "account" :name "account"}]]]
[:div.button
[:input {:type "submit" :value "create"}]]]))

Expand Down Expand Up @@ -83,24 +85,30 @@
(GET "/" []
(view-layout
(view-create-account-input)
(view-payment-input)
(when-let [account (session-get :account)]
(html
(view-payment-input)
(view-balances (mint/balances account))
(view-history (mint/history account))))))

(POST "/create-account" [account]
(do
(try
(mint/create-account account)
(session-put! :account account)
(flash-put! :message (str "Account " account " created"))
(redirect "/")))
(catch IllegalArgumentException e
(flash-put! :error (.getMessage e))))
(redirect "/"))

(POST "/pay" [from to currency amount memo]
(do
(try
(mint/pay from to currency (Integer/parseInt amount) memo)
(flash-put! :message (str "Account " to " paid"))
(redirect "/")))
(catch NumberFormatException _
(flash-put! :error "Invalid amount"))
(catch IllegalArgumentException e
(flash-put! :error (.getMessage e))))
(redirect "/"))

(ANY "/*" [path]
(redirect "/")))
Expand Down
7 changes: 7 additions & 0 deletions test/i2conomy/test/mint.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
(is (= 0 (count @accounts)))
(is (= 0 (count @transfers)))))

(deftest create-duplicate-account
(do
(reset-all!)
(create-account "alice")
(is (thrown-with-msg? IllegalArgumentException #"account alice already exist"
(create-account "alice")))))

(deftest single-payment
(do
(reset-all!)
Expand Down

0 comments on commit 10f62ab

Please sign in to comment.