6. Error HandlingΒΆ

WebApi gives you a way to raise errors in your handler using raise. The following handler is an example that raises a 404 error

instance ApiHandler MyApiImpl GET User where
  handler _ req = do
    hasUser <- isUserInDB
    if (hasUser)
        then respond (UserToken "Foo" "Bar")
        else raise status404 ()

raise takes two arguments. First one is the status code which we need to send with the Response. Second argument is of type ApiErr m r which defaults to Unit ().

If you want to send some additional information with your error response, you can write a data type for error and specify that as ApiErr in your contract.

An example,

data Error = Error { error :: Text } deriving (Show, Generic)
instance ToJSON Error
instance ParamErrToApiErr Error where
    toApiErr errs = Error (toApiErr errs)

instance ApiContract MyApiService POST User where
 type FormParam POST User = UserData
 type ApiOut    POST User = UserToken
 type ApiErr    POST User = Error

Any type which you associate with ApiErr, should have a ParamErrToApiErr instance. This is needed for WebApi to map all the failures to this type. Also based on ContentType set in the contract (which defaults to JSON), we need to give the required instance. In this case it is ToJSON.