diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2025-02-15 15:40:56 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2025-02-15 15:40:56 +0000 |
commit | 24b07f9166a3c100121143f0d59d3d5d167a82ad (patch) | |
tree | ba6a416cc7bb6f685e0d5632222e49a822928d6c /src/player.lisp | |
parent | 4ded442dd47babd9f3c0163ffe274f92d0e153a7 (diff) | |
download | cantedraw-24b07f9166a3c100121143f0d59d3d5d167a82ad.tar.gz cantedraw-24b07f9166a3c100121143f0d59d3d5d167a82ad.tar.bz2 cantedraw-24b07f9166a3c100121143f0d59d3d5d167a82ad.zip |
Introduce custom errors for player handling
Custom errors for malformed/nonexistent player IDs as well as a player
not having a high enough balance for the money requested.
This will allow me to encode more information in the errors, for
callers to use. I'm generally of the opinion that callers should be
checking error conditions before calling these functions, but per
Murphy's law it would be nice to have more information in the error
message.
Diffstat (limited to 'src/player.lisp')
-rw-r--r-- | src/player.lisp | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/player.lisp b/src/player.lisp index 7979b25..a5b1625 100644 --- a/src/player.lisp +++ b/src/player.lisp @@ -28,6 +28,25 @@ (deftype players () 'list) +(define-condition error-player-nonexistent (error) + ((id :initarg :id :reader id)) + (:report + (lambda (err stream) + (format stream "Player [~a] is non-existent or malformed" + (id err))))) + +(define-condition error-player-broke (error) + ((id :initarg :id :reader id) + (balance :initarg :balance :reader balance) + (required :initarg :required :reader required)) + (:report + (lambda (err stream) + (format stream "Player [~a] has balance $~a but $~a requested." + (id err) + (balance err) + (required err))))) + + (fn player-id (player) (-> (player) fixnum) (car player)) @@ -50,10 +69,13 @@ (let ((p (assoc id players))) (cond ((not (typep p 'player)) - (error "Player [~a] does not exist.")) + (error 'error-player-nonexistent + :id id)) ((not (player-can-bet? amount p)) - (error "Player [~a] has $~a but needs to pay ~a." - id (player-balance p) amount)) + (error 'error-player-broke + :id id + :balance (player-balance p) + :required amount)) (t (destructuring-bind (id balance cards) p (setf (cdr (assoc id players)) |