Improve error handlling
This commit is contained in:
parent
b07c813b77
commit
9371746887
2 changed files with 20 additions and 9 deletions
|
@ -4,31 +4,42 @@
|
||||||
((gm
|
((gm
|
||||||
:initform (make-instance 'game-manager))))
|
:initform (make-instance 'game-manager))))
|
||||||
|
|
||||||
|
|
||||||
|
(define-condition bad-request (error)
|
||||||
|
((msg
|
||||||
|
:reader bad-request-msg
|
||||||
|
:initarg :msg
|
||||||
|
:initform "Unkown error occurs"))
|
||||||
|
(:report (lambda (condition stream) (format stream "Bad Request: ~a" (bad-request-msg condition)))))
|
||||||
|
|
||||||
|
|
||||||
;;; Parse the request and return it as a plist
|
;;; Parse the request and return it as a plist
|
||||||
|
;;; TODO Check game using game-manager (create the right error condition in gm)
|
||||||
(defun parse-request (request)
|
(defun parse-request (request)
|
||||||
(flet ((normalizer (key) (string-upcase key)))
|
(flet ((normalizer (key) (string-upcase key)))
|
||||||
(let* ((p-request (parse request :normalize-all t :keyword-normalizer #'normalizer ))
|
(let* ((p-request (parse request :normalize-all t :keyword-normalizer #'normalizer ))
|
||||||
(type (getf p-request :type :not-found)))
|
(type (getf p-request :type :not-found)))
|
||||||
(cond
|
(cond
|
||||||
((eq type :not-found)
|
((eq type :not-found)
|
||||||
(error "Invalid request: Bad request type"))
|
(error 'bad-request :msg "No json \"type\" field provided"))
|
||||||
((equal type "update")
|
((equal type "update")
|
||||||
(progn
|
(progn
|
||||||
(unless (getf p-request :game-id) (error "Invalid request: No game id"))
|
(unless (getf p-request :game-id) (error 'bad-request :msg "No json \"game-id\" field provided"))
|
||||||
(let ((dir (getf p-request :direction :not-found)))
|
(let ((dir (getf p-request :direction :not-found)))
|
||||||
(when (eq :not-found dir) (error "Invalid request: No snake direction provided"))
|
(when (eq :not-found dir) (error 'bad-request :msg "No json \"direction\" field provided"))
|
||||||
(unless (or (equal "up" dir) (equal "down" dir) (equal "left" dir) (equal "right" dir) (eq nil dir)) (error "Invalid request: Bad direction"))
|
(unless (or (equal "up" dir) (equal "down" dir) (equal "left" dir) (equal "right" dir) (eq nil dir)) (error 'bad-request :msg "Bad \"direction\" field value"))
|
||||||
(cond
|
(cond
|
||||||
((equal dir "up") (setf (getf p-request :direction) :up))
|
((equal dir "up") (setf (getf p-request :direction) :up))
|
||||||
((equal dir "down") (setf (getf p-request :direction) :down))
|
((equal dir "down") (setf (getf p-request :direction) :down))
|
||||||
((equal dir "left") (setf (getf p-request :direction) :left))
|
((equal dir "left") (setf (getf p-request :direction) :left))
|
||||||
((equal dir "right") (setf (getf p-request :direction) :right))))))
|
((equal dir "right") (setf (getf p-request :direction) :right))))))
|
||||||
((not (equal type "new-game"))
|
((not (equal type "new-game"))
|
||||||
(error "Invalid request: Unknow request type")))
|
(error 'bad-request :msg "Unknown request type")))
|
||||||
p-request)))
|
p-request)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(defmethod handle-new-game ((api api) data)
|
(defmethod handle-new-game ((api api) data)
|
||||||
(with-slots (gm) api
|
(with-slots (gm) api
|
||||||
(let* ((game-id (create-game gm)))
|
(let* ((game-id (create-game gm)))
|
||||||
|
|
|
@ -20,9 +20,9 @@
|
||||||
|
|
||||||
|
|
||||||
;;; The server :D
|
;;; The server :D
|
||||||
(defun start (port)
|
(defun start (interface port)
|
||||||
(format t "Server start !~%")
|
(format t "Server started!~%")
|
||||||
(let ((socket (usocket:socket-listen "127.0.0.1" port)))
|
(let ((socket (usocket:socket-listen interface port)))
|
||||||
(unwind-protect ; To be sure to close server socket
|
(unwind-protect ; To be sure to close server socket
|
||||||
(loop
|
(loop
|
||||||
(handle-client (usocket:socket-accept socket :element-type 'character)))
|
(handle-client (usocket:socket-accept socket :element-type 'character)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue