Skip to content

Commit e669884

Browse files
authored
Log failed http requests (#2336)
fix #2315 --- <!-- Consider each and tick it off one way or the other --> * [x] CHANGELOG updated or not needed * [x] Documentation updated or not needed * [x] Haddocks updated or not needed * [x] No new TODOs introduced or explained herafter
2 parents fd1a53f + 391bc73 commit e669884

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

hydra-node/src/Hydra/API/APIServerLog.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ data APIServerLog
1818
, path :: PathInfo
1919
}
2020
| APITransactionSubmitted {submittedTxId :: String}
21+
| APIReturnedError {reason :: String}
2122
deriving stock (Eq, Show, Generic)
2223
deriving anyclass (ToJSON, FromJSON)
2324

hydra-node/src/Hydra/API/HTTPServer.hs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ httpApp tracer directChain env stateFile pparams getNodeState getCommitInfo getP
257257
>>= respond
258258
("POST", ["commit"]) ->
259259
consumeRequestBodyStrict request
260-
>>= handleDraftCommitUtxo env pparams directChain getCommitInfo
260+
>>= handleDraftCommitUtxo tracer env pparams directChain getCommitInfo
261261
>>= respond
262262
("DELETE", ["commits", _]) ->
263263
consumeRequestBodyStrict request
@@ -290,6 +290,7 @@ httpApp tracer directChain env stateFile pparams getNodeState getCommitInfo getP
290290
handleDraftCommitUtxo ::
291291
forall tx.
292292
IsChainState tx =>
293+
Tracer IO APIServerLog ->
293294
Environment ->
294295
PParams LedgerEra ->
295296
Chain tx IO ->
@@ -298,9 +299,14 @@ handleDraftCommitUtxo ::
298299
-- | Request body.
299300
LBS.ByteString ->
300301
IO Response
301-
handleDraftCommitUtxo env pparams directChain getCommitInfo body = do
302+
handleDraftCommitUtxo tracer env pparams directChain getCommitInfo body = do
302303
case Aeson.eitherDecode' body :: Either String (DraftCommitTxRequest tx) of
303-
Left err ->
304+
Left err -> do
305+
traceWith tracer $
306+
APIInvalidInput
307+
{ reason = "Failed to parse request to DraftCommitTxRequest: " <> show err
308+
, inputReceived = show body
309+
}
304310
pure $ responseLBS status400 jsonContent (Aeson.encode $ Aeson.String $ pack err)
305311
Right someCommitRequest ->
306312
getCommitInfo >>= \case
@@ -317,32 +323,50 @@ handleDraftCommitUtxo env pparams directChain getCommitInfo body = do
317323
deposit headId CommitBlueprintTx{blueprintTx, lookupUTxO = utxo} changeAddress
318324
SimpleCommitRequest{utxoToCommit} ->
319325
deposit headId CommitBlueprintTx{blueprintTx = txSpendingUTxO utxoToCommit, lookupUTxO = utxoToCommit} Nothing
320-
CannotCommit -> pure $ responseLBS status500 [] (Aeson.encode (FailedToDraftTxNotInitializing :: PostTxError tx))
326+
CannotCommit -> do
327+
traceWith tracer $
328+
APIInvalidInput
329+
{ reason = "CannotCommit: Hydra node is not in the Initialializing state."
330+
, inputReceived = show body
331+
}
332+
pure $ responseLBS status400 [] (Aeson.encode (FailedToDraftTxNotInitializing :: PostTxError tx))
321333
where
322334
deposit headId commitBlueprint changeAddress = do
323335
-- NOTE: Three times deposit period means we have one deposit period time to
324336
-- increment because a deposit only activates after one deposit period and
325337
-- expires one deposit period before deadline.
326338
deadline <- addUTCTime (3 * toNominalDiffTime depositPeriod) <$> getCurrentTime
327-
draftDepositTx headId pparams commitBlueprint deadline changeAddress <&> \case
328-
Left e -> responseLBS status400 jsonContent (Aeson.encode $ toJSON e)
329-
Right depositTx -> okJSON $ DraftCommitTxResponse depositTx
339+
result <- draftDepositTx headId pparams commitBlueprint deadline changeAddress
340+
case result of
341+
Left e -> do
342+
traceWith tracer $
343+
APIReturnedError
344+
{ reason = "Failed to draft deposit transaction: " <> show e
345+
}
346+
pure $ responseLBS status400 jsonContent (Aeson.encode $ toJSON e)
347+
Right depositTx -> pure $ okJSON $ DraftCommitTxResponse depositTx
330348

331349
draftCommit headId lookupUTxO blueprintTx = do
332-
draftCommitTx headId CommitBlueprintTx{lookupUTxO, blueprintTx} <&> \case
350+
result <- draftCommitTx headId CommitBlueprintTx{lookupUTxO, blueprintTx}
351+
case result of
333352
Left e ->
334353
-- Distinguish between errors users can actually benefit from and
335354
-- other errors that are turned into 500 responses.
336355
case e of
337-
CommittedTooMuchADAForMainnet _ _ -> badRequest e
338-
UnsupportedLegacyOutput _ -> badRequest e
339-
CannotFindOwnInitial _ -> badRequest e
340-
DepositTooLow _ _ -> badRequest e
341-
AmountTooLow _ _ -> badRequest e
342-
FailedToConstructDepositTx _ -> badRequest e
343-
_ -> responseLBS status500 [] (Aeson.encode $ toJSON e)
356+
CommittedTooMuchADAForMainnet _ _ -> pure $ badRequest e
357+
UnsupportedLegacyOutput _ -> pure $ badRequest e
358+
CannotFindOwnInitial _ -> pure $ badRequest e
359+
DepositTooLow _ _ -> pure $ badRequest e
360+
AmountTooLow _ _ -> pure $ badRequest e
361+
FailedToConstructDepositTx _ -> pure $ badRequest e
362+
_ -> do
363+
traceWith tracer $
364+
APIReturnedError
365+
{ reason = "Failed to draft commit transaction: " <> show e
366+
}
367+
pure $ responseLBS status500 [] (Aeson.encode $ toJSON e)
344368
Right commitTx ->
345-
okJSON $ DraftCommitTxResponse commitTx
369+
pure $ okJSON $ DraftCommitTxResponse commitTx
346370

347371
Chain{draftCommitTx, draftDepositTx} = directChain
348372

0 commit comments

Comments
 (0)