on master help sign in
Get JSON output for compiler errors in a nicer format
commitca6005ce59d4de841f6eb0b3a4311d3651da1a9b authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 28 Mar 2018, 15:26 (9 years ago) parent74fdf60
14 files changed+67−43
builder/src/Deps/Get.hs+2−2 builder/src/Deps/Website.hs+4−4 builder/src/Elm/Project/Json.hs+1−1 builder/src/File/Header.hs+1−1 builder/src/Reporting/Exit.hs+1−1 builder/src/Reporting/Exit/Assets.hs+2−2 builder/src/Reporting/Exit/Compile.hs+11 builder/src/Reporting/Exit/Crawl.hs+9−4 builder/src/Reporting/Exit/Help.hs+17−7 builder/src/Reporting/Exit/Http.hs+3−3 compiler/src/Elm/Compiler.hs+3−2 compiler/src/Json/Decode.hs+6−6 compiler/src/Reporting/Error/Json.hs → compiler/src/Json/Decode/Error.hs+6−9 elm.cabal+1−1
builder/src/Deps/Get.hs+2−2
@@ -123,7 +123,7 @@ info name version =
123123 liftIO (BS.writeFile elmJson bits)
124124 return bits
125125
126− case Decode.parse "elm.json" "project" E.badContentToDocs Project.pkgDecoder json of
126+ case Decode.parse "project" E.badContentToDocs Project.pkgDecoder json of
127127 Right pkgInfo ->
128128 return pkgInfo
129129
@@ -151,7 +151,7 @@ docs name version =
151151 liftIO (BS.writeFile docsJson bits)
152152 return bits
153153
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
155155 Right pkgInfo ->
156156 return pkgInfo
157157
builder/src/Deps/Website.hs+4−4
@@ -133,12 +133,12 @@ fetchJson rootName errorToDocs decoder path =
133133 Http.package path [] $ \request manager ->
134134 do response <- Client.httpLbs request manager
135135 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
137137 Right value ->
138138 return $ Right value
139139
140140 Left jsonProblem ->
141− return $ Left $ E.BadJson jsonProblem
141+ return $ Left $ E.BadJson path jsonProblem
142142
143143
144144
@@ -281,12 +281,12 @@ githubCommit name version =
281281 Http.run $ Http.anything endpoint $ \request manager ->
282282 do response <- Client.httpLbs (request { Client.requestHeaders = headers }) manager
283283 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
285285 Right value ->
286286 return $ Right value
287287
288288 Left jsonProblem ->
289− return $ Left $ E.BadJson jsonProblem
289+ return $ Left $ E.BadJson "github.json" jsonProblem
290290
291291
292292
builder/src/Elm/Project/Json.hs+1−1
@@ -250,7 +250,7 @@ encodeVersion version =
250250read :: FilePath -> Task.Task Project
251251read path =
252252 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
254254 Left err ->
255255 throwBadJson (E.BadJson err)
256256
builder/src/File/Header.hs+1−1
@@ -156,7 +156,7 @@ parse project path time source =
156156 return ( maybeName, Info path time source deps )
157157
158158 Left msg ->
159− Task.throw (E.BadHeader path source msg)
159+ Task.throw (E.BadHeader path time source msg)
160160
161161
162162checkTag :: 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 =
9696 Bump.toReport bumpError
9797
9898 Compile e es ->
99− Help.compilerReport $ Compile.toDoc e es
99+ Help.compilerReport e es
100100
101101 Crawl crawlError ->
102102 Crawl.toReport crawlError
builder/src/Reporting/Exit/Assets.hs+2−2
@@ -88,8 +88,8 @@ corruptJsonToReport path pkg vsn =
8888elmJsonProblemToReport :: ElmJsonProblem -> Help.Report
8989elmJsonProblemToReport problem =
9090 case problem of
91− BadJson jsonErrorDoc ->
92− Help.compilerReport jsonErrorDoc
91+ BadJson doc ->
92+ Help.jsonReport "BAD JSON" (Just "elm.json") doc
9393
9494 BadSrcDir dir ->
9595 Help.report "BAD JSON" (Just "elm.json")
builder/src/Reporting/Exit/Compile.hs+11
@@ -2,6 +2,7 @@
22{-# LANGUAGE OverloadedStrings #-}
33module Reporting.Exit.Compile
44 ( Exit(..)
5+ , toJson
56 , toDoc
67 )
78 where
@@ -15,6 +16,7 @@ import qualified Text.PrettyPrint.ANSI.Leijen as P
1516
1617import qualified Elm.Compiler as Compiler
1718import qualified Elm.Compiler.Module as Module
19+import qualified Json.Encode as Encode
1820
1921
2022
@@ -32,6 +34,15 @@ data Exit =
3234
3335
3436
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+
3546-- TO DOC
3647
3748
builder/src/Reporting/Exit/Crawl.hs+9−4
@@ -12,12 +12,14 @@ import qualified Data.ByteString as BS
1212import qualified Data.Char as Char
1313import qualified Data.Map as Map
1414import qualified Data.Text.Encoding as Text
15+import qualified Data.Time.Clock as Time
1516import qualified Text.PrettyPrint.ANSI.Leijen as P
1617import Text.PrettyPrint.ANSI.Leijen ((<+>), (<>))
1718
1819import qualified Elm.Compiler as Compiler
1920import qualified Elm.Compiler.Module as Module
2021import qualified Elm.Package as Pkg
22+import qualified Reporting.Exit.Compile as Compile
2123import qualified Reporting.Exit.Help as Help
2224
2325
@@ -36,7 +38,7 @@ data Exit
3638data Problem
3739 = ModuleNotFound Origin Module.Raw -- TODO suggest other names
3840 | ModuleAmbiguous Origin Module.Raw [FilePath] [Pkg.Package]
39− | BadHeader FilePath BS.ByteString Compiler.Error
41+ | BadHeader FilePath Time.UTCTime BS.ByteString Compiler.Error
4042 | ModuleNameReservedForKernel Origin Module.Raw
4143 | ModuleNameMissing FilePath Module.Raw
4244 | ModuleNameMismatch
@@ -117,9 +119,12 @@ problemToReport problem =
117119 ModuleAmbiguous origin child paths pkgs ->
118120 ambiguousToDoc origin child paths pkgs
119121
120− BadHeader path source compilerError ->
121− Help.compilerReport $
122− Compiler.errorsToDoc path (Text.decodeUtf8 source) [compilerError]
122+ BadHeader path time source compilerError ->
123+ let
124+ compilerExit =
125+ Compile.Exit "???" path time (Text.decodeUtf8 source) [compilerError]
126+ in
127+ Help.compilerReport compilerExit []
123128
124129 ModuleNameMissing path name ->
125130 namelessToDoc path name
builder/src/Reporting/Exit/Help.hs+17−7
@@ -3,6 +3,7 @@ module Reporting.Exit.Help
33 ( Report
44 , report
55 , docReport
6+ , jsonReport
67 , compilerReport
78 , reportToDoc
89 , reportToJson
@@ -27,6 +28,7 @@ import Text.PrettyPrint.ANSI.Leijen ((<>),(<+>))
2728import qualified Elm.Compiler as Compiler
2829import qualified Elm.Package as Pkg
2930import qualified Json.Encode as Encode
31+import qualified Reporting.Exit.Compile as Compile
3032
3133
3234
@@ -34,7 +36,7 @@ import qualified Json.Encode as Encode
3436
3537
3638data Report
37− = CompileErrors
39+ = CompilerReport Compile.Exit [Compile.Exit]
3840 | Report
3941 { _title :: String
4042 , _path :: Maybe FilePath
@@ -52,9 +54,14 @@ docReport title path startDoc others =
5254 Report title path $ stack (startDoc:others)
5355
5456
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
5663compilerReport =
57− error "TODO compilerReport"
64+ CompilerReport
5865
5966
6067
@@ -64,8 +71,8 @@ compilerReport =
6471reportToDoc :: Report -> P.Doc
6572reportToDoc report_ =
6673 case report_ of
67− CompileErrors ->
68− error "TODO reportToDoc"
74+ CompilerReport e es ->
75+ Compile.toDoc e es
6976
7077 Report title maybePath message ->
7178 let
@@ -94,8 +101,11 @@ reportToDoc report_ =
94101reportToJson :: Report -> Encode.Value
95102reportToJson report_ =
96103 case report_ of
97− CompileErrors ->
98− error "TODO reportToJson"
104+ CompilerReport e es ->
105+ Encode.object
106+ [ ("type", Encode.text "compile-errors")
107+ , ("errors", Encode.list Compile.toJson (e:es))
108+ ]
99109
100110 Report title maybePath message ->
101111 let
builder/src/Reporting/Exit/Http.hs+3−3
@@ -21,7 +21,7 @@ import qualified Reporting.Exit.Help as Help
2121
2222data Exit
2323 = Unknown String
24− | BadJson P.Doc
24+ | BadJson String P.Doc
2525 | BadZipData
2626 | BadZipSha String String
2727
@@ -46,8 +46,8 @@ toReport url exit =
4646 ]
4747 ]
4848
49− BadJson jsonErrorDoc ->
50− Help.compilerReport jsonErrorDoc
49+ BadJson path doc ->
50+ Help.jsonReport "UNEXPECTED JSON" (Just path) doc
5151
5252 BadZipData ->
5353 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 =
7979-- ERRORS TO JSON
8080
8181
82−errorsToJson :: FilePath -> Text.Text -> [Error.Error] -> Encode.Value
83−errorsToJson filePath source errors =
82+errorsToJson :: M.Raw -> FilePath -> Text.Text -> [Error.Error] -> Encode.Value
83+errorsToJson moduleName filePath source errors =
8484 let
8585 reports =
8686 concatMap (Error.toReports (Code.toSource source) Map.empty) errors
8787 in
8888 Encode.object
8989 [ ("path", Encode.text (Text.pack filePath))
90+ , ("name", Encode.name moduleName)
9091 , ("errors", Encode.array (map reportToJson reports))
9192 ]
9293
compiler/src/Json/Decode.hs+6−6
@@ -25,6 +25,7 @@ import qualified Data.Vector as Vector
2525import Foreign.ForeignPtr (ForeignPtr)
2626import GHC.Word (Word8(..))
2727
28+import qualified Json.Decode.Error as DecodeError
2829import qualified Json.Decode.Internals as Json
2930import qualified Parse.Primitives as P
3031import qualified Parse.Primitives.Internals as I
@@ -33,7 +34,6 @@ import qualified Parse.Primitives.Keyword as Keyword
3334import qualified Parse.Primitives.Number as Number
3435import qualified Parse.Primitives.Symbol as Symbol
3536import qualified Reporting.Error.Syntax as E
36−import qualified Reporting.Error.Json as J
3737import qualified Reporting.Helpers as H
3838import qualified Reporting.Region as R
3939import qualified Reporting.Render.Code as Code
@@ -43,23 +43,23 @@ import qualified Reporting.Render.Code as Code
4343-- PARSE
4444
4545
46−parse :: FilePath -> String -> (e -> [H.Doc]) -> Json.Decoder e a -> B.ByteString -> Either H.Doc a
47−parse path rootName userErrorToDocs (Json.Decoder run) bytestring =
46+parse :: String -> (e -> [H.Doc]) -> Json.Decoder e a -> B.ByteString -> Either H.Doc a
47+parse rootName userErrorToDocs (Json.Decoder run) bytestring =
4848 let
4949 source =
5050 Code.toSource (Text.replace "\t" " " (Text.decodeUtf8 bytestring))
5151
5252 toDoc err =
53− J.toDoc path rootName source userErrorToDocs err
53+ DecodeError.toDoc rootName source userErrorToDocs err
5454 in
5555 case P.run jsonFile bytestring of
5656 Left err ->
57− Left (toDoc (J.BadJson err))
57+ Left (toDoc (DecodeError.BadJson err))
5858
5959 Right value ->
6060 case run value of
6161 Left err ->
62− Left (toDoc (J.BadContent err))
62+ Left (toDoc (DecodeError.BadContent err))
6363
6464 Right answer ->
6565 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 #-}
31{-# LANGUAGE OverloadedStrings #-}
4−module Reporting.Error.Json
2+module Json.Decode.Error
53 ( Error(..)
64 , toDoc
75 )
@@ -20,7 +18,6 @@ import qualified Json.Decode.Internals as Json
2018import qualified Json.Encode as E
2119import qualified Reporting.Error.Syntax as Syntax
2220import qualified Reporting.Helpers as H
23−import qualified Reporting.Region as R
2421import qualified Reporting.Render.Code as Code
2522import qualified Reporting.Report as Report
2623
@@ -38,15 +35,15 @@ data Error e
3835-- TO DOC
3936
4037
41−toDoc :: FilePath -> String -> Code.Source -> (e -> [H.Doc]) -> Error e -> H.Doc
42−toDoc path rootName source userErrorToDocs err =
43− Report.toDoc path $
38+toDoc :: String -> Code.Source -> (e -> [H.Doc]) -> Error e -> H.Doc
39+toDoc rootName source userErrorToDocs err =
4440 case err of
4541 BadJson syntaxError ->
46− Syntax.toReport source syntaxError
42+ case Syntax.toReport source syntaxError of
43+ Report.Report _ _ _ doc ->
44+ doc
4745
4846 BadContent jsonError ->
49− Report.Report "BAD JSON" R.zero [] $
5047 case flatten jsonError of
5148 [] ->
5249 H.reflow
elm.cabal+1−1
@@ -168,6 +168,7 @@ Executable elm
168168 Generate.JavaScript.Builder,
169169 Generate.JavaScript.Expression,
170170 Generate.JavaScript.Name,
171+ Json.Decode.Error,
171172 Json.Decode.Internals,
172173 Nitpick.PatternMatches,
173174 Optimize.Case,
@@ -198,7 +199,6 @@ Executable elm
198199 Reporting.Error,
199200 Reporting.Error.Canonicalize,
200201 Reporting.Error.Docs,
201− Reporting.Error.Json,
202202 Reporting.Error.Main,
203203 Reporting.Error.Pattern,
204204 Reporting.Error.Syntax,