{-# LANGUAGE FlexibleContexts #-}

module FoundationDB.Error.Internal where

import Control.Exception
import Control.Monad.Error.Class (MonadError (..), liftEither)
import Control.Monad.IO.Class (MonadIO (..))
import Data.ByteString (ByteString)
import Data.Maybe (fromJust)
import Data.Word (Word32)
import qualified FoundationDB.Internal.Bindings as FDB

fdbEither :: MonadIO m => m (FDB.CFDBError, a) -> m (Either Error a)
fdbEither :: m (CFDBError, a) -> m (Either Error a)
fdbEither m (CFDBError, a)
f = do
  (CFDBError
err, a
res) <- m (CFDBError, a)
f
  case CFDBError -> Maybe CError
toError CFDBError
err of
    Just CError
x -> Either Error a -> m (Either Error a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Error a -> m (Either Error a))
-> Either Error a -> m (Either Error a)
forall a b. (a -> b) -> a -> b
$ Error -> Either Error a
forall a b. a -> Either a b
Left (Error -> Either Error a) -> Error -> Either Error a
forall a b. (a -> b) -> a -> b
$ CError -> Error
CError CError
x
    Maybe CError
Nothing -> Either Error a -> m (Either Error a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either Error a
forall a b. b -> Either a b
Right a
res)

fdbExcept ::
  (MonadError Error m, MonadIO m) =>
  IO (FDB.CFDBError, a) ->
  m a
fdbExcept :: IO (CFDBError, a) -> m a
fdbExcept IO (CFDBError, a)
x = do
  Either Error a
e <- IO (Either Error a) -> m (Either Error a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either Error a) -> m (Either Error a))
-> IO (Either Error a) -> m (Either Error a)
forall a b. (a -> b) -> a -> b
$ IO (CFDBError, a) -> IO (Either Error a)
forall (m :: * -> *) a.
MonadIO m =>
m (CFDBError, a) -> m (Either Error a)
fdbEither IO (CFDBError, a)
x
  Either Error a -> m a
forall e (m :: * -> *) a. MonadError e m => Either e a -> m a
liftEither Either Error a
e

fdbEither' :: MonadIO m => m FDB.CFDBError -> m (Either Error ())
fdbEither' :: m CFDBError -> m (Either Error ())
fdbEither' m CFDBError
f = do
  CFDBError
err <- m CFDBError
f
  case CFDBError -> Maybe CError
toError CFDBError
err of
    Just CError
x -> Either Error () -> m (Either Error ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Error () -> m (Either Error ()))
-> Either Error () -> m (Either Error ())
forall a b. (a -> b) -> a -> b
$ Error -> Either Error ()
forall a b. a -> Either a b
Left (Error -> Either Error ()) -> Error -> Either Error ()
forall a b. (a -> b) -> a -> b
$ CError -> Error
CError CError
x
    Maybe CError
Nothing -> Either Error () -> m (Either Error ())
forall (m :: * -> *) a. Monad m => a -> m a
return (() -> Either Error ()
forall a b. b -> Either a b
Right ())

fdbExcept' ::
  (MonadError Error m, MonadIO m) =>
  IO FDB.CFDBError ->
  m ()
fdbExcept' :: IO CFDBError -> m ()
fdbExcept' IO CFDBError
x = do
  Either Error ()
e <- IO (Either Error ()) -> m (Either Error ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either Error ()) -> m (Either Error ()))
-> IO (Either Error ()) -> m (Either Error ())
forall a b. (a -> b) -> a -> b
$ IO CFDBError -> IO (Either Error ())
forall (m :: * -> *).
MonadIO m =>
m CFDBError -> m (Either Error ())
fdbEither' IO CFDBError
x
  Either Error () -> m ()
forall e (m :: * -> *) a. MonadError e m => Either e a -> m a
liftEither Either Error ()
e

liftFDBError :: MonadError Error m => Either FDB.CFDBError a -> m a
liftFDBError :: Either CFDBError a -> m a
liftFDBError = (CFDBError -> m a) -> (a -> m a) -> Either CFDBError a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (CFDBError -> Error) -> CFDBError -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CError -> Error
CError (CError -> Error) -> (CFDBError -> CError) -> CFDBError -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe CError -> CError
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe CError -> CError)
-> (CFDBError -> Maybe CError) -> CFDBError -> CError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CFDBError -> Maybe CError
toError) a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

fdbThrowing :: IO (FDB.CFDBError, a) -> IO a
fdbThrowing :: IO (CFDBError, a) -> IO a
fdbThrowing IO (CFDBError, a)
a = do
  (CFDBError
e, a
res) <- IO (CFDBError, a)
a
  case CFDBError -> Maybe CError
toError CFDBError
e of
    Just CError
x -> Error -> IO a
forall e a. Exception e => e -> IO a
throwIO (Error -> IO a) -> Error -> IO a
forall a b. (a -> b) -> a -> b
$ CError -> Error
CError CError
x
    Maybe CError
Nothing -> a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res

fdbThrowing' :: IO FDB.CFDBError -> IO ()
fdbThrowing' :: IO CFDBError -> IO ()
fdbThrowing' IO CFDBError
a = do
  CFDBError
e <- IO CFDBError
a
  case CFDBError -> Maybe CError
toError CFDBError
e of
    Just CError
x -> Error -> IO ()
forall e a. Exception e => e -> IO a
throwIO (Error -> IO ()) -> Error -> IO ()
forall a b. (a -> b) -> a -> b
$ CError -> Error
CError CError
x
    Maybe CError
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Represents a range of keys that conflicted with another transaction.
-- @ConflictRange x y@ corresponds to a range of keys that share the prefix @x@
-- (including @x@) up to the prefix @y@  (not including @y@). It is always the
-- case that @x <= y@.
data ConflictRange = ConflictRange ByteString ByteString
  deriving (Int -> ConflictRange -> ShowS
[ConflictRange] -> ShowS
ConflictRange -> String
(Int -> ConflictRange -> ShowS)
-> (ConflictRange -> String)
-> ([ConflictRange] -> ShowS)
-> Show ConflictRange
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ConflictRange] -> ShowS
$cshowList :: [ConflictRange] -> ShowS
show :: ConflictRange -> String
$cshow :: ConflictRange -> String
showsPrec :: Int -> ConflictRange -> ShowS
$cshowsPrec :: Int -> ConflictRange -> ShowS
Show, ReadPrec [ConflictRange]
ReadPrec ConflictRange
Int -> ReadS ConflictRange
ReadS [ConflictRange]
(Int -> ReadS ConflictRange)
-> ReadS [ConflictRange]
-> ReadPrec ConflictRange
-> ReadPrec [ConflictRange]
-> Read ConflictRange
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ConflictRange]
$creadListPrec :: ReadPrec [ConflictRange]
readPrec :: ReadPrec ConflictRange
$creadPrec :: ReadPrec ConflictRange
readList :: ReadS [ConflictRange]
$creadList :: ReadS [ConflictRange]
readsPrec :: Int -> ReadS ConflictRange
$creadsPrec :: Int -> ReadS ConflictRange
Read, ConflictRange -> ConflictRange -> Bool
(ConflictRange -> ConflictRange -> Bool)
-> (ConflictRange -> ConflictRange -> Bool) -> Eq ConflictRange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConflictRange -> ConflictRange -> Bool
$c/= :: ConflictRange -> ConflictRange -> Bool
== :: ConflictRange -> ConflictRange -> Bool
$c== :: ConflictRange -> ConflictRange -> Bool
Eq, Eq ConflictRange
Eq ConflictRange
-> (ConflictRange -> ConflictRange -> Ordering)
-> (ConflictRange -> ConflictRange -> Bool)
-> (ConflictRange -> ConflictRange -> Bool)
-> (ConflictRange -> ConflictRange -> Bool)
-> (ConflictRange -> ConflictRange -> Bool)
-> (ConflictRange -> ConflictRange -> ConflictRange)
-> (ConflictRange -> ConflictRange -> ConflictRange)
-> Ord ConflictRange
ConflictRange -> ConflictRange -> Bool
ConflictRange -> ConflictRange -> Ordering
ConflictRange -> ConflictRange -> ConflictRange
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ConflictRange -> ConflictRange -> ConflictRange
$cmin :: ConflictRange -> ConflictRange -> ConflictRange
max :: ConflictRange -> ConflictRange -> ConflictRange
$cmax :: ConflictRange -> ConflictRange -> ConflictRange
>= :: ConflictRange -> ConflictRange -> Bool
$c>= :: ConflictRange -> ConflictRange -> Bool
> :: ConflictRange -> ConflictRange -> Bool
$c> :: ConflictRange -> ConflictRange -> Bool
<= :: ConflictRange -> ConflictRange -> Bool
$c<= :: ConflictRange -> ConflictRange -> Bool
< :: ConflictRange -> ConflictRange -> Bool
$c< :: ConflictRange -> ConflictRange -> Bool
compare :: ConflictRange -> ConflictRange -> Ordering
$ccompare :: ConflictRange -> ConflictRange -> Ordering
$cp1Ord :: Eq ConflictRange
Ord)

-- | Represents all errors that can occur when running a 'Transaction'.
data Error = CError CError | Error FDBHsError
  deriving (Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Error] -> ShowS
$cshowList :: [Error] -> ShowS
show :: Error -> String
$cshow :: Error -> String
showsPrec :: Int -> Error -> ShowS
$cshowsPrec :: Int -> Error -> ShowS
Show, Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c== :: Error -> Error -> Bool
Eq, Eq Error
Eq Error
-> (Error -> Error -> Ordering)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Error)
-> (Error -> Error -> Error)
-> Ord Error
Error -> Error -> Bool
Error -> Error -> Ordering
Error -> Error -> Error
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Error -> Error -> Error
$cmin :: Error -> Error -> Error
max :: Error -> Error -> Error
$cmax :: Error -> Error -> Error
>= :: Error -> Error -> Bool
$c>= :: Error -> Error -> Bool
> :: Error -> Error -> Bool
$c> :: Error -> Error -> Bool
<= :: Error -> Error -> Bool
$c<= :: Error -> Error -> Bool
< :: Error -> Error -> Bool
$c< :: Error -> Error -> Bool
compare :: Error -> Error -> Ordering
$ccompare :: Error -> Error -> Ordering
$cp1Ord :: Eq Error
Ord)

instance Exception Error

data DirLayerUserError
  = -- | Thrown if the user attempts to open the root directory.
    CannotOpenRoot
  | -- | Thrown if the user specifies a manual prefix that is already in use.
    PrefixInUse
  | -- | Thrown if a prefix manually specified by the user previously conflicts
    --   with a prefix chosen by the automatic allocator. Includes the conflicting
    --   prefix.
    ManualPrefixConflict ByteString
  | -- | The @layer@ bytestring provided to @open'@ does not match the layer
    --   already present. The mismatched layers are included in this constructor.
    LayerMismatch ByteString ByteString
  | -- | Thrown if the directory layer structure already in FoundationDB is a
    --   newer major version than that provided by this library. This would mean
    --   that the directory layer was originally created by a newer version of one
    --   of the FoundationDB client libraries. The major, minor, micro version
    --   of the directory layer are provided to this constructor.
    VersionError Word32 Word32 Word32
  deriving (Int -> DirLayerUserError -> ShowS
[DirLayerUserError] -> ShowS
DirLayerUserError -> String
(Int -> DirLayerUserError -> ShowS)
-> (DirLayerUserError -> String)
-> ([DirLayerUserError] -> ShowS)
-> Show DirLayerUserError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DirLayerUserError] -> ShowS
$cshowList :: [DirLayerUserError] -> ShowS
show :: DirLayerUserError -> String
$cshow :: DirLayerUserError -> String
showsPrec :: Int -> DirLayerUserError -> ShowS
$cshowsPrec :: Int -> DirLayerUserError -> ShowS
Show, DirLayerUserError -> DirLayerUserError -> Bool
(DirLayerUserError -> DirLayerUserError -> Bool)
-> (DirLayerUserError -> DirLayerUserError -> Bool)
-> Eq DirLayerUserError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DirLayerUserError -> DirLayerUserError -> Bool
$c/= :: DirLayerUserError -> DirLayerUserError -> Bool
== :: DirLayerUserError -> DirLayerUserError -> Bool
$c== :: DirLayerUserError -> DirLayerUserError -> Bool
Eq, Eq DirLayerUserError
Eq DirLayerUserError
-> (DirLayerUserError -> DirLayerUserError -> Ordering)
-> (DirLayerUserError -> DirLayerUserError -> Bool)
-> (DirLayerUserError -> DirLayerUserError -> Bool)
-> (DirLayerUserError -> DirLayerUserError -> Bool)
-> (DirLayerUserError -> DirLayerUserError -> Bool)
-> (DirLayerUserError -> DirLayerUserError -> DirLayerUserError)
-> (DirLayerUserError -> DirLayerUserError -> DirLayerUserError)
-> Ord DirLayerUserError
DirLayerUserError -> DirLayerUserError -> Bool
DirLayerUserError -> DirLayerUserError -> Ordering
DirLayerUserError -> DirLayerUserError -> DirLayerUserError
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: DirLayerUserError -> DirLayerUserError -> DirLayerUserError
$cmin :: DirLayerUserError -> DirLayerUserError -> DirLayerUserError
max :: DirLayerUserError -> DirLayerUserError -> DirLayerUserError
$cmax :: DirLayerUserError -> DirLayerUserError -> DirLayerUserError
>= :: DirLayerUserError -> DirLayerUserError -> Bool
$c>= :: DirLayerUserError -> DirLayerUserError -> Bool
> :: DirLayerUserError -> DirLayerUserError -> Bool
$c> :: DirLayerUserError -> DirLayerUserError -> Bool
<= :: DirLayerUserError -> DirLayerUserError -> Bool
$c<= :: DirLayerUserError -> DirLayerUserError -> Bool
< :: DirLayerUserError -> DirLayerUserError -> Bool
$c< :: DirLayerUserError -> DirLayerUserError -> Bool
compare :: DirLayerUserError -> DirLayerUserError -> Ordering
$ccompare :: DirLayerUserError -> DirLayerUserError -> Ordering
$cp1Ord :: Eq DirLayerUserError
Ord)

-- | Errors arising from the foundationdb-haskell library implementation.
data FDBHsError
  = -- | Errors that can occur from user error when using the directory layer.
    DirLayerUserError DirLayerUserError
  | -- | Errors that can occur when doing directory layer operations.
    -- These can be indicative of bugs in foundationdb-haskell.
    DirectoryLayerInternalError String
  | -- | Errors in parsing tuples.
    ParseError String
  | -- | Thrown by foundationdb-haskell's transaction retry logic. Contains the
    -- underlying error from the C bindings that caused the transaction to be
    -- retried.
    MaxRetriesExceeded Error
  | -- | Thrown by foundationdb-haskell on startup if the Haskell code doesn't
    -- support the desired API version. This can happen even if the underlying C
    -- library does support the desired version -- we sometimes drop support
    -- for older versions sooner than the C API.
    UnsupportedAPIVersion
  | -- | The structure of keys returned by the transaction module of the special
    -- keys keyspace was not in the expected format. The raw key/values
    -- are returned, unparsed.
    ConflictRangeParseFailure [(ByteString, ByteString)]
  | -- | Thrown when an integer to be encoded by the tuple layer would take more
    -- than 255 bytes to encode.
    TupleIntTooLarge
  deriving (Int -> FDBHsError -> ShowS
[FDBHsError] -> ShowS
FDBHsError -> String
(Int -> FDBHsError -> ShowS)
-> (FDBHsError -> String)
-> ([FDBHsError] -> ShowS)
-> Show FDBHsError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FDBHsError] -> ShowS
$cshowList :: [FDBHsError] -> ShowS
show :: FDBHsError -> String
$cshow :: FDBHsError -> String
showsPrec :: Int -> FDBHsError -> ShowS
$cshowsPrec :: Int -> FDBHsError -> ShowS
Show, FDBHsError -> FDBHsError -> Bool
(FDBHsError -> FDBHsError -> Bool)
-> (FDBHsError -> FDBHsError -> Bool) -> Eq FDBHsError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FDBHsError -> FDBHsError -> Bool
$c/= :: FDBHsError -> FDBHsError -> Bool
== :: FDBHsError -> FDBHsError -> Bool
$c== :: FDBHsError -> FDBHsError -> Bool
Eq, Eq FDBHsError
Eq FDBHsError
-> (FDBHsError -> FDBHsError -> Ordering)
-> (FDBHsError -> FDBHsError -> Bool)
-> (FDBHsError -> FDBHsError -> Bool)
-> (FDBHsError -> FDBHsError -> Bool)
-> (FDBHsError -> FDBHsError -> Bool)
-> (FDBHsError -> FDBHsError -> FDBHsError)
-> (FDBHsError -> FDBHsError -> FDBHsError)
-> Ord FDBHsError
FDBHsError -> FDBHsError -> Bool
FDBHsError -> FDBHsError -> Ordering
FDBHsError -> FDBHsError -> FDBHsError
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FDBHsError -> FDBHsError -> FDBHsError
$cmin :: FDBHsError -> FDBHsError -> FDBHsError
max :: FDBHsError -> FDBHsError -> FDBHsError
$cmax :: FDBHsError -> FDBHsError -> FDBHsError
>= :: FDBHsError -> FDBHsError -> Bool
$c>= :: FDBHsError -> FDBHsError -> Bool
> :: FDBHsError -> FDBHsError -> Bool
$c> :: FDBHsError -> FDBHsError -> Bool
<= :: FDBHsError -> FDBHsError -> Bool
$c<= :: FDBHsError -> FDBHsError -> Bool
< :: FDBHsError -> FDBHsError -> Bool
$c< :: FDBHsError -> FDBHsError -> Bool
compare :: FDBHsError -> FDBHsError -> Ordering
$ccompare :: FDBHsError -> FDBHsError -> Ordering
$cp1Ord :: Eq FDBHsError
Ord)

-- | Errors that can come from the underlying C library.
-- Most error names are self-explanatory.
-- See https://apple.github.io/foundationdb/api-error-codes.html#developer-guide-error-codes
-- for a description of these errors.
data CError
  = OperationFailed
  | TimedOut
  | TransactionTooOld
  | FutureVersion
  | -- | Returned if a transaction failed because of a conflict. If
    -- 'FoundationDB.Transaction.getConflictingKeys' is set, returns conflicting
    -- key ranges.
    NotCommitted [ConflictRange]
  | CommitUnknownResult
  | TransactionCanceled
  | TransactionTimedOut
  | TooManyWatches
  | WatchesDisabled
  | AccessedUnreadable
  | DatabaseLocked
  | ClusterVersionChanged
  | ExternalClientAlreadyLoaded
  | OperationCancelled
  | FutureReleased
  | PlatformError
  | LargeAllocFailed
  | PerformanceCounterError
  | IOError
  | FileNotFound
  | BindFailed
  | FileNotReadable
  | FileNotWritable
  | NoClusterFileFound
  | FileTooLarge
  | ClientInvalidOperation
  | CommitReadIncomplete
  | TestSpecificationInvalid
  | KeyOutsideLegalRange
  | InvertedRange
  | InvalidOptionValue
  | InvalidOption
  | NetworkNotSetup
  | NetworkAlreadySetup
  | ReadVersionAlreadySet
  | VersionInvalid
  | RangeLimitsInvalid
  | InvalidDatabaseName
  | AttributeNotFound
  | FutureNotSet
  | FutureNotError
  | UsedDuringCommit
  | InvalidMutationType
  | TransactionInvalidVersion
  | NoCommitVersion
  | EnvironmentVariableNetworkOptionFailed
  | TransactionReadOnly
  | IncompatibleProtocolVersion
  | TransactionTooLarge
  | KeyTooLarge
  | ValueTooLarge
  | ConnectionStringInvalid
  | AddressInUse
  | InvalidLocalAddress
  | TLSError
  | UnsupportedOperation
  | APIVersionUnset
  | APIVersionAlreadySet
  | APIVersionInvalid
  | APIVersionNotSupported
  | ExactModeWithoutLimits
  | UnknownError
  | InternalError
  | OtherError {CError -> CFDBError
getOtherError :: FDB.CFDBError}
  deriving (Int -> CError -> ShowS
[CError] -> ShowS
CError -> String
(Int -> CError -> ShowS)
-> (CError -> String) -> ([CError] -> ShowS) -> Show CError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CError] -> ShowS
$cshowList :: [CError] -> ShowS
show :: CError -> String
$cshow :: CError -> String
showsPrec :: Int -> CError -> ShowS
$cshowsPrec :: Int -> CError -> ShowS
Show, CError -> CError -> Bool
(CError -> CError -> Bool)
-> (CError -> CError -> Bool) -> Eq CError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CError -> CError -> Bool
$c/= :: CError -> CError -> Bool
== :: CError -> CError -> Bool
$c== :: CError -> CError -> Bool
Eq, Eq CError
Eq CError
-> (CError -> CError -> Ordering)
-> (CError -> CError -> Bool)
-> (CError -> CError -> Bool)
-> (CError -> CError -> Bool)
-> (CError -> CError -> Bool)
-> (CError -> CError -> CError)
-> (CError -> CError -> CError)
-> Ord CError
CError -> CError -> Bool
CError -> CError -> Ordering
CError -> CError -> CError
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CError -> CError -> CError
$cmin :: CError -> CError -> CError
max :: CError -> CError -> CError
$cmax :: CError -> CError -> CError
>= :: CError -> CError -> Bool
$c>= :: CError -> CError -> Bool
> :: CError -> CError -> Bool
$c> :: CError -> CError -> Bool
<= :: CError -> CError -> Bool
$c<= :: CError -> CError -> Bool
< :: CError -> CError -> Bool
$c< :: CError -> CError -> Bool
compare :: CError -> CError -> Ordering
$ccompare :: CError -> CError -> Ordering
$cp1Ord :: Eq CError
Ord)

-- | Convert error int to 'CError' sum. If 0 (which indicates success), returns
-- 'Nothing'. See 'isError' for another way to guard for success.
toError :: FDB.CFDBError -> Maybe CError
toError :: CFDBError -> Maybe CError
toError CFDBError
0 = Maybe CError
forall a. Maybe a
Nothing
toError CFDBError
1000 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
OperationFailed
toError CFDBError
1004 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TimedOut
toError CFDBError
1007 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TransactionTooOld
toError CFDBError
1009 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FutureVersion
toError CFDBError
1020 = CError -> Maybe CError
forall a. a -> Maybe a
Just ([ConflictRange] -> CError
NotCommitted [])
toError CFDBError
1021 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
CommitUnknownResult
toError CFDBError
1025 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TransactionCanceled
toError CFDBError
1031 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TransactionTimedOut
toError CFDBError
1032 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TooManyWatches
toError CFDBError
1034 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
WatchesDisabled
toError CFDBError
1036 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
AccessedUnreadable
toError CFDBError
1038 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
DatabaseLocked
toError CFDBError
1039 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ClusterVersionChanged
toError CFDBError
1040 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ExternalClientAlreadyLoaded
toError CFDBError
1101 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
OperationCancelled
toError CFDBError
1102 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FutureReleased
toError CFDBError
1500 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
PlatformError
toError CFDBError
1501 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
LargeAllocFailed
toError CFDBError
1502 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
PerformanceCounterError
toError CFDBError
1510 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
IOError
toError CFDBError
1511 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FileNotFound
toError CFDBError
1512 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
BindFailed
toError CFDBError
1513 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FileNotReadable
toError CFDBError
1514 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FileNotWritable
toError CFDBError
1515 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
NoClusterFileFound
toError CFDBError
1516 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FileTooLarge
toError CFDBError
2000 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ClientInvalidOperation
toError CFDBError
2002 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
CommitReadIncomplete
toError CFDBError
2003 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TestSpecificationInvalid
toError CFDBError
2004 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
KeyOutsideLegalRange
toError CFDBError
2005 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InvertedRange
toError CFDBError
2006 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InvalidOptionValue
toError CFDBError
2007 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InvalidOption
toError CFDBError
2008 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
NetworkNotSetup
toError CFDBError
2009 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
NetworkAlreadySetup
toError CFDBError
2010 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ReadVersionAlreadySet
toError CFDBError
2011 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
VersionInvalid
toError CFDBError
2012 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
RangeLimitsInvalid
toError CFDBError
2013 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InvalidDatabaseName
toError CFDBError
2014 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
AttributeNotFound
toError CFDBError
2015 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FutureNotSet
toError CFDBError
2016 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
FutureNotError
toError CFDBError
2017 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
UsedDuringCommit
toError CFDBError
2018 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InvalidMutationType
toError CFDBError
2020 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TransactionInvalidVersion
toError CFDBError
2021 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
NoCommitVersion
toError CFDBError
2022 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
EnvironmentVariableNetworkOptionFailed
toError CFDBError
2023 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TransactionReadOnly
toError CFDBError
2100 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
IncompatibleProtocolVersion
toError CFDBError
2101 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TransactionTooLarge
toError CFDBError
2102 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
KeyTooLarge
toError CFDBError
2103 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ValueTooLarge
toError CFDBError
2104 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ConnectionStringInvalid
toError CFDBError
2105 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
AddressInUse
toError CFDBError
2106 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InvalidLocalAddress
toError CFDBError
2107 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
TLSError
toError CFDBError
2108 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
UnsupportedOperation
toError CFDBError
2200 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
APIVersionUnset
toError CFDBError
2201 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
APIVersionAlreadySet
toError CFDBError
2202 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
APIVersionInvalid
toError CFDBError
2203 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
APIVersionNotSupported
toError CFDBError
2210 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
ExactModeWithoutLimits
toError CFDBError
4000 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
UnknownError
toError CFDBError
4100 = CError -> Maybe CError
forall a. a -> Maybe a
Just CError
InternalError
toError CFDBError
n = CError -> Maybe CError
forall a. a -> Maybe a
Just (CError -> Maybe CError) -> CError -> Maybe CError
forall a b. (a -> b) -> a -> b
$ CFDBError -> CError
OtherError CFDBError
n

toCFDBError :: CError -> FDB.CFDBError
toCFDBError :: CError -> CFDBError
toCFDBError CError
OperationFailed = CFDBError
1000
toCFDBError CError
TimedOut = CFDBError
1004
toCFDBError CError
TransactionTooOld = CFDBError
1007
toCFDBError CError
FutureVersion = CFDBError
1009
toCFDBError (NotCommitted [ConflictRange]
_) = CFDBError
1020
toCFDBError CError
CommitUnknownResult = CFDBError
1021
toCFDBError CError
TransactionCanceled = CFDBError
1025
toCFDBError CError
TransactionTimedOut = CFDBError
1031
toCFDBError CError
TooManyWatches = CFDBError
1032
toCFDBError CError
WatchesDisabled = CFDBError
1034
toCFDBError CError
AccessedUnreadable = CFDBError
1036
toCFDBError CError
DatabaseLocked = CFDBError
1038
toCFDBError CError
ClusterVersionChanged = CFDBError
1039
toCFDBError CError
ExternalClientAlreadyLoaded = CFDBError
1040
toCFDBError CError
OperationCancelled = CFDBError
1101
toCFDBError CError
FutureReleased = CFDBError
1102
toCFDBError CError
PlatformError = CFDBError
1500
toCFDBError CError
LargeAllocFailed = CFDBError
1501
toCFDBError CError
PerformanceCounterError = CFDBError
1502
toCFDBError CError
IOError = CFDBError
1510
toCFDBError CError
FileNotFound = CFDBError
1511
toCFDBError CError
BindFailed = CFDBError
1512
toCFDBError CError
FileNotReadable = CFDBError
1513
toCFDBError CError
FileNotWritable = CFDBError
1514
toCFDBError CError
NoClusterFileFound = CFDBError
1515
toCFDBError CError
FileTooLarge = CFDBError
1516
toCFDBError CError
ClientInvalidOperation = CFDBError
2000
toCFDBError CError
CommitReadIncomplete = CFDBError
2002
toCFDBError CError
TestSpecificationInvalid = CFDBError
2003
toCFDBError CError
KeyOutsideLegalRange = CFDBError
2004
toCFDBError CError
InvertedRange = CFDBError
2005
toCFDBError CError
InvalidOptionValue = CFDBError
2006
toCFDBError CError
InvalidOption = CFDBError
2007
toCFDBError CError
NetworkNotSetup = CFDBError
2008
toCFDBError CError
NetworkAlreadySetup = CFDBError
2009
toCFDBError CError
ReadVersionAlreadySet = CFDBError
2010
toCFDBError CError
VersionInvalid = CFDBError
2011
toCFDBError CError
RangeLimitsInvalid = CFDBError
2012
toCFDBError CError
InvalidDatabaseName = CFDBError
2013
toCFDBError CError
AttributeNotFound = CFDBError
2014
toCFDBError CError
FutureNotSet = CFDBError
2015
toCFDBError CError
FutureNotError = CFDBError
2016
toCFDBError CError
UsedDuringCommit = CFDBError
2017
toCFDBError CError
InvalidMutationType = CFDBError
2018
toCFDBError CError
TransactionInvalidVersion = CFDBError
2020
toCFDBError CError
NoCommitVersion = CFDBError
2021
toCFDBError CError
EnvironmentVariableNetworkOptionFailed = CFDBError
2022
toCFDBError CError
TransactionReadOnly = CFDBError
2023
toCFDBError CError
IncompatibleProtocolVersion = CFDBError
2100
toCFDBError CError
TransactionTooLarge = CFDBError
2101
toCFDBError CError
KeyTooLarge = CFDBError
2102
toCFDBError CError
ValueTooLarge = CFDBError
2103
toCFDBError CError
ConnectionStringInvalid = CFDBError
2104
toCFDBError CError
AddressInUse = CFDBError
2105
toCFDBError CError
InvalidLocalAddress = CFDBError
2106
toCFDBError CError
TLSError = CFDBError
2107
toCFDBError CError
UnsupportedOperation = CFDBError
2108
toCFDBError CError
APIVersionUnset = CFDBError
2200
toCFDBError CError
APIVersionAlreadySet = CFDBError
2201
toCFDBError CError
APIVersionInvalid = CFDBError
2202
toCFDBError CError
APIVersionNotSupported = CFDBError
2203
toCFDBError CError
ExactModeWithoutLimits = CFDBError
2210
toCFDBError CError
UnknownError = CFDBError
4000
toCFDBError CError
InternalError = CFDBError
4100
toCFDBError (OtherError CFDBError
err) = CFDBError
err

-- | Returns true if the given error indicates that the erroring transaction
-- can be retried, but the transaction might have already been committed. In
-- such cases, the transaction should only be retried if it is idempotent.
retryable :: Error -> Bool
retryable :: Error -> Bool
retryable (CError CError
e) =
  FDBErrorPredicate -> CFDBError -> Bool
FDB.errorPredicate FDBErrorPredicate
FDB.ErrorPredicateRetryable (CError -> CFDBError
toCFDBError CError
e)
retryable (Error FDBHsError
_) = Bool
False

-- | Returns true if the given error indicates that the transaction was
-- definitely not committed.
retryableNotCommitted :: Error -> Bool
retryableNotCommitted :: Error -> Bool
retryableNotCommitted (CError CError
e) =
  FDBErrorPredicate -> CFDBError -> Bool
FDB.errorPredicate FDBErrorPredicate
FDB.ErrorPredicateRetryableNotCommitted (CError -> CFDBError
toCFDBError CError
e)
retryableNotCommitted (Error FDBHsError
_) = Bool
False