Haskell Servant - login endpoint with two error states
Since the Handler LoginAttempt from action only ever contains Ok, its result should instead be Handler Text. And then nobody ever sees a LoginAttempt object, so we don't need it.

action :: Config -&gt; JWTSettings -&gt; Credentials -&gt; Handler Text
action config jwt (Credentials email pass) = do
  login &lt;- either authenticationError pure =&lt;&lt;
    runSqlPool (Queries.validateAccount email pass) (getPool config)
  token &lt;- either tokenGenerationError pure =&lt;&lt;
    liftIO (makeJWT login jwt Nothing)
  pure $ decodeUtf8 token -- perhaps: pure $ decode $ object [ "token" .= decodeUtf8 token ]
  where
    autheticationError   _ = throwError $ err401 { errBody = encode $ object [ "error" .= ("Invalid username or password" :: Text) ] }
    tokenGenerationError _ = throwError $ err500 { errBody = encode $ object [ "error" .= ("Error generating token"       :: Text) ] }