Open utdemir opened 8 years ago
Edit: Moved this comment to https://github.com/haskell-servant/servant/issues/294
@arianvp I guess you wrote this for #294 .
Relating to this, is there a way to return JSON encodable errors using ServantErr
? Based on the tutorial, it seems like ServantErr
always has a string based errBody
which is not how most APIs are built :smile:
Something like
data ServantErr e = ServantErr { errHTTPCode :: Int
, errReasonPhrase :: String
, errBody :: e
, errHeaders :: [HTTP.Header]
} deriving (Show, Eq, Read)
Such that e
can only be a value that corresponds to values that are accepted by the MimeRender
instance of the resource? Yes I think this would be extremely useful.
I don't think it would be too hard to implement. Right?
Yes, that looks great, @arianvp. To clarify, does MimeRender
refer to the following?
type UserAPI = "users" :> QueryParam "sortby" SortBy :> Get '[JSON] [User]
^^^^^^^^^^^^^^
I’d give it a shot to implement but I am still a beginner Haskell programmer. Happy to help in other ways though: I have quite a bit of experience building REST APIs with other languages, so I can share that and could also help out with docs, etc. Let me know.
I'm not entirely sure we could have a fully polymorphic e
, but if we can't, then:
data ServantErr cts = forall e. AllMimeRender e cts =>
ServantErr { errHTTPCode :: Int
, errReasonPhrase :: String
, errBody :: e
, errHeaders :: [HTTP.Header]
} deriving (Show, Eq, Read)
REST API error handling:
Facebook: https://developers.facebook.com/docs/graph-api/using-graph-api#errors
{
"error": {
"message": "Message describing the error",
"type": "OAuthException",
"code": 190,
"error_subcode": 460,
"error_user_title": "A title",
"error_user_msg": "A message",
"fbtrace_id": "EJplcsCHuLu"
}
}
Twitter: https://dev.twitter.com/overview/api/response-codes
{
"errors": [
{
"message": "Sorry, that page does not exist",
"code": 34
}
]
}
@gasi Yes it does. Check out the tutorial, it explains how JSON
relates to MimeRender
http://haskell-servant.github.io/tutorial/server.html#using-content-types-with-your-data-types
@arianvp 👍 I’ll check it out.
Hi.
When any endpoint on servant-server returns a
ServantErr
, it just puts theerrBody
as is. But the JS client generated withservant-js
expects a JSON object and tries to decode it; then it fails with a syntax error.I think either the server should encode the errors conforming with
Content-type
, or generated JavaScript client must not convert it from JSON and just give the response body to error callback.Example:
When calling the API with generated JavaScript, instead of calling the error handler, it fails with syntax error.