Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 6306f07

Browse files
author
Patrick Thomson
committed
Better story for handling JSON exceptions.
1 parent 6d599a5 commit 6306f07

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Data/Handle.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE GADTs #-}
1+
{-# LANGUAGE DeriveAnyClass, GADTs #-}
22

33
module Data.Handle
44
( Handle (..)
@@ -11,14 +11,15 @@ module Data.Handle
1111
, readBlobPairsFromHandle
1212
, readFromHandle
1313
, openFileForReading
14+
, InvalidJSONException (..)
1415
) where
1516

1617
import Prologue
1718

19+
import Control.Exception (throw)
1820
import Data.Aeson
1921
import qualified Data.ByteString.Lazy as BL
2022
import qualified Data.ByteString.Lazy.Char8 as BLC
21-
import System.Exit
2223
import qualified System.IO as IO
2324

2425
import Data.Blob
@@ -58,9 +59,14 @@ readPathsFromHandle (ReadHandle h) = liftIO $ fmap BLC.unpack . BLC.lines <$> BL
5859
readBlobPairsFromHandle :: MonadIO m => Handle 'IO.ReadMode -> m [BlobPair]
5960
readBlobPairsFromHandle = fmap blobs <$> readFromHandle
6061

62+
newtype InvalidJSONException = InvalidJSONException String
63+
deriving (Eq, Show, Exception)
64+
65+
-- | Read JSON-encoded data from a 'Handle'. Throws
66+
-- 'InvalidJSONException' on parse failure.
6167
readFromHandle :: (FromJSON a, MonadIO m) => Handle 'IO.ReadMode -> m a
6268
readFromHandle (ReadHandle h) = do
6369
input <- liftIO $ BL.hGetContents h
6470
case eitherDecode input of
65-
Left e -> liftIO (die (e <> ". Invalid input on " <> show h <> ", expecting JSON"))
71+
Left e -> throw (InvalidJSONException e)
6672
Right d -> pure d

test/Semantic/IO/Spec.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ spec = parallel $ do
8484

8585
it "throws on blank input" $ do
8686
h <- openFileForReading "test/fixtures/cli/blank.json"
87-
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
87+
readBlobPairsFromHandle h `shouldThrow` jsonException
8888

8989
it "throws if language field not given" $ do
9090
h <- openFileForReading "test/fixtures/cli/diff-no-language.json"
91-
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
91+
readBlobsFromHandle h `shouldThrow` jsonException
9292

9393
it "throws if null on before and after" $ do
9494
h <- openFileForReading "test/fixtures/cli/diff-null-both-sides.json"
95-
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
95+
readBlobPairsFromHandle h `shouldThrow` jsonException
9696

9797
describe "readBlobsFromHandle" $ do
9898
it "returns blobs for valid JSON encoded parse input" $ do
@@ -103,9 +103,13 @@ spec = parallel $ do
103103

104104
it "throws on blank input" $ do
105105
h <- openFileForReading "test/fixtures/cli/blank.json"
106-
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
106+
readBlobsFromHandle h `shouldThrow` jsonException
107107

108108
where blobsFromFilePath path = do
109109
h <- openFileForReading path
110110
blobs <- readBlobPairsFromHandle h
111111
pure blobs
112+
113+
jsonException :: Selector InvalidJSONException
114+
jsonException = const True
115+

0 commit comments

Comments
 (0)