Skip to content

Commit 31a99f1

Browse files
authored
Merge pull request #1324 from IntersectMBO/1166-query-slot-number-malformated-timestamp-exits-with-callstack-error
Improve error message for malformed timestamps
2 parents 6c336d4 + b79d67e commit 31a99f1

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

cardano-cli/src/Cardano/CLI/EraBased/Common/Option.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ import Data.Maybe
5151
import Data.Text (Text)
5252
import Data.Text qualified as Text
5353
import Data.Time.Clock (UTCTime)
54-
import Data.Time.Format (defaultTimeLocale, parseTimeOrError)
54+
import Data.Time.Format (defaultTimeLocale, parseTimeM)
5555
import Data.Word
5656
import GHC.Exts (IsList (..))
5757
import GHC.Natural (Natural)
5858
import Lens.Micro
5959
import Network.Socket (PortNumber)
6060
import Options.Applicative hiding (help, str)
6161
import Options.Applicative qualified as Opt
62+
import Options.Applicative.Types (readerAsk)
6263
import Text.Read (readEither, readMaybe)
6364
import Text.Read qualified as Read
6465
import Vary (Vary, (:|))
@@ -1413,9 +1414,19 @@ pTxMetadataJsonSchema =
14131414
pure TxMetadataJsonNoSchema
14141415
]
14151416

1416-
convertTime :: String -> UTCTime
1417-
convertTime =
1418-
parseTimeOrError False defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ"
1417+
convertTime :: String -> Maybe UTCTime
1418+
convertTime = parseTimeM False defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ"
1419+
1420+
timeReader :: ReadM UTCTime
1421+
timeReader = do
1422+
arg <- readerAsk
1423+
maybe
1424+
( readerError $
1425+
"Malformed timestamp `" ++ arg ++ "', use UTC timestamp in YYYY-MM-DDThh:mm:ssZ format."
1426+
)
1427+
return
1428+
. convertTime
1429+
$ arg
14191430

14201431
pMetadataFile :: Parser MetadataFile
14211432
pMetadataFile =

cardano-cli/src/Cardano/CLI/EraBased/Genesis/Option.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ pGenesisDir =
387387
pMaybeSystemStart :: Parser (Maybe SystemStart)
388388
pMaybeSystemStart =
389389
Opt.optional $
390-
fmap (SystemStart . convertTime) $
391-
Opt.strOption $
390+
fmap SystemStart $
391+
Opt.option timeReader $
392392
mconcat
393393
[ Opt.long "start-time"
394394
, Opt.metavar "UTC_TIME"

cardano-cli/src/Cardano/CLI/EraBased/Query/Option.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,13 @@ pQuerySlotNumberCmd envCli =
579579
<*> pUtcTimestamp
580580
where
581581
pUtcTimestamp =
582-
convertTime
583-
<$> (Opt.strArgument . mconcat)
584-
[ Opt.metavar "TIMESTAMP"
585-
, Opt.help "UTC timestamp in YYYY-MM-DDThh:mm:ssZ format"
586-
]
582+
Opt.argument
583+
timeReader
584+
( mconcat
585+
[ Opt.metavar "TIMESTAMP"
586+
, Opt.help "UTC timestamp in YYYY-MM-DDThh:mm:ssZ format"
587+
]
588+
)
587589

588590
pQueryRefScriptSizeCmd :: forall era. IsEra era => EnvCli -> Parser (QueryCmds era)
589591
pQueryRefScriptSizeCmd envCli =

cardano-cli/src/Cardano/CLI/EraBased/Transaction/Option.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ systemStartPOSIX =
486486

487487
systemStartUTC :: Parser SystemStart
488488
systemStartUTC =
489-
SystemStart . convertTime
490-
<$> ( Opt.strOption $
489+
SystemStart
490+
<$> ( Opt.option timeReader $
491491
mconcat
492492
[ Opt.long "start-time-utc"
493493
, Opt.metavar "UTC_TIME"

cardano-cli/src/Cardano/CLI/Legacy/Option.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ pGenesisCmds envCli =
236236
pMaybeSystemStart :: Parser (Maybe SystemStart)
237237
pMaybeSystemStart =
238238
Opt.optional $
239-
fmap (SystemStart . convertTime) $
240-
Opt.strOption $
239+
fmap SystemStart $
240+
Opt.option timeReader $
241241
mconcat
242242
[ Opt.long "start-time"
243243
, Opt.metavar "UTC_TIME"

0 commit comments

Comments
 (0)