0 | module HTTP.RequestErr
2 | import Control.Monad.MErr
4 | import Derive.Prelude
7 | import JSON.Simple.Derive
10 | %language ElabReflection
13 | record RequestErr where
18 | details : Either JSON String
21 | %runElab derive "RequestErr" [Show,Eq,FromJSON,ToJSON]
24 | requestErr : Status -> RequestErr
25 | requestErr (MkStatus c e) = RE c e "" (Left JNull) ""
28 | requestErrMsg : String -> Status -> RequestErr
29 | requestErrMsg m (MkStatus c e) = RE c e m (Left JNull) ""
32 | requestErrDetails : Interpolation a => a -> Status -> RequestErr
33 | requestErrDetails v (MkStatus c e) = RE c e "" (Right "\{v}") ""
36 | requestErrJSON : ToJSON a => a -> Status -> RequestErr
37 | requestErrJSON v (MkStatus c e) = RE c e "" (Left $
toJSON v) ""
42 | toJSONErr : FromJSON e => RequestErr -> Maybe e
43 | toJSONErr (RE _ _ _ (Left j) _) = eitherToMaybe $
fromJSON j
44 | toJSONErr (RE _ _ _ (Right s) _) = eitherToMaybe $
decode s
52 | -> {auto hr : Has RequestErr es}
53 | -> {auto ha : Has e es}
54 | -> {auto fj : FromJSON e}
55 | -> {auto me : MErr f}
59 | catch RequestErr act >>= \case
61 | Left x => maybe (throw x) throw (toJSONErr @{fj} x)