{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RecordWildCards    #-}
module Servant.Server.Internal.ServerError where

import           Control.Exception
                 (Exception)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy  as LBS
import           Data.Typeable
                 (Typeable)
import qualified Network.HTTP.Types    as HTTP
import           Network.Wai
                 (Response, responseLBS)

data ServerError = ServerError
    { ServerError -> Int
errHTTPCode     :: Int
    , ServerError -> String
errReasonPhrase :: String
    , ServerError -> ByteString
errBody         :: LBS.ByteString
    , ServerError -> [Header]
errHeaders      :: [HTTP.Header]
    }
  deriving (Int -> ServerError -> ShowS
[ServerError] -> ShowS
ServerError -> String
(Int -> ServerError -> ShowS)
-> (ServerError -> String)
-> ([ServerError] -> ShowS)
-> Show ServerError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ServerError -> ShowS
showsPrec :: Int -> ServerError -> ShowS
$cshow :: ServerError -> String
show :: ServerError -> String
$cshowList :: [ServerError] -> ShowS
showList :: [ServerError] -> ShowS
Show, ServerError -> ServerError -> Bool
(ServerError -> ServerError -> Bool)
-> (ServerError -> ServerError -> Bool) -> Eq ServerError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ServerError -> ServerError -> Bool
== :: ServerError -> ServerError -> Bool
$c/= :: ServerError -> ServerError -> Bool
/= :: ServerError -> ServerError -> Bool
Eq, ReadPrec [ServerError]
ReadPrec ServerError
Int -> ReadS ServerError
ReadS [ServerError]
(Int -> ReadS ServerError)
-> ReadS [ServerError]
-> ReadPrec ServerError
-> ReadPrec [ServerError]
-> Read ServerError
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS ServerError
readsPrec :: Int -> ReadS ServerError
$creadList :: ReadS [ServerError]
readList :: ReadS [ServerError]
$creadPrec :: ReadPrec ServerError
readPrec :: ReadPrec ServerError
$creadListPrec :: ReadPrec [ServerError]
readListPrec :: ReadPrec [ServerError]
Read, Typeable)

instance Exception ServerError

responseServerError :: ServerError -> Response
responseServerError :: ServerError -> Response
responseServerError ServerError{Int
String
[Header]
ByteString
$sel:errHTTPCode:ServerError :: ServerError -> Int
$sel:errReasonPhrase:ServerError :: ServerError -> String
$sel:errBody:ServerError :: ServerError -> ByteString
$sel:errHeaders:ServerError :: ServerError -> [Header]
errHTTPCode :: Int
errReasonPhrase :: String
errBody :: ByteString
errHeaders :: [Header]
..} = Status -> [Header] -> ByteString -> Response
responseLBS Status
status [Header]
errHeaders ByteString
errBody
  where
    status :: Status
status = Int -> ByteString -> Status
HTTP.mkStatus Int
errHTTPCode (String -> ByteString
BS.pack String
errReasonPhrase)

-- | 'err300' Multiple Choices
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err300 { errBody = "I can't choose." }
--
err300 :: ServerError
err300 :: ServerError
err300 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
300
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Multiple Choices"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err301' Moved Permanently
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err301
--
err301 :: ServerError
err301 :: ServerError
err301 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
301
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Moved Permanently"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err302' Found
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err302
--
err302 :: ServerError
err302 :: ServerError
err302 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
302
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Found"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err303' See Other
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err303
--
err303 :: ServerError
err303 :: ServerError
err303 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
303
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"See Other"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err304' Not Modified
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err304
--
err304 :: ServerError
err304 :: ServerError
err304 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
304
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Not Modified"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err305' Use Proxy
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err305
--
err305 :: ServerError
err305 :: ServerError
err305 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
305
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Use Proxy"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err307' Temporary Redirect
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err307
--
err307 :: ServerError
err307 :: ServerError
err307 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
307
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Temporary Redirect"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err400' Bad Request
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err400 { errBody = "Your request makes no sense to me." }
--
err400 :: ServerError
err400 :: ServerError
err400 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
400
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Bad Request"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err401' Unauthorized
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err401 { errBody = "Your credentials are invalid." }
--
err401 :: ServerError
err401 :: ServerError
err401 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
401
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Unauthorized"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err402' Payment Required
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err402 { errBody = "You have 0 credits. Please give me $$$." }
--
err402 :: ServerError
err402 :: ServerError
err402 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
402
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Payment Required"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err403' Forbidden
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err403 { errBody = "Please login first." }
--
err403 :: ServerError
err403 :: ServerError
err403 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
403
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Forbidden"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err404' Not Found
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err404 { errBody = "Are you lost?" }
--
err404 :: ServerError
err404 :: ServerError
err404 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
404
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Not Found"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err405' Method Not Allowed
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err405 { errBody = "Your account privileges does not allow for this.  Please pay $$$." }
--
err405 :: ServerError
err405 :: ServerError
err405 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
405
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Method Not Allowed"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err406' Not Acceptable
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err406
--
err406 :: ServerError
err406 :: ServerError
err406 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
406
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Not Acceptable"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err407' Proxy Authentication Required
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err407
--
err407 :: ServerError
err407 :: ServerError
err407 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
407
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Proxy Authentication Required"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err409' Conflict
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err409 { errBody = "Transaction conflicts with 59879cb56c7c159231eeacdd503d755f7e835f74" }
--
err409 :: ServerError
err409 :: ServerError
err409 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
409
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Conflict"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err410' Gone
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err410 { errBody = "I know it was here at some point, but.. I blame bad luck." }
--
err410 :: ServerError
err410 :: ServerError
err410 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
410
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Gone"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err411' Length Required
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError err411
--
err411 :: ServerError
err411 :: ServerError
err411 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
411
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Length Required"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err412' Precondition Failed
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err412 { errBody = "Precondition fail: x < 42 && y > 57" }
--
err412 :: ServerError
err412 :: ServerError
err412 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
412
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Precondition Failed"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err413' Request Entity Too Large
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err413 { errBody = "Request exceeded 64k." }
--
err413 :: ServerError
err413 :: ServerError
err413 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
413
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Request Entity Too Large"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err414' Request-URI Too Large
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err414 { errBody = "Maximum length is 64." }
--
err414 :: ServerError
err414 :: ServerError
err414 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
414
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Request-URI Too Large"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err415' Unsupported Media Type
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err415 { errBody = "Supported media types:  gif, png" }
--
err415 :: ServerError
err415 :: ServerError
err415 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
415
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Unsupported Media Type"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err416' Request range not satisfiable
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err416 { errBody = "Valid range is [0, 424242]." }
--
err416 :: ServerError
err416 :: ServerError
err416 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
416
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Request range not satisfiable"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err417' Expectation Failed
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err417 { errBody = "I found a quux in the request.  This isn't going to work." }
--
err417 :: ServerError
err417 :: ServerError
err417 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
417
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Expectation Failed"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err418' Expectation Failed
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err418 { errBody = "Apologies, this is not a webserver but a teapot." }
--
err418 :: ServerError
err418 :: ServerError
err418 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
418
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"I'm a teapot"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err422' Unprocessable Entity
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err422 { errBody = "I understood your request, but can't process it." }
--
err422 :: ServerError
err422 :: ServerError
err422 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
422
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Unprocessable Entity"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err429' Too Many Requests
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err429 { errBody = "You have sent too many requests in a short period of time." }
--
err429 :: ServerError
err429 :: ServerError
err429 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
429
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Too Many Requests"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err500' Internal Server Error
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err500 { errBody = "Exception in module A.B.C:55.  Have a great day!" }
--
err500 :: ServerError
err500 :: ServerError
err500 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
500
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Internal Server Error"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err501' Not Implemented
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err501 { errBody = "/v1/foo is not supported with quux in the request." }
--
err501 :: ServerError
err501 :: ServerError
err501 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
501
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Not Implemented"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err502' Bad Gateway
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err502 { errBody = "Tried gateway foo, bar, and baz.  None responded." }
--
err502 :: ServerError
err502 :: ServerError
err502 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
502
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Bad Gateway"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err503' Service Unavailable
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err503 { errBody = "We're rewriting in PHP." }
--
err503 :: ServerError
err503 :: ServerError
err503 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
503
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Service Unavailable"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err504' Gateway Time-out
--
-- Example:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err504 { errBody = "Backend foobar did not respond in 5 seconds." }
--
err504 :: ServerError
err504 :: ServerError
err504 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
504
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"Gateway Time-out"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }

-- | 'err505' HTTP Version not supported
--
-- Example usage:
--
-- > failingHandler :: Handler ()
-- > failingHandler = throwError $ err505 { errBody = "I support HTTP/4.0 only." }
--
err505 :: ServerError
err505 :: ServerError
err505 = ServerError { $sel:errHTTPCode:ServerError :: Int
errHTTPCode = Int
505
                    , $sel:errReasonPhrase:ServerError :: String
errReasonPhrase = String
"HTTP Version not supported"
                    , $sel:errBody:ServerError :: ByteString
errBody = ByteString
""
                    , $sel:errHeaders:ServerError :: [Header]
errHeaders = []
                    }