Commit 8809d506 authored by Allele Dev's avatar Allele Dev

improve JSON instance

parent b782971d
{-# LANGUAGE OverloadedStrings #-}
module API.Errors ( module API.Errors (
APIError(..), APIError(..),
ViewErrors(..) ViewErrors(..)
) where ) where
import Data.Aeson import Data.Aeson
import Data.Aeson.Types
import Data.Text import Data.Text
import Data.UUID import Data.UUID
...@@ -20,33 +20,36 @@ data APIError ...@@ -20,33 +20,36 @@ data APIError
newtype ViewErrors = ViewErrors [APIError] newtype ViewErrors = ViewErrors [APIError]
(.=.) :: Text -> Text -> Pair
k .=. v = k .= v
instance ToJSON APIError where instance ToJSON APIError where
toJSON (Unknown t) = toJSON (Unknown t) =
object [ "type" .= ("unknown" :: Text) object [ "type" .=. "unknown"
, "description" .= t , "description" .= t
] ]
toJSON AuthenticationDown = toJSON AuthenticationDown =
object [ "type" .= ("authentication_down" :: Text) ] object [ "type" .=. "authentication_down" ]
toJSON Unauthorized = toJSON Unauthorized =
object [ "type" .= ("unauthorized" :: Text) ] object [ "type" .=. "unauthorized" ]
toJSON BadCredentials = toJSON BadCredentials =
object [ "type" .= ("incorrect_credentials" :: Text) ] object [ "type" .=. "incorrect_credentials" ]
toJSON (DBError t) = toJSON (DBError t) =
object [ "type" .= ("database_error" :: Text) object [ "type" .=. "database_error"
, "description" .= t , "description" .= t
] ]
toJSON UserExists = toJSON UserExists =
object [ "type" .= ("user_exists" :: Text) object [ "type" .=. "user_exists"
, "description" .= ("This email is already registered." :: Text) , "description" .=. "This email is already registered."
] ]
toJSON (BadRequest desc) = toJSON (BadRequest desc) =
object [ "type" .= ("bad_request" :: Text) object [ "type" .=. "bad_request"
, "description" .= desc , "description" .= desc
] ]
toJSON (NotFound id') = toJSON (NotFound id') =
object [ "type" .= ("not_found" :: Text) object [ "type" .=. "not_found"
, "id" .= toText id' , "id" .= toText id'
, "description" .= ("Entity not found" :: Text) , "description" .=. "Entity not found"
] ]
instance ToJSON ViewErrors where instance ToJSON ViewErrors where
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment