@@ -7,78 +7,85 @@ module Cardano.Db.Error (
7
7
AsDbError (.. ),
8
8
CallSite (.. ),
9
9
DbError (.. ),
10
- LookupFail (.. ),
11
10
runOrThrowIODb ,
11
+ runOrThrowIO ,
12
12
logAndThrowIO ,
13
+ base16encode
13
14
) where
14
15
15
16
import Cardano.BM.Trace (Trace , logError )
16
17
import Cardano.Db.Schema.Ids
17
- import Cardano.Prelude (throwIO )
18
+ import Cardano.Prelude (throwIO , MonadIO )
18
19
import Control.Exception (Exception )
19
20
import Data.ByteString.Char8 (ByteString )
20
21
import Data.Text (Text )
21
22
import Data.Word (Word16 , Word64 )
22
23
import GHC.Generics (Generic )
23
24
import qualified Data.ByteString.Base16 as Base16
24
25
import qualified Data.Text.Encoding as Text
25
- import qualified Hasql.Session as HasqlS
26
+ import qualified Hasql.Session as HsqlS
26
27
27
28
class AsDbError e where
28
29
toDbError :: DbError -> e
29
30
fromDbError :: e -> Maybe DbError
30
31
31
32
data DbError
32
- = QueryError ! Text ! CallSite ! HasqlS. SessionError
33
- | DecodingError ! Text ! CallSite ! HasqlS. RowError
34
- | ConnectionError ! Text ! CallSite
35
- | TransactionError ! Text ! CallSite
33
+ = DbError ! CallSite ! Text ! HsqlS. SessionError
34
+ | DbLookupError ! CallSite ! Text ! LookupContext
36
35
deriving (Show , Eq )
37
36
37
+ instance Exception DbError
38
+
38
39
data CallSite = CallSite
39
40
{ csModule :: ! Text
40
41
, csFile :: ! Text
41
42
, csLine :: ! Int
42
43
} deriving (Show , Eq )
43
44
44
- data LookupFail
45
- = DbLookupBlockHash ! ByteString
46
- | DbLookupBlockId ! Word64
47
- | DbLookupMessage ! Text
48
- | DbLookupTxHash ! ByteString
49
- | DbLookupTxOutPair ! ByteString ! Word16
50
- | DbLookupEpochNo ! Word64
51
- | DbLookupSlotNo ! Word64
52
- | DbLookupGovActionPair ! TxId ! Word64
53
- | DbMetaEmpty
54
- | DbMetaMultipleRows
55
- | DBMultipleGenesis
56
- | DBExtraMigration ! String
57
- | DBPruneConsumed ! String
58
- | DBRJsonbInSchema ! String
59
- | DBTxOutVariant ! String
60
- deriving (Eq , Generic )
45
+ data LookupContext
46
+ = BlockHashContext ! ByteString
47
+ | BlockIdContext ! Word64
48
+ | MessageContext ! Text
49
+ | TxHashContext ! ByteString
50
+ | TxOutPairContext ! ByteString ! Word16
51
+ | EpochNoContext ! Word64
52
+ | SlotNoContext ! Word64
53
+ | GovActionPairContext ! TxId ! Word64
54
+ | MetaEmptyContext
55
+ | MetaMultipleRowsContext
56
+ | MultipleGenesisContext
57
+ | ExtraMigrationContext ! String
58
+ | PruneConsumedContext ! String
59
+ | RJsonbInSchemaContext ! String
60
+ | TxOutVariantContext ! String
61
+ deriving (Show , Eq , Generic )
62
+
63
+ instance Exception LookupContext
61
64
62
- instance Exception LookupFail
65
+ -- catchDbError :: String -> HsqlT.Transaction a -> HsqlT.Transaction a
66
+ -- catchDbError context action =
67
+ -- action `catch` \e ->
68
+ -- throwError $ DbError $ context ++ ": " ++ show e
63
69
64
- instance Show LookupFail where
65
- show =
66
- \ case
67
- DbLookupBlockHash h -> " The block hash " <> show (base16encode h) <> " is missing from the DB."
68
- DbLookupBlockId blkid -> " block id " <> show blkid
69
- DbLookupMessage txt -> show txt
70
- DbLookupTxHash h -> " tx hash " <> show (base16encode h)
71
- DbLookupTxOutPair h i -> concat [" tx out pair (" , show $ base16encode h, " , " , show i, " )" ]
72
- DbLookupEpochNo e -> " epoch number " ++ show e
73
- DbLookupSlotNo s -> " slot number " ++ show s
74
- DbLookupGovActionPair txId index -> concat [" missing GovAction (" , show txId, " , " , show index, " )" ]
75
- DbMetaEmpty -> " Meta table is empty"
76
- DbMetaMultipleRows -> " Multiple rows in Meta table which should only contain one"
77
- DBMultipleGenesis -> " Multiple Genesis blocks found. These are blocks without an EpochNo"
78
- DBExtraMigration e -> " DBExtraMigration : " <> e
79
- DBPruneConsumed e -> " DBExtraMigration" <> e
80
- DBRJsonbInSchema e -> " DBRJsonbInSchema" <> e
81
- DBTxOutVariant e -> " DbTxOutVariant" <> e
70
+
71
+ -- instance Show LookupFail where
72
+ -- show =
73
+ -- \case
74
+ -- DbLookupBlockHash h -> "The block hash " <> show (base16encode h) <> " is missing from the DB."
75
+ -- DbLookupBlockId blkid -> "block id " <> show blkid
76
+ -- DbLookupMessage txt -> show txt
77
+ -- DbLookupTxHash h -> "tx hash " <> show (base16encode h)
78
+ -- DbLookupTxOutPair h i -> concat ["tx out pair (", show $ base16encode h, ", ", show i, ")"]
79
+ -- DbLookupEpochNo e -> "epoch number " ++ show e
80
+ -- DbLookupSlotNo s -> "slot number " ++ show s
81
+ -- DbLookupGovActionPair txId index -> concat ["missing GovAction (", show txId, ", ", show index, ")"]
82
+ -- DbMetaEmpty -> "Meta table is empty"
83
+ -- DbMetaMultipleRows -> "Multiple rows in Meta table which should only contain one"
84
+ -- DBMultipleGenesis -> "Multiple Genesis blocks found. These are blocks without an EpochNo"
85
+ -- DBExtraMigration e -> "DBExtraMigration : " <> e
86
+ -- DBPruneConsumed e -> "DBExtraMigration" <> e
87
+ -- DBRJsonbInSchema e -> "DBRJsonbInSchema" <> e
88
+ -- DBTxOutVariant e -> "DbTxOutVariant" <> e
82
89
83
90
base16encode :: ByteString -> Text
84
91
base16encode = Text. decodeUtf8 . Base16. encode
@@ -90,6 +97,13 @@ runOrThrowIODb ioEither = do
90
97
Left err -> throwIO err
91
98
Right a -> pure a
92
99
100
+ runOrThrowIO :: forall e a m . (MonadIO m ) => (Exception e ) => m (Either e a ) -> m a
101
+ runOrThrowIO ioEither = do
102
+ et <- ioEither
103
+ case et of
104
+ Left err -> throwIO err
105
+ Right a -> pure a
106
+
93
107
logAndThrowIO :: Trace IO Text -> Text -> IO a
94
108
logAndThrowIO tracer msg = do
95
109
logError tracer msg
0 commit comments