Skip to content

Commit 67ca032

Browse files
committed
fix: apply code review suggestions
1 parent 5f658a4 commit 67ca032

File tree

3 files changed

+26
-42
lines changed

3 files changed

+26
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ cabal.project.local~
9999
.HTF/
100100
.ghc.environment.*
101101
/_build/
102+
.aider*

src/Cardano/Tools/DBQuery.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ instance ToJSON DBQueryLog where
5050
data Error
5151
= ParseError Text
5252
| QueryError Text
53-
deriving (Eq, Show)
53+
deriving (Eq, Show, Generic)
54+
55+
instance ToJSON Error where
56+
toJSON = \case
57+
ParseError msg -> object ["tag" .= ("ParseError" :: Text), "message" .= msg]
58+
QueryError msg -> object ["tag" .= ("QueryError" :: Text), "message" .= msg]
5459

5560
data Query
5661
= GetBlock StandardPoint
@@ -65,13 +70,13 @@ runQuery :: Tracer IO DBQueryLog -> FilePath -> FilePath -> Text -> IO ()
6570
runQuery tracer configurationFile databaseDirectory query =
6671
withDB configurationFile databaseDirectory (contramap DBLog tracer) $ \db ->
6772
runDBQuery db query >>= \case
68-
Err err -> print err
73+
Err err -> LBS.putStr $ Aeson.encode err
6974
Found result -> LBS.putStr result
7075

7176
runDBQuery :: DB -> Text -> IO (Result LBS.ByteString)
7277
runDBQuery db query = do
7378
case parseQuery query of
74-
Left _err -> pure $ Err (MalformedQuery query)
79+
Left err -> pure $ Err (MalformedQuery query)
7580
Right q ->
7681
case q of
7782
GetBlock point -> getBlock db point
@@ -96,4 +101,4 @@ parseQuery str =
96101
withPoint q point =
97102
maybe (Left $ ParseError $ "error parsing point: " <> point) (Right . q) $ parsePoint point
98103
withSlot q slot =
99-
maybe (Left $ ParseError $ "error parsing point: " <> slot) (Right . q) $ makeSlot slot
104+
maybe (Left $ ParseError $ "error parsing slot: " <> slot) (Right . q) $ makeSlot slot

src/Cardano/Tools/DBServer.hs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -74,58 +74,36 @@ webApp db req send =
7474
_ -> send responseNotFound
7575
where
7676
responseNotFound = responseLBS status404 [] ""
77+
responseBadRequest msg = responseLBS status400 [] msg
78+
responseOk content = responseLBS status200 [("content-type", "application/json")] content
7779

7880
handleGetSnapshots =
7981
listSnapshots db >>= \snapshotsPoints ->
80-
send $
81-
responseLBS
82-
status200
83-
[("content-type", "application/json")]
84-
(encode snapshotsPoints)
82+
send $ responseOk (encode snapshotsPoints)
8583

86-
handleGetHeader slot hash =
84+
handleWithPoint action slot hash =
8785
case makePoint slot hash of
88-
Nothing ->
89-
send $ responseLBS status400 [] "Malformed hash or slot"
86+
Nothing -> send $ responseBadRequest "Malformed hash or slot"
9087
Just point ->
91-
getHeader db point >>= \case
88+
action db point >>= \case
9289
Err NotFound -> send responseNotFound
93-
Err err -> send $ responseLBS status400 [] ("Bad query: " <> toBytestring err)
94-
Found header -> send $ responseLBS status200 [("content-type", "application/text")] (LHex.encode header)
90+
Err err -> send $ responseBadRequest ("Bad query: " <> toBytestring err)
91+
Found result -> send $ responseOk (LHex.encode result)
92+
93+
handleGetHeader slot hash = handleWithPoint getHeader slot hash
9594

9695
handleGetSnapshot slot =
9796
case makeSlot slot of
98-
Nothing -> send $ responseLBS status400 [] "Malformed slot"
97+
Nothing -> send $ responseBadRequest "Malformed slot"
9998
Just slot' ->
10099
getSnapshot db slot' >>= \case
101100
Err NotFound -> send responseNotFound
102-
Err err -> send $ responseLBS status400 [] ("Bad query: " <> toBytestring err)
103-
Found snapshot ->
104-
send $
105-
responseLBS
106-
status200
107-
[("content-type", "application/json")]
108-
(LHex.encode snapshot)
109-
110-
handleGetParent slot hash =
111-
case makePoint slot hash of
112-
Nothing ->
113-
send $ responseLBS status400 [] "Malformed hash or slot"
114-
Just point ->
115-
getParent db point >>= \case
116-
Err NotFound -> send responseNotFound
117-
Err err -> send $ responseLBS status400 [] ("Bad query: " <> toBytestring err)
118-
Found parent -> send $ responseLBS status200 [("content-type", "application/json")] (LHex.encode parent)
101+
Err err -> send $ responseBadRequest ("Bad query: " <> toBytestring err)
102+
Found snapshot -> send $ responseOk (LHex.encode snapshot)
119103

120-
handleGetBlock slot hash = do
121-
case makePoint slot hash of
122-
Nothing ->
123-
send $ responseLBS status400 [] "Malformed hash or slot"
124-
Just point ->
125-
getBlock db point >>= \case
126-
Err NotFound -> send responseNotFound
127-
Err err -> send $ responseLBS status400 [] ("Bad query: " <> toBytestring err)
128-
Found parent -> send $ responseLBS status200 [("content-type", "application/json")] (LHex.encode parent)
104+
handleGetParent slot hash = handleWithPoint getParent slot hash
105+
106+
handleGetBlock slot hash = handleWithPoint getBlock slot hash
129107

130108
-- * Tracing
131109

0 commit comments

Comments
 (0)