@@ -10,7 +10,6 @@ module Hasura.Base.Error
1010 showQErr ,
1111 encodeQErr ,
1212 encodeGQLErr ,
13- noInternalQErrEnc ,
1413 err400 ,
1514 err404 ,
1615 err405 ,
6160import Control.Arrow.Extended
6261import Control.Lens (makeLensesFor , makePrisms )
6362import Data.Aeson
63+ import Data.Aeson.Encoding qualified as J
6464import Data.Aeson.Internal
6565import Data.Aeson.Key qualified as K
6666import Data.Aeson.Types
@@ -196,23 +196,31 @@ instance ToJSON QErrExtra where
196196 HideInconsistencies -> Null
197197
198198instance ToJSON QErr where
199- toJSON (QErr jPath _ msg code Nothing ) =
200- object
201- [ " path" .= encodeJSONPath jPath,
202- " error" .= msg,
203- " code" .= code
204- ]
205- toJSON (QErr jPath _ msg code (Just extra)) = object
206- $ case extra of
207- ExtraInternal e -> err ++ [" internal" .= e]
208- ExtraExtensions {} -> err
209- HideInconsistencies -> []
199+ toJSON (QErr jPath _ msg code extra) =
200+ object $ case extra of
201+ Just (ExtraInternal e) -> " internal" .= e : err
202+ Just ExtraExtensions {} -> err
203+ Just HideInconsistencies -> []
204+ Nothing -> err
210205 where
211206 err =
212- [ " path " .= encodeJSONPath jPath ,
213- " error " .= msg ,
207+ [ " error " .= msg ,
208+ " path " .= encodeJSONPath jPath ,
214209 " code" .= code
215210 ]
211+ toEncoding (QErr jPath _ msg code extra) =
212+ pairs $ case extra of
213+ Just (ExtraInternal e) -> err <> " internal" .= e -- Internal comes after all other properties so is the last thing the user sees
214+ Just ExtraExtensions {} -> err
215+ Just HideInconsistencies -> mempty
216+ Nothing -> err
217+ where
218+ err =
219+ -- error property comes first so the error message is the first
220+ -- thing the user sees
221+ (" error" .= msg)
222+ <> (" path" .= encodeJSONPath jPath)
223+ <> (" code" .= code)
216224
217225-- | Overrides the status and code of a QErr while retaining all other fields.
218226overrideQErrStatus :: HTTP. Status -> Code -> QErr -> QErr
@@ -226,41 +234,32 @@ prefixQErr prefix err = err {qeError = prefix <> qeError err}
226234showQErr :: QErr -> Text
227235showQErr = TL. toStrict . TL. decodeUtf8 . encode
228236
229- noInternalQErrEnc :: QErr -> Value
230- noInternalQErrEnc (QErr jPath _ msg code _) =
231- object
232- [ " path" .= encodeJSONPath jPath,
233- " error" .= msg,
234- " code" .= code
235- ]
236-
237- encodeGQLErr :: Bool -> QErr -> Value
237+ encodeGQLErr :: Bool -> QErr -> Encoding
238238encodeGQLErr includeInternal (QErr jPath _ msg code maybeExtra) =
239- object
240- [ " message" .= msg,
241- " extensions" .= extnsObj
242- ]
239+ pairs ((" message" .= msg) <> (J. pair " extensions" extnsObj))
243240 where
244- appendIf cond a b = if cond then a ++ b else a
241+ appendIf cond a b = if cond then a <> b else a
245242
246243 extnsObj = case maybeExtra of
247- Nothing -> object codeAndPath
244+ Nothing -> pairs codeAndPath
248245 -- if an `extensions` key is given in the error response from the webhook,
249246 -- we ignore the `code` key regardless of whether the `extensions` object
250247 -- contains a `code` field:
251- Just (ExtraExtensions v) -> v
248+ Just (ExtraExtensions v) -> toEncoding v
252249 Just (ExtraInternal v) ->
253- object $ appendIf includeInternal codeAndPath [ " internal" .= v]
254- Just HideInconsistencies -> Null
250+ pairs $ appendIf includeInternal codeAndPath ( " internal" .= v)
251+ Just HideInconsistencies -> toEncoding Null
255252 codeAndPath =
256- [ " path" .= encodeJSONPath jPath,
257- " code" .= code
258- ]
253+ (" path" .= encodeJSONPath jPath)
254+ <> (" code" .= code)
259255
260256-- whether internal should be included or not
261- encodeQErr :: Bool -> QErr -> Value
262- encodeQErr True = toJSON
263- encodeQErr _ = noInternalQErrEnc
257+ encodeQErr :: Bool -> QErr -> Encoding
258+ encodeQErr True = toEncoding
259+ encodeQErr False = toEncoding . removeInternalErr
260+ where
261+ removeInternalErr :: QErr -> QErr
262+ removeInternalErr err = err {qeInternal = Nothing }
264263
265264-- Postgres Connection Errors
266265instance PG. FromPGConnErr QErr where
0 commit comments