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

Safe JSON response helper #59

Open
singpolyma opened this issue Apr 26, 2014 · 2 comments
Open

Safe JSON response helper #59

singpolyma opened this issue Apr 26, 2014 · 2 comments

Comments

@singpolyma
Copy link
Contributor

I currently use:

safeJSONresponse :: (Aeson.FromJSON a) => Response -> InputStream ByteString - > IO (Either Error (Response, a))
safeJSONresponse resp i = runUnexceptionalIO $ runEitherT $ do
   v <- fmapLT (\e -> handle e (fromException e)) $ fromIO $
      parseFromStream Aeson.json' i
   case Aeson.fromJSON v of
      Aeson.Success a -> return (resp, a)
      Aeson.Error _ -> throwT parseError
   where
   parseError = Error Unavailable (T.pack "JSON parse error")
   handle _ (Just (ParseException _)) = parseError
   handle e _ = Error Unavailable (T.pack $ "Exception: " ++ show e)

For something generic to go into the library, this would probably be simplified to use Maybe and only catch the parse error -- other exceptions would still go through.

@istathar
Copy link
Member

So you obviously know about jsonHandler. Is the problem that exceptions come out of it?

AfC

@singpolyma
Copy link
Contributor Author

@afcowie my specific issue is that parse errors are exceptions with it (because parseFromStream can throw a ParseException separately from aeson returning Error)

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

No branches or pull requests

2 participants