Skip to content

Commit 260b743

Browse files
authored
Merge pull request #51 from purescript/phil/json-errors
JSON errors
2 parents ab82510 + 4446d85 commit 260b743

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

server/Main.hs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
-----------------------------------------------------------------------------
2-
--
3-
-- Module : Main
4-
-- Copyright : (c) Phil Freeman 2013-2015
5-
-- License : MIT
6-
--
7-
-- Maintainer : [email protected]
8-
-- Stability :
9-
-- Portability :
10-
--
11-
-- |
12-
--
13-
-----------------------------------------------------------------------------
14-
151
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveAnyClass #-}
3+
{-# LANGUAGE DeriveGeneric #-}
164
{-# LANGUAGE OverloadedStrings #-}
175
{-# LANGUAGE TupleSections #-}
186

19-
module Main (
20-
main
21-
) where
7+
module Main (main) where
228

239
import Control.Monad (unless)
2410
import Control.Monad.IO.Class (liftIO)
@@ -37,10 +23,12 @@ import qualified Data.Text as T
3723
import qualified Data.Text.Encoding as T
3824
import qualified Data.Text.Lazy as TL
3925
import Data.Traversable (for)
26+
import GHC.Generics (Generic)
4027
import qualified Language.PureScript as P
4128
import qualified Language.PureScript.Bundle as Bundle
4229
import qualified Language.PureScript.CodeGen.JS as J
4330
import qualified Language.PureScript.CoreFn as CF
31+
import qualified Language.PureScript.Errors.JSON as P
4432
import qualified Language.PureScript.Interactive as I
4533
import System.Environment (getArgs)
4634
import System.Exit (exitFailure)
@@ -51,18 +39,25 @@ import System.IO.UTF8 (readUTF8File)
5139
import Web.Scotty
5240
import qualified Web.Scotty as Scotty
5341

54-
type JS = String
42+
type JS = Text
43+
44+
data Error
45+
= CompilerErrors [P.JSONError]
46+
| OtherError Text
47+
deriving Generic
48+
49+
instance A.ToJSON Error
5550

5651
server :: TL.Text -> [P.ExternsFile] -> P.Environment -> Int -> IO ()
5752
server bundled externs initEnv port = do
58-
let compile :: Text -> IO (Either String JS)
53+
let compile :: Text -> IO (Either Error JS)
5954
compile input
60-
| T.length input > 20000 = return $ Left "Please limit your input to 20000 characters"
55+
| T.length input > 20000 = return (Left (OtherError "Please limit your input to 20000 characters"))
6156
| otherwise = do
6257
let printErrors = P.prettyPrintMultipleErrors (P.defaultPPEOptions { P.ppeCodeColor = Nothing })
6358
case P.parseModuleFromFile (const "<file>") (undefined, input) of
6459
Left parseError ->
65-
return . Left . printErrors . P.MultipleErrors . return . P.toPositionedError $ parseError
60+
return . Left . CompilerErrors . pure . P.toJSONError False P.Error . P.toPositionedError $ parseError
6661
Right (_, m) | P.getModuleName m == P.ModuleName [P.ProperName "Main"] -> do
6762
(resultMay, _) <- runLogger' . runExceptT . flip runReaderT P.defaultOptions $ do
6863
((P.Module ss coms moduleName elaborated exps, env), nextVar) <- P.runSupplyT 0 $ do
@@ -75,9 +70,9 @@ server bundled externs initEnv port = do
7570
unless (null . CF.moduleForeign $ renamed) . throwError . P.errorMessage $ P.MissingFFIModule moduleName
7671
P.evalSupplyT nextVar $ P.prettyPrintJS <$> J.moduleToJs renamed Nothing
7772
case resultMay of
78-
Left errs -> return . Left . printErrors $ errs
79-
Right js -> return (Right js)
80-
Right _ -> return $ Left "The name of the main module should be Main."
73+
Left errs -> (return . Left . CompilerErrors . P.toJSONErrors False P.Error) errs
74+
Right js -> (return . Right) js
75+
Right _ -> (return . Left. OtherError) "The name of the main module should be Main."
8176

8277
scotty port $ do
8378
get "/" $

stack.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
resolver: lts-6.10
1+
resolver: lts-6.25
22
flags: {}
33
packages:
44
- '.'
55
extra-deps:
6-
- purescript-0.10.2
6+
- purescript-0.10.3
77
- bower-json-0.8.0
8-
- language-javascript-0.6.0.4
8+
- language-javascript-0.6.0.9
99
- parsec-3.1.11

trypurescript.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: trypurescript
2-
version: 0.9.1
2+
version: 0.10.3
33
cabal-version: >=1.8
44
build-type: Simple
5-
license: MIT
5+
license: BSD3
66
license-file: LICENSE
77
copyright: (c) Phil Freeman 2013
88
maintainer: [email protected]
@@ -20,7 +20,7 @@ executable trypurescript
2020
filepath -any,
2121
Glob -any,
2222
scotty -any,
23-
purescript ==0.10.2,
23+
purescript ==0.10.3,
2424
containers -any,
2525
http-types >= 0.8.5,
2626
transformers ==0.4.*,

0 commit comments

Comments
 (0)