keathley / pointfree.io

A web site for converting haskell code into pointfree haskell code
http://pointfree.io
MIT License
162 stars 7 forks source link

Code snippet produces 500 - Internal Server Error #8

Open clupasq opened 7 years ago

clupasq commented 7 years ago

The following code:

f n = and[mod n i>0|i<-[2..n-1]]

Produces an HTTP 500 error.

axionbuster commented 4 months ago

Add this code:

import Control.Exception (SomeException, catch, evaluate)

ignore :: SomeException -> Maybe a
ignore _ = Nothing

-- in `get "/snippet" $ do ...`
c <- param "code"
d <- liftIO $ catch (evaluate $ pointfree' c) (return . ignore)
case d of
  Nothing -> json $ Error "..."
  Just pf -> json $ ...

The problem is that, in the pointfree package, unsupported constructs invoke the internal todo function, which raises an error.

As a workaround, catch the error using the catch function from Control.Exception. As a note, it's necessary to force the evaluation of pointfree' c using evaluate.