File: Exception.hs

package info (click to toggle)
haskell-postgresql-simple 0.7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 564 kB
  • sloc: haskell: 6,438; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 3,301 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE  CPP #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Exception (testExceptions) where

import Database.PostgreSQL.Simple
import Test.Tasty.HUnit (Assertion, assertBool)
import Common (TestEnv)
import Control.Exception (Exception (..), SomeException)
import Data.Maybe (isJust)
import Data.Either (isLeft)
import Control.Exception (throwIO, try)

testExceptions :: TestEnv -> Assertion
testExceptions _ = do
  let sqlError = SqlError
        { sqlState       = ""
        , sqlExecStatus  = FatalError
        , sqlErrorMsg    = ""
        , sqlErrorDetail = ""
        , sqlErrorHint   = ""
        }
  let sqlEx :: SomeException = toException sqlError
  assertBool "SqlError is SomePostgreSqlException" $ isJust (fromException sqlEx :: Maybe SomePostgreSqlException)
  assertBool "SqlError is SqlError" $ isJust (fromException sqlEx :: Maybe SqlError)
  eSqlError :: Either SqlError () <- try $ throwIO sqlEx
  assertBool "Can catch SqlError" $ isLeft eSqlError
  eSqlPostgreSqlEx :: Either SomePostgreSqlException () <- try $ throwIO sqlEx
  assertBool "Can catch SomePostgreSqlException from SqlError" $ isLeft eSqlPostgreSqlEx

  let formatError = FormatError
        { fmtMessage = ""
        , fmtQuery = ""
        , fmtParams = []
        }
  let formatEx :: SomeException = toException formatError
  assertBool "FormatError is SomePostgreSqlException" $ isJust (fromException formatEx :: Maybe SomePostgreSqlException)
  assertBool "FormatError is FormatError" $ isJust (fromException formatEx :: Maybe FormatError)
  eFormatError :: Either FormatError () <- try $ throwIO formatEx
  assertBool "Can catch FormatError" $ isLeft eFormatError
  eFormatPostreSqlEx :: Either SomePostgreSqlException () <- try $ throwIO formatEx
  assertBool "Can catch SomePostgreSqlException from FormatError" $ isLeft eFormatPostreSqlEx

  let queryError = QueryError
          { qeMessage = ""
          , qeQuery = ""
          }
  let queryEx :: SomeException = toException queryError
  assertBool "QueryError is SomePostgreSqlException" $ isJust (fromException queryEx :: Maybe SomePostgreSqlException)
  assertBool "QueryError is QueryError" $ isJust (fromException queryEx :: Maybe QueryError)
  eQueryError :: Either QueryError () <- try $ throwIO queryEx
  assertBool "Can catch QueryError" $ isLeft eQueryError
  eQueryPostgreSqlEx :: Either SomePostgreSqlException () <- try $ throwIO queryEx
  assertBool "Can catch SomePostgreSqlException from QueryError" $ isLeft eQueryPostgreSqlEx

  let resultError = Incompatible
        { errSQLType = ""
        , errSQLTableOid = Nothing
        , errSQLField = ""
        , errHaskellType = ""
        , errMessage = ""
        }
  let resultEx :: SomeException = toException resultError
  assertBool "ResultError is SomePostgreSqlException" $ isJust (fromException resultEx :: Maybe SomePostgreSqlException)
  assertBool "ResultError is ResultError" $ isJust (fromException resultEx :: Maybe ResultError)
  eResultEx :: Either ResultError () <- try $ throwIO resultEx
  assertBool "Can catch ResultError" $ isLeft eResultEx
  eResultPostgreSqlEx :: Either SomePostgreSqlException () <- try $ throwIO resultEx
  assertBool "Can catch SomePostgreSqlException from ResultError" $ isLeft eResultPostgreSqlEx