aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <manzerberdes@gmx.com>2019-05-11 11:13:40 +0200
committerLoic Guegan <manzerberdes@gmx.com>2019-05-11 11:13:40 +0200
commit39cf5947d89280d7c346a2a8677b9ad68eb3bec1 (patch)
treec92c3eb2bc0e9378d001ba87248ba68ce3310c48
parent9371746887da325a902e9995755667054dfd31d1 (diff)
Improve error management
-rw-r--r--server/api/api.lisp33
1 files changed, 17 insertions, 16 deletions
diff --git a/server/api/api.lisp b/server/api/api.lisp
index 6c99ea8..7ccff24 100644
--- a/server/api/api.lisp
+++ b/server/api/api.lisp
@@ -51,8 +51,8 @@
(defmethod handle-update ((api api) data)
(with-slots (gm) api
(let* ((dir (getf data :direction))
- (game-id (getf data :game-id))
- (game (get-game gm game-id)))
+ (game-id (getf data :game-id))
+ (game (get-game gm game-id)))
(cond
((equal dir "up") (setf dir :up))
((equal dir "down") (setf dir :down))
@@ -66,21 +66,22 @@
(append (list :type "state") (dump gm game-id))))))
-;;; TODO: Improve error management
(defmethod handle-request ((api api) request)
- (flet ((handle-fun ()
- (let* ((data (parse-request request))
- (type (getf data :type)))
- (cond
- ((equal type "new-game") (handle-new-game api data))
- ((equal type "update") (handle-update api data))
- (t (format t "Unknow type"))))))
-
- (handler-case
- (handle-fun)
- (t (c)
- (format t "Got an exception: ~a~%" c)
- "Bad request"))))
+ ;; Catch request error and send it to the client
+ (handler-case
+ (let* ((data (parse-request request))
+ (type (getf data :type)))
+ (cond
+ ((equal type "new-game") (handle-new-game api data))
+ ((equal type "update") (handle-update api data))
+ (t (format t "Unknow type"))))
+ (error (condition) ; Send reason to the client
+ (let ((reason (make-array 0
+ :element-type 'character
+ :adjustable t
+ :fill-pointer 0)))
+ (format reason "~a~%" condition :escape nil)
+ reason))))