on master help sign in
Get JSON output for compiler errors in a nicer format
ca6005cEvan Czaplicki
builder/src/Deps/Get.hs+2−2
@@ -123,7 +123,7 @@ info name version =
123 liftIO (BS.writeFile elmJson bits)
123 liftIO (BS.writeFile elmJson bits)
124 return bits
124 return bits
125
125
126− case Decode.parse "elm.json" "project" E.badContentToDocs Project.pkgDecoder json of
126+ case Decode.parse "project" E.badContentToDocs Project.pkgDecoder json of
127 Right pkgInfo ->
127 Right pkgInfo ->
128 return pkgInfo
128 return pkgInfo
129
129
@@ -151,7 +151,7 @@ docs name version =
151 liftIO (BS.writeFile docsJson bits)
151 liftIO (BS.writeFile docsJson bits)
152 return bits
152 return bits
153
153
154− case Decode.parse "docs.json" "docs" errorToDocs (Docs.toDict <$> Decode.list Docs.decoder) json of
154+ case Decode.parse "docs" errorToDocs (Docs.toDict <$> Decode.list Docs.decoder) json of
155 Right pkgInfo ->
155 Right pkgInfo ->
156 return pkgInfo
156 return pkgInfo
157
157
builder/src/Deps/Website.hs+4−4
@@ -133,12 +133,12 @@ fetchJson rootName errorToDocs decoder path =
133 Http.package path [] $ \request manager ->
133 Http.package path [] $ \request manager ->
134 do response <- Client.httpLbs request manager
134 do response <- Client.httpLbs request manager
135 let bytes = LBS.toStrict (Client.responseBody response)
135 let bytes = LBS.toStrict (Client.responseBody response)
136− case D.parse path rootName errorToDocs decoder bytes of
136+ case D.parse rootName errorToDocs decoder bytes of
137 Right value ->
137 Right value ->
138 return $ Right value
138 return $ Right value
139
139
140 Left jsonProblem ->
140 Left jsonProblem ->
141− return $ Left $ E.BadJson jsonProblem
141+ return $ Left $ E.BadJson path jsonProblem
142
142
143
143
144
144
@@ -281,12 +281,12 @@ githubCommit name version =
281 Http.run $ Http.anything endpoint $ \request manager ->
281 Http.run $ Http.anything endpoint $ \request manager ->
282 do response <- Client.httpLbs (request { Client.requestHeaders = headers }) manager
282 do response <- Client.httpLbs (request { Client.requestHeaders = headers }) manager
283 let bytes = LBS.toStrict (Client.responseBody response)
283 let bytes = LBS.toStrict (Client.responseBody response)
284− case D.parse "github.json" "github" id decoder bytes of
284+ case D.parse "github" id decoder bytes of
285 Right value ->
285 Right value ->
286 return $ Right value
286 return $ Right value
287
287
288 Left jsonProblem ->
288 Left jsonProblem ->
289− return $ Left $ E.BadJson jsonProblem
289+ return $ Left $ E.BadJson "github.json" jsonProblem
290
290
291
291
292
292
builder/src/Elm/Project/Json.hs+1−1
@@ -250,7 +250,7 @@ encodeVersion version =
250read :: FilePath -> Task.Task Project
250read :: FilePath -> Task.Task Project
251read path =
251read path =
252 do bytes <- liftIO $ BS.readFile path
252 do bytes <- liftIO $ BS.readFile path
253− case D.parse path "project" E.badContentToDocs decoder bytes of
253+ case D.parse "project" E.badContentToDocs decoder bytes of
254 Left err ->
254 Left err ->
255 throwBadJson (E.BadJson err)
255 throwBadJson (E.BadJson err)
256
256
builder/src/File/Header.hs+1−1
@@ -156,7 +156,7 @@ parse project path time source =
156 return ( maybeName, Info path time source deps )
156 return ( maybeName, Info path time source deps )
157
157
158 Left msg ->
158 Left msg ->
159− Task.throw (E.BadHeader path source msg)
159+ Task.throw (E.BadHeader path time source msg)
160
160
161
161
162checkTag :: Project -> FilePath -> Maybe (Header.Tag, Module.Raw) -> Task.Task_ E.Problem (Maybe Module.Raw)
162checkTag :: Project -> FilePath -> Maybe (Header.Tag, Module.Raw) -> Task.Task_ E.Problem (Maybe Module.Raw)
builder/src/Reporting/Exit.hs+1−1
@@ -96,7 +96,7 @@ toReport exit =
96 Bump.toReport bumpError
96 Bump.toReport bumpError
97
97
98 Compile e es ->
98 Compile e es ->
99− Help.compilerReport $ Compile.toDoc e es
99+ Help.compilerReport e es
100
100
101 Crawl crawlError ->
101 Crawl crawlError ->
102 Crawl.toReport crawlError
102 Crawl.toReport crawlError
builder/src/Reporting/Exit/Assets.hs+2−2
@@ -88,8 +88,8 @@ corruptJsonToReport path pkg vsn =
88elmJsonProblemToReport :: ElmJsonProblem -> Help.Report
88elmJsonProblemToReport :: ElmJsonProblem -> Help.Report
89elmJsonProblemToReport problem =
89elmJsonProblemToReport problem =
90 case problem of
90 case problem of
91− BadJson jsonErrorDoc ->
91+ BadJson doc ->
92− Help.compilerReport jsonErrorDoc
92+ Help.jsonReport "BAD JSON" (Just "elm.json") doc
93
93
94 BadSrcDir dir ->
94 BadSrcDir dir ->
95 Help.report "BAD JSON" (Just "elm.json")
95 Help.report "BAD JSON" (Just "elm.json")
builder/src/Reporting/Exit/Compile.hs+11
@@ -2,6 +2,7 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Exit.Compile
3module Reporting.Exit.Compile
4 ( Exit(..)
4 ( Exit(..)
5+ , toJson
5 , toDoc
6 , toDoc
6 )
7 )
7 where
8 where
@@ -15,6 +16,7 @@ import qualified Text.PrettyPrint.ANSI.Leijen as P
15
16
16import qualified Elm.Compiler as Compiler
17import qualified Elm.Compiler as Compiler
17import qualified Elm.Compiler.Module as Module
18import qualified Elm.Compiler.Module as Module
19+import qualified Json.Encode as Encode
18
20
19
21
20
22
@@ -32,6 +34,15 @@ data Exit =
32
34
33
35
34
36
37+-- TO JSON
38+
39+
40+toJson :: Exit -> Encode.Value
41+toJson (Exit name path _ source errors) =
42+ Compiler.errorsToJson name path source errors
43+
44+
45+
35-- TO DOC
46-- TO DOC
36
47
37
48
builder/src/Reporting/Exit/Crawl.hs+9−4
@@ -12,12 +12,14 @@ import qualified Data.ByteString as BS
12import qualified Data.Char as Char
12import qualified Data.Char as Char
13import qualified Data.Map as Map
13import qualified Data.Map as Map
14import qualified Data.Text.Encoding as Text
14import qualified Data.Text.Encoding as Text
15+import qualified Data.Time.Clock as Time
15import qualified Text.PrettyPrint.ANSI.Leijen as P
16import qualified Text.PrettyPrint.ANSI.Leijen as P
16import Text.PrettyPrint.ANSI.Leijen ((<+>), (<>))
17import Text.PrettyPrint.ANSI.Leijen ((<+>), (<>))
17
18
18import qualified Elm.Compiler as Compiler
19import qualified Elm.Compiler as Compiler
19import qualified Elm.Compiler.Module as Module
20import qualified Elm.Compiler.Module as Module
20import qualified Elm.Package as Pkg
21import qualified Elm.Package as Pkg
22+import qualified Reporting.Exit.Compile as Compile
21import qualified Reporting.Exit.Help as Help
23import qualified Reporting.Exit.Help as Help
22
24
23
25
@@ -36,7 +38,7 @@ data Exit
36data Problem
38data Problem
37 = ModuleNotFound Origin Module.Raw -- TODO suggest other names
39 = ModuleNotFound Origin Module.Raw -- TODO suggest other names
38 | ModuleAmbiguous Origin Module.Raw [FilePath] [Pkg.Package]
40 | ModuleAmbiguous Origin Module.Raw [FilePath] [Pkg.Package]
39− | BadHeader FilePath BS.ByteString Compiler.Error
41+ | BadHeader FilePath Time.UTCTime BS.ByteString Compiler.Error
40 | ModuleNameReservedForKernel Origin Module.Raw
42 | ModuleNameReservedForKernel Origin Module.Raw
41 | ModuleNameMissing FilePath Module.Raw
43 | ModuleNameMissing FilePath Module.Raw
42 | ModuleNameMismatch
44 | ModuleNameMismatch
@@ -117,9 +119,12 @@ problemToReport problem =
117 ModuleAmbiguous origin child paths pkgs ->
119 ModuleAmbiguous origin child paths pkgs ->
118 ambiguousToDoc origin child paths pkgs
120 ambiguousToDoc origin child paths pkgs
119
121
120− BadHeader path source compilerError ->
122+ BadHeader path time source compilerError ->
121− Help.compilerReport $
123+ let
124+ compilerExit =
122− Compiler.errorsToDoc path (Text.decodeUtf8 source) [compilerError]
125+ Compile.Exit "???" path time (Text.decodeUtf8 source) [compilerError]
126+ in
127+ Help.compilerReport compilerExit []
123
128
124 ModuleNameMissing path name ->
129 ModuleNameMissing path name ->
125 namelessToDoc path name
130 namelessToDoc path name
builder/src/Reporting/Exit/Help.hs+17−7
@@ -3,6 +3,7 @@ module Reporting.Exit.Help
3 ( Report
3 ( Report
4 , report
4 , report
5 , docReport
5 , docReport
6+ , jsonReport
6 , compilerReport
7 , compilerReport
7 , reportToDoc
8 , reportToDoc
8 , reportToJson
9 , reportToJson
@@ -27,6 +28,7 @@ import Text.PrettyPrint.ANSI.Leijen ((<>),(<+>))
27import qualified Elm.Compiler as Compiler
28import qualified Elm.Compiler as Compiler
28import qualified Elm.Package as Pkg
29import qualified Elm.Package as Pkg
29import qualified Json.Encode as Encode
30import qualified Json.Encode as Encode
31+import qualified Reporting.Exit.Compile as Compile
30
32
31
33
32
34
@@ -34,7 +36,7 @@ import qualified Json.Encode as Encode
34
36
35
37
36data Report
38data Report
37− = CompileErrors
39+ = CompilerReport Compile.Exit [Compile.Exit]
38 | Report
40 | Report
39 { _title :: String
41 { _title :: String
40 , _path :: Maybe FilePath
42 , _path :: Maybe FilePath
@@ -52,9 +54,14 @@ docReport title path startDoc others =
52 Report title path $ stack (startDoc:others)
54 Report title path $ stack (startDoc:others)
53
55
54
56
55−compilerReport :: P.Doc -> Report
57+jsonReport :: String -> Maybe FilePath -> P.Doc -> Report
58+jsonReport =
59+ Report
60+
61+
62+compilerReport :: Compile.Exit -> [Compile.Exit] -> Report
56compilerReport =
63compilerReport =
57− error "TODO compilerReport"
64+ CompilerReport
58
65
59
66
60
67
@@ -64,8 +71,8 @@ compilerReport =
64reportToDoc :: Report -> P.Doc
71reportToDoc :: Report -> P.Doc
65reportToDoc report_ =
72reportToDoc report_ =
66 case report_ of
73 case report_ of
67− CompileErrors ->
74+ CompilerReport e es ->
68− error "TODO reportToDoc"
75+ Compile.toDoc e es
69
76
70 Report title maybePath message ->
77 Report title maybePath message ->
71 let
78 let
@@ -94,8 +101,11 @@ reportToDoc report_ =
94reportToJson :: Report -> Encode.Value
101reportToJson :: Report -> Encode.Value
95reportToJson report_ =
102reportToJson report_ =
96 case report_ of
103 case report_ of
97− CompileErrors ->
104+ CompilerReport e es ->
98− error "TODO reportToJson"
105+ Encode.object
106+ [ ("type", Encode.text "compile-errors")
107+ , ("errors", Encode.list Compile.toJson (e:es))
108+ ]
99
109
100 Report title maybePath message ->
110 Report title maybePath message ->
101 let
111 let
builder/src/Reporting/Exit/Http.hs+3−3
@@ -21,7 +21,7 @@ import qualified Reporting.Exit.Help as Help
21
21
22data Exit
22data Exit
23 = Unknown String
23 = Unknown String
24− | BadJson P.Doc
24+ | BadJson String P.Doc
25 | BadZipData
25 | BadZipData
26 | BadZipSha String String
26 | BadZipSha String String
27
27
@@ -46,8 +46,8 @@ toReport url exit =
46 ]
46 ]
47 ]
47 ]
48
48
49− BadJson jsonErrorDoc ->
49+ BadJson path doc ->
50− Help.compilerReport jsonErrorDoc
50+ Help.jsonReport "UNEXPECTED JSON" (Just path) doc
51
51
52 BadZipData ->
52 BadZipData ->
53 Help.report "CORRUPT ZIP" Nothing "I could not unzip the file downloaded from:"
53 Help.report "CORRUPT ZIP" Nothing "I could not unzip the file downloaded from:"
compiler/src/Elm/Compiler.hs+3−2
@@ -79,14 +79,15 @@ errorsToDoc filePath source errors =
79-- ERRORS TO JSON
79-- ERRORS TO JSON
80
80
81
81
82−errorsToJson :: FilePath -> Text.Text -> [Error.Error] -> Encode.Value
82+errorsToJson :: M.Raw -> FilePath -> Text.Text -> [Error.Error] -> Encode.Value
83−errorsToJson filePath source errors =
83+errorsToJson moduleName filePath source errors =
84 let
84 let
85 reports =
85 reports =
86 concatMap (Error.toReports (Code.toSource source) Map.empty) errors
86 concatMap (Error.toReports (Code.toSource source) Map.empty) errors
87 in
87 in
88 Encode.object
88 Encode.object
89 [ ("path", Encode.text (Text.pack filePath))
89 [ ("path", Encode.text (Text.pack filePath))
90+ , ("name", Encode.name moduleName)
90 , ("errors", Encode.array (map reportToJson reports))
91 , ("errors", Encode.array (map reportToJson reports))
91 ]
92 ]
92
93
compiler/src/Json/Decode.hs+6−6
@@ -25,6 +25,7 @@ import qualified Data.Vector as Vector
25import Foreign.ForeignPtr (ForeignPtr)
25import Foreign.ForeignPtr (ForeignPtr)
26import GHC.Word (Word8(..))
26import GHC.Word (Word8(..))
27
27
28+import qualified Json.Decode.Error as DecodeError
28import qualified Json.Decode.Internals as Json
29import qualified Json.Decode.Internals as Json
29import qualified Parse.Primitives as P
30import qualified Parse.Primitives as P
30import qualified Parse.Primitives.Internals as I
31import qualified Parse.Primitives.Internals as I
@@ -33,7 +34,6 @@ import qualified Parse.Primitives.Keyword as Keyword
33import qualified Parse.Primitives.Number as Number
34import qualified Parse.Primitives.Number as Number
34import qualified Parse.Primitives.Symbol as Symbol
35import qualified Parse.Primitives.Symbol as Symbol
35import qualified Reporting.Error.Syntax as E
36import qualified Reporting.Error.Syntax as E
36−import qualified Reporting.Error.Json as J
37import qualified Reporting.Helpers as H
37import qualified Reporting.Helpers as H
38import qualified Reporting.Region as R
38import qualified Reporting.Region as R
39import qualified Reporting.Render.Code as Code
39import qualified Reporting.Render.Code as Code
@@ -43,23 +43,23 @@ import qualified Reporting.Render.Code as Code
43-- PARSE
43-- PARSE
44
44
45
45
46−parse :: FilePath -> String -> (e -> [H.Doc]) -> Json.Decoder e a -> B.ByteString -> Either H.Doc a
46+parse :: String -> (e -> [H.Doc]) -> Json.Decoder e a -> B.ByteString -> Either H.Doc a
47−parse path rootName userErrorToDocs (Json.Decoder run) bytestring =
47+parse rootName userErrorToDocs (Json.Decoder run) bytestring =
48 let
48 let
49 source =
49 source =
50 Code.toSource (Text.replace "\t" " " (Text.decodeUtf8 bytestring))
50 Code.toSource (Text.replace "\t" " " (Text.decodeUtf8 bytestring))
51
51
52 toDoc err =
52 toDoc err =
53− J.toDoc path rootName source userErrorToDocs err
53+ DecodeError.toDoc rootName source userErrorToDocs err
54 in
54 in
55 case P.run jsonFile bytestring of
55 case P.run jsonFile bytestring of
56 Left err ->
56 Left err ->
57− Left (toDoc (J.BadJson err))
57+ Left (toDoc (DecodeError.BadJson err))
58
58
59 Right value ->
59 Right value ->
60 case run value of
60 case run value of
61 Left err ->
61 Left err ->
62− Left (toDoc (J.BadContent err))
62+ Left (toDoc (DecodeError.BadContent err))
63
63
64 Right answer ->
64 Right answer ->
65 Right answer
65 Right answer
compiler/src/Reporting/Error/Json.hs → compiler/src/Json/Decode/Error.hsrenamed+6−9
@@ -1,7 +1,5 @@
1−{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
4−module Reporting.Error.Json
2+module Json.Decode.Error
5 ( Error(..)
3 ( Error(..)
6 , toDoc
4 , toDoc
7 )
5 )
@@ -20,7 +18,6 @@ import qualified Json.Decode.Internals as Json
20import qualified Json.Encode as E
18import qualified Json.Encode as E
21import qualified Reporting.Error.Syntax as Syntax
19import qualified Reporting.Error.Syntax as Syntax
22import qualified Reporting.Helpers as H
20import qualified Reporting.Helpers as H
23−import qualified Reporting.Region as R
24import qualified Reporting.Render.Code as Code
21import qualified Reporting.Render.Code as Code
25import qualified Reporting.Report as Report
22import qualified Reporting.Report as Report
26
23
@@ -38,15 +35,15 @@ data Error e
38-- TO DOC
35-- TO DOC
39
36
40
37
41−toDoc :: FilePath -> String -> Code.Source -> (e -> [H.Doc]) -> Error e -> H.Doc
38+toDoc :: String -> Code.Source -> (e -> [H.Doc]) -> Error e -> H.Doc
42−toDoc path rootName source userErrorToDocs err =
39+toDoc rootName source userErrorToDocs err =
43− Report.toDoc path $
44 case err of
40 case err of
45 BadJson syntaxError ->
41 BadJson syntaxError ->
46− Syntax.toReport source syntaxError
42+ case Syntax.toReport source syntaxError of
43+ Report.Report _ _ _ doc ->
44+ doc
47
45
48 BadContent jsonError ->
46 BadContent jsonError ->
49− Report.Report "BAD JSON" R.zero [] $
50 case flatten jsonError of
47 case flatten jsonError of
51 [] ->
48 [] ->
52 H.reflow
49 H.reflow
elm.cabal+1−1
@@ -168,6 +168,7 @@ Executable elm
168 Generate.JavaScript.Builder,
168 Generate.JavaScript.Builder,
169 Generate.JavaScript.Expression,
169 Generate.JavaScript.Expression,
170 Generate.JavaScript.Name,
170 Generate.JavaScript.Name,
171+ Json.Decode.Error,
171 Json.Decode.Internals,
172 Json.Decode.Internals,
172 Nitpick.PatternMatches,
173 Nitpick.PatternMatches,
173 Optimize.Case,
174 Optimize.Case,
@@ -198,7 +199,6 @@ Executable elm
198 Reporting.Error,
199 Reporting.Error,
199 Reporting.Error.Canonicalize,
200 Reporting.Error.Canonicalize,
200 Reporting.Error.Docs,
201 Reporting.Error.Docs,
201− Reporting.Error.Json,
202 Reporting.Error.Main,
202 Reporting.Error.Main,
203 Reporting.Error.Pattern,
203 Reporting.Error.Pattern,
204 Reporting.Error.Syntax,
204 Reporting.Error.Syntax,