Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newtype instead of bare string in MimeUnrender / ResultStream? #999

Open
phadej opened this issue Jun 24, 2018 · 3 comments
Open

newtype instead of bare string in MimeUnrender / ResultStream? #999

phadej opened this issue Jun 24, 2018 · 3 comments

Comments

@phadej
Copy link
Contributor

phadej commented Jun 24, 2018

Currently we have

mimeUnrender :: Proxy ctype -> ByteString -> Either String a

I think that we should have a wrapper over that String. The same error is also in ResultStream definition:

newtype ResultStream a = ResultStream (forall b. (IO (Maybe (Either String a)) -> IO b) -> IO b)

If newrype is too much at once, I'll be already happy with a type alias. I have no good candidate for the name though.

@phadej phadej changed the title newtype over parsing errors? newtype instead of bare string in MimeUnrender / ResultStream? Jun 24, 2018
@alpmestan
Copy link
Contributor

alpmestan commented Jul 28, 2018

ContentTypeError? BodyDecodingError? 😞

I'd probably be happy with a type alias.

jhmcstanton pushed a commit to jhmcstanton/servant that referenced this issue Oct 15, 2018
jhmcstanton added a commit to jhmcstanton/servant that referenced this issue Oct 15, 2018
@jhmcstanton
Copy link

Just made two type synonyms, one per module, with the name BodyDecodingError (see d5f3cf1). Any other changes needed?

@asheshambasta
Copy link

So, we're using servant for our REST API, where our clients are ensured everything from the API is going to be JSON.

Now, we've got custom json validation on our side, and I've implemented :

data ReadJSON

-- | Describes the "Accept" header required to be able to process requests of this type.
instance Accept ReadJSON where
  contentType _ = "application" // "json" /: ("charset", "utf-8")

-- | /Unrender/ a value from incoming HTTP data (`ByteString`)
instance JSONRead a => MimeUnrender ReadJSON a where
  mimeUnrender _ bs = do
    v <- eitherDecode bs -- aeson
    decodeJSONEitherText v & _Left %~ T.unpack -- JSONRead

Now, what I'd like to do is be able to have an equivalent of mimeUnrender that outputs some custom type that can also be returned: Eg.

class IsServantAPIErr err where 
  displayErr :: err -> String 
  errJSON :: err -> Value 

class (IsServantAPIErr (UnrenderErr a), Accept a) => MyMimeUnrender a where 
  type UnrenderErr a -- assoc. type that indicates the error type 
  mimeUnrender :: ByteString -> Either (UnrenderErr a) a 

What would be the best way to achieve this? Is there something I can do with the current system to implement something to this effect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants