From 1dd31d7091501bfb8f1bfa70b6b01d8f58163a49 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 27 Dec 2019 17:55:42 +0100 Subject: [PATCH] Make reading IDs safe and provide useful debug messages --- src/Hanafuda/Message.hs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Hanafuda/Message.hs b/src/Hanafuda/Message.hs index 53d369b..2f13b60 100644 --- a/src/Hanafuda/Message.hs +++ b/src/Hanafuda/Message.hs @@ -37,10 +37,13 @@ import qualified Hanafuda.KoiKoi as KoiKoi ( , Score, Scores, Source(..), Step(..), Yaku(..) ) import Hanafuda.Player (Player(..), Players(..)) +import Text.Read (readMaybe) instance IDType a => FromJSON (ID a) where - parseJSON = withText decoding (return . read . Text.unpack) - where decoding = let Prefix p = (prefix :: Prefix a) in p ++ "ID" + parseJSON = withText decoding (safeRead . Text.unpack) + where + decoding = let Prefix p = (prefix :: Prefix a) in p ++ "ID" + safeRead s = maybe (fail $ "Not an ID: '" ++ s ++ "'") return $ readMaybe s instance IDType a => FromJSONKey (ID a) where fromJSONKey = FromJSONKeyText (read . Text.unpack) instance IDType a => ToJSON (ID a) where