module Hasql.Errors where
import Data.ByteString.Char8 qualified as BC
import Hasql.Prelude
data SessionError
=
QueryError
ByteString
[Text]
CommandError
|
PipelineError
CommandError
deriving (Int -> SessionError -> ShowS
[SessionError] -> ShowS
SessionError -> String
(Int -> SessionError -> ShowS)
-> (SessionError -> String)
-> ([SessionError] -> ShowS)
-> Show SessionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SessionError -> ShowS
showsPrec :: Int -> SessionError -> ShowS
$cshow :: SessionError -> String
show :: SessionError -> String
$cshowList :: [SessionError] -> ShowS
showList :: [SessionError] -> ShowS
Show, SessionError -> SessionError -> Bool
(SessionError -> SessionError -> Bool)
-> (SessionError -> SessionError -> Bool) -> Eq SessionError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SessionError -> SessionError -> Bool
== :: SessionError -> SessionError -> Bool
$c/= :: SessionError -> SessionError -> Bool
/= :: SessionError -> SessionError -> Bool
Eq, Typeable)
instance Exception SessionError where
displayException :: SessionError -> String
displayException = \case
QueryError ByteString
query [Text]
params CommandError
commandError ->
let queryContext :: Maybe (ByteString, Int)
queryContext :: Maybe (ByteString, Int)
queryContext = case CommandError
commandError of
ClientError Maybe ByteString
_ -> Maybe (ByteString, Int)
forall a. Maybe a
Nothing
ResultError ResultError
resultError -> case ResultError
resultError of
ServerError ByteString
_ ByteString
message Maybe ByteString
_ Maybe ByteString
_ (Just Int
position) -> (ByteString, Int) -> Maybe (ByteString, Int)
forall a. a -> Maybe a
Just (ByteString
message, Int
position)
ResultError
_ -> Maybe (ByteString, Int)
forall a. Maybe a
Nothing
findLineAndPos :: ByteString -> Int -> (Int, Int)
findLineAndPos :: ByteString -> Int -> (Int, Int)
findLineAndPos ByteString
byteString Int
errorPos =
let (Int
_, Int
line, Int
pos) =
((Int, Int, Int) -> Char -> (Int, Int, Int))
-> (Int, Int, Int) -> ByteString -> (Int, Int, Int)
forall a. (a -> Char -> a) -> a -> ByteString -> a
BC.foldl'
( \(Int
total, Int
line, Int
pos) Char
c ->
case Int
total Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 of
Int
0 -> (Int
total, Int
line, Int
pos)
Int
cursor
| Int
cursor Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
errorPos -> (-Int
1, Int
line, Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n' -> (Int
total Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
0)
| Bool
otherwise -> (Int
total Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
line, Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
)
(Int
0, Int
1, Int
0)
ByteString
byteString
in (Int
line, Int
pos)
formatErrorContext :: ByteString -> ByteString -> Int -> ByteString
formatErrorContext :: ByteString -> ByteString -> Int -> ByteString
formatErrorContext ByteString
query ByteString
message Int
errorPos =
let lines :: [ByteString]
lines = ByteString -> [ByteString]
BC.lines ByteString
query
(Int
lineNum, Int
linePos) = ByteString -> Int -> (Int, Int)
findLineAndPos ByteString
query Int
errorPos
in [ByteString] -> ByteString
BC.unlines (Int -> [ByteString] -> [ByteString]
forall a. Int -> [a] -> [a]
take Int
lineNum [ByteString]
lines)
ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> Char -> ByteString
BC.replicate (Int
linePos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Char
' '
ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"^ "
ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
message
prettyQuery :: ByteString
prettyQuery :: ByteString
prettyQuery = case Maybe (ByteString, Int)
queryContext of
Maybe (ByteString, Int)
Nothing -> ByteString
query
Just (ByteString
message, Int
pos) -> ByteString -> ByteString -> Int -> ByteString
formatErrorContext ByteString
query ByteString
message Int
pos
in String
"QueryError!\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n Query:\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
BC.unpack ByteString
prettyQuery
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n Params: "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Text] -> String
forall a. Show a => a -> String
show [Text]
params
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n Error: "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CommandError -> String
renderCommandErrorAsReason CommandError
commandError
PipelineError CommandError
commandError ->
String
"PipelineError!\n Reason: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CommandError -> String
renderCommandErrorAsReason CommandError
commandError
where
renderCommandErrorAsReason :: CommandError -> String
renderCommandErrorAsReason = \case
ClientError (Just ByteString
message) -> String
"Client error: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
forall a. Show a => a -> String
show ByteString
message
ClientError Maybe ByteString
Nothing -> String
"Client error without details"
ResultError ResultError
resultError -> case ResultError
resultError of
ServerError ByteString
code ByteString
message Maybe ByteString
details Maybe ByteString
hint Maybe Int
position ->
String
"Server error "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
BC.unpack ByteString
code
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
BC.unpack ByteString
message
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> (ByteString -> String) -> Maybe ByteString -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\ByteString
d -> String
"\n Details: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
BC.unpack ByteString
d) Maybe ByteString
details
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> (ByteString -> String) -> Maybe ByteString -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\ByteString
h -> String
"\n Hint: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
BC.unpack ByteString
h) Maybe ByteString
hint
UnexpectedResult Text
message -> String
"Unexpected result: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
message
RowError Int
row Int
column RowError
rowError ->
String
"Error in row " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
row String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", column " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
column String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> RowError -> String
forall a. Show a => a -> String
show RowError
rowError
UnexpectedAmountOfRows Int
amount ->
String
"Unexpected amount of rows: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
amount
data CommandError
=
ClientError (Maybe ByteString)
|
ResultError ResultError
deriving (Int -> CommandError -> ShowS
[CommandError] -> ShowS
CommandError -> String
(Int -> CommandError -> ShowS)
-> (CommandError -> String)
-> ([CommandError] -> ShowS)
-> Show CommandError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommandError -> ShowS
showsPrec :: Int -> CommandError -> ShowS
$cshow :: CommandError -> String
show :: CommandError -> String
$cshowList :: [CommandError] -> ShowS
showList :: [CommandError] -> ShowS
Show, CommandError -> CommandError -> Bool
(CommandError -> CommandError -> Bool)
-> (CommandError -> CommandError -> Bool) -> Eq CommandError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommandError -> CommandError -> Bool
== :: CommandError -> CommandError -> Bool
$c/= :: CommandError -> CommandError -> Bool
/= :: CommandError -> CommandError -> Bool
Eq)
data ResultError
=
ServerError
ByteString
ByteString
(Maybe ByteString)
(Maybe ByteString)
(Maybe Int)
|
UnexpectedResult Text
|
RowError Int Int RowError
|
UnexpectedAmountOfRows Int
deriving (Int -> ResultError -> ShowS
[ResultError] -> ShowS
ResultError -> String
(Int -> ResultError -> ShowS)
-> (ResultError -> String)
-> ([ResultError] -> ShowS)
-> Show ResultError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResultError -> ShowS
showsPrec :: Int -> ResultError -> ShowS
$cshow :: ResultError -> String
show :: ResultError -> String
$cshowList :: [ResultError] -> ShowS
showList :: [ResultError] -> ShowS
Show, ResultError -> ResultError -> Bool
(ResultError -> ResultError -> Bool)
-> (ResultError -> ResultError -> Bool) -> Eq ResultError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ResultError -> ResultError -> Bool
== :: ResultError -> ResultError -> Bool
$c/= :: ResultError -> ResultError -> Bool
/= :: ResultError -> ResultError -> Bool
Eq)
data RowError
=
EndOfInput
|
UnexpectedNull
|
ValueError Text
deriving (Int -> RowError -> ShowS
[RowError] -> ShowS
RowError -> String
(Int -> RowError -> ShowS)
-> (RowError -> String) -> ([RowError] -> ShowS) -> Show RowError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RowError -> ShowS
showsPrec :: Int -> RowError -> ShowS
$cshow :: RowError -> String
show :: RowError -> String
$cshowList :: [RowError] -> ShowS
showList :: [RowError] -> ShowS
Show, RowError -> RowError -> Bool
(RowError -> RowError -> Bool)
-> (RowError -> RowError -> Bool) -> Eq RowError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RowError -> RowError -> Bool
== :: RowError -> RowError -> Bool
$c/= :: RowError -> RowError -> Bool
/= :: RowError -> RowError -> Bool
Eq)