2019-07-22 15:47:13 +03:00
|
|
|
|
-- | Functions and datatypes for interpreting Postgres errors.
|
2020-10-27 16:53:49 +03:00
|
|
|
|
module Hasura.Backends.Postgres.SQL.Error
|
2019-08-23 15:57:09 +03:00
|
|
|
|
( PGErrorType(..)
|
|
|
|
|
, _PGDataException
|
|
|
|
|
, _PGIntegrityConstraintViolation
|
|
|
|
|
, _PGSyntaxErrorOrAccessRuleViolation
|
|
|
|
|
, pgErrorType
|
|
|
|
|
|
|
|
|
|
, PGErrorCode(..)
|
|
|
|
|
, _PGErrorGeneric
|
|
|
|
|
, _PGErrorSpecific
|
|
|
|
|
|
|
|
|
|
, PGDataException(..)
|
|
|
|
|
, PGIntegrityConstraintViolation(..)
|
|
|
|
|
, PGSyntaxErrorOrAccessRuleViolation(..)
|
|
|
|
|
) where
|
2019-07-22 15:47:13 +03:00
|
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
|
import qualified Database.PG.Query.Connection as Q
|
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
|
import Control.Lens.TH (makePrisms)
|
|
|
|
|
|
2019-07-22 15:47:13 +03:00
|
|
|
|
-- | The top-level error code type. Errors in Postgres are divided into different /classes/, which
|
|
|
|
|
-- are further subdivided into individual error codes. Even if a particular status code is not known
|
|
|
|
|
-- to the application, it’s possible to determine its class and handle it appropriately.
|
2019-08-23 15:57:09 +03:00
|
|
|
|
data PGErrorType
|
|
|
|
|
= PGDataException !(Maybe (PGErrorCode PGDataException))
|
|
|
|
|
| PGIntegrityConstraintViolation !(Maybe (PGErrorCode PGIntegrityConstraintViolation))
|
|
|
|
|
| PGSyntaxErrorOrAccessRuleViolation !(Maybe (PGErrorCode PGSyntaxErrorOrAccessRuleViolation))
|
2019-07-22 15:47:13 +03:00
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
2019-08-23 15:57:09 +03:00
|
|
|
|
data PGErrorCode a
|
|
|
|
|
= PGErrorGeneric
|
2019-07-22 15:47:13 +03:00
|
|
|
|
-- ^ represents errors that have the non-specific @000@ status code
|
2019-08-23 15:57:09 +03:00
|
|
|
|
| PGErrorSpecific !a
|
2019-07-22 15:47:13 +03:00
|
|
|
|
-- ^ represents errors with a known, more specific status code
|
|
|
|
|
deriving (Show, Eq, Functor)
|
|
|
|
|
|
2019-08-23 15:57:09 +03:00
|
|
|
|
data PGDataException
|
|
|
|
|
= PGInvalidDatetimeFormat
|
|
|
|
|
| PGInvalidParameterValue
|
|
|
|
|
| PGInvalidEscapeSequence
|
|
|
|
|
| PGInvalidTextRepresentation
|
2019-07-22 15:47:13 +03:00
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
2019-08-23 15:57:09 +03:00
|
|
|
|
data PGIntegrityConstraintViolation
|
|
|
|
|
= PGRestrictViolation
|
|
|
|
|
| PGNotNullViolation
|
|
|
|
|
| PGForeignKeyViolation
|
|
|
|
|
| PGUniqueViolation
|
|
|
|
|
| PGCheckViolation
|
|
|
|
|
| PGExclusionViolation
|
2019-07-22 15:47:13 +03:00
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
2019-08-23 15:57:09 +03:00
|
|
|
|
data PGSyntaxErrorOrAccessRuleViolation
|
|
|
|
|
= PGUndefinedObject
|
|
|
|
|
| PGInvalidColumnReference
|
2019-07-22 15:47:13 +03:00
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
2019-08-23 15:57:09 +03:00
|
|
|
|
$(makePrisms ''PGErrorType)
|
|
|
|
|
$(makePrisms ''PGErrorCode)
|
2019-07-22 15:47:13 +03:00
|
|
|
|
|
2019-08-23 15:57:09 +03:00
|
|
|
|
pgErrorType :: Q.PGStmtErrDetail -> Maybe PGErrorType
|
2019-07-22 15:47:13 +03:00
|
|
|
|
pgErrorType errorDetails = parseTypes =<< Q.edStatusCode errorDetails
|
|
|
|
|
where
|
|
|
|
|
parseTypes fullCodeText = choice
|
2019-08-23 15:57:09 +03:00
|
|
|
|
[ withClass "22" PGDataException
|
|
|
|
|
[ code "007" PGInvalidDatetimeFormat
|
|
|
|
|
, code "023" PGInvalidParameterValue
|
|
|
|
|
, code "025" PGInvalidEscapeSequence
|
|
|
|
|
, code "P02" PGInvalidTextRepresentation
|
2019-07-22 15:47:13 +03:00
|
|
|
|
]
|
2019-08-23 15:57:09 +03:00
|
|
|
|
, withClass "23" PGIntegrityConstraintViolation
|
|
|
|
|
[ code "001" PGRestrictViolation
|
|
|
|
|
, code "502" PGNotNullViolation
|
|
|
|
|
, code "503" PGForeignKeyViolation
|
|
|
|
|
, code "505" PGUniqueViolation
|
|
|
|
|
, code "514" PGCheckViolation
|
|
|
|
|
, code "P01" PGExclusionViolation
|
2019-07-22 15:47:13 +03:00
|
|
|
|
]
|
2019-08-23 15:57:09 +03:00
|
|
|
|
, withClass "42" PGSyntaxErrorOrAccessRuleViolation
|
|
|
|
|
[ code "704" PGUndefinedObject
|
|
|
|
|
, code "P10" PGInvalidColumnReference
|
2019-07-22 15:47:13 +03:00
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
where
|
|
|
|
|
(classText, codeText) = T.splitAt 2 fullCodeText
|
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
|
withClass :: Text -> (Maybe a -> b) -> [Maybe a] -> Maybe b
|
2019-07-22 15:47:13 +03:00
|
|
|
|
withClass expectedClassText mkClass codes =
|
|
|
|
|
guard (classText == expectedClassText) $> mkClass (choice codes)
|
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
|
code :: Text -> a -> Maybe (PGErrorCode a)
|
2019-07-22 15:47:13 +03:00
|
|
|
|
code expectedCodeText codeValue =
|
2019-08-23 15:57:09 +03:00
|
|
|
|
guard (codeText == expectedCodeText) $> PGErrorSpecific codeValue
|