module Hasql.Errors where
import qualified Data.ByteString.Char8 as BC
import Hasql.Prelude
data QueryError
= QueryError ByteString [Text] CommandError
deriving (Int -> QueryError -> ShowS
[QueryError] -> ShowS
QueryError -> String
(Int -> QueryError -> ShowS)
-> (QueryError -> String)
-> ([QueryError] -> ShowS)
-> Show QueryError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> QueryError -> ShowS
showsPrec :: Int -> QueryError -> ShowS
$cshow :: QueryError -> String
show :: QueryError -> String
$cshowList :: [QueryError] -> ShowS
showList :: [QueryError] -> ShowS
Show, QueryError -> QueryError -> Bool
(QueryError -> QueryError -> Bool)
-> (QueryError -> QueryError -> Bool) -> Eq QueryError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: QueryError -> QueryError -> Bool
== :: QueryError -> QueryError -> Bool
$c/= :: QueryError -> QueryError -> Bool
/= :: QueryError -> QueryError -> Bool
Eq, Typeable)
instance Exception QueryError where
displayException :: QueryError -> String
displayException (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
<> case CommandError
commandError of
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
"Unknown client error"
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
"Row error: " 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
":" 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)