fix: improve error stringification

This commit is contained in:
orion 2024-07-21 16:51:12 -05:00
parent 4179367385
commit 5f8be2e1f2
Signed by: orion
GPG Key ID: 6D4165AE4C928719

View File

@ -2,10 +2,15 @@ module Effect.Postgres.Error.Common where
import Prelude
import Data.Array as Array
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Foldable (fold)
import Data.Generic.Rep (class Generic)
import Data.Maybe (maybe)
import Data.Newtype (wrap)
import Data.Postgres.Query (Query)
import Data.Show.Generic (genericShow)
import Data.String as String
import Effect.Exception as Effect
import Foreign (MultipleErrors)
@ -21,7 +26,23 @@ data Error
derive instance Generic Error _
instance Show Error where
show = genericShow
show = toString
toString :: Error -> String
toString =
let
indent n s = fold $ ((fold $ Array.replicate n " ") <> _) <$> String.split (wrap "\n") s
jsError n e =
indent n (Effect.message e)
<> maybe "" (\s -> "\n" <> indent n s) (Effect.stack e)
in
case _ of
Deserializing q es -> "Deserializing " <> show q <> "\n" <> indent 1 (show es)
Serializing es -> "Serializing" <> "\n" <> indent 1 (show es)
Executing q e -> "Executing " <> show q <> "\n" <> jsError 1 e
Connecting e -> "Connecting\n" <> jsError 1 e
Disconnecting e -> "Disconnecting\n" <> jsError 1 e
Other e -> "Other\n" <> jsError 1 e
toException' :: Error -> Effect.Error
toException' = Effect.error <<< show