on master help sign in
Support conversion from errors to JSON
caa8283Evan Czaplicki
src/Elm/Compiler.hs+7−1
@@ -2,10 +2,11 @@
2module Elm.Compiler
2module Elm.Compiler
3 ( version, rawVersion
3 ( version, rawVersion
4 , parseDependencies, compile
4 , parseDependencies, compile
5− , Error, errorToString
5+ , Error, errorToString, errorToJson
6 , Warning, warningToString
6 , Warning, warningToString
7 ) where
7 ) where
8
8
9+import qualified Data.Aeson as Json
9import qualified Data.Map as Map
10import qualified Data.Map as Map
10
11
11import qualified AST.Module as Module
12import qualified AST.Module as Module
@@ -89,6 +90,11 @@ errorToString location source (Error err) =
89 Error.toString location source err
90 Error.toString location source err
90
91
91
92
93+errorToJson :: String -> Error -> Json.Value
94+errorToJson location (Error err) =
95+ Error.toJson location err
96+
97+
92-- WARNINGS
98-- WARNINGS
93
99
94newtype Warning = Warning (A.Located Warning.Warning)
100newtype Warning = Warning (A.Located Warning.Warning)
src/Reporting/Error.hs+51
@@ -1,10 +1,16 @@
1+{-# OPTIONS_GHC -Wall #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2module Reporting.Error where
3module Reporting.Error where
3
4
5+import Data.Aeson ((.=))
6+import qualified Data.Aeson as Json
7+import qualified Data.Aeson.Types as Json
8+
4import qualified Reporting.Annotation as A
9import qualified Reporting.Annotation as A
5import qualified Reporting.Error.Canonicalize as Canonicalize
10import qualified Reporting.Error.Canonicalize as Canonicalize
6import qualified Reporting.Error.Syntax as Syntax
11import qualified Reporting.Error.Syntax as Syntax
7import qualified Reporting.Error.Type as Type
12import qualified Reporting.Error.Type as Type
13+import qualified Reporting.Region as Region
8import qualified Reporting.Report as Report
14import qualified Reporting.Report as Report
9
15
10
16
@@ -33,3 +39,48 @@ toString location source (A.A region err) =
33 Type.toReport typeError
39 Type.toReport typeError
34 in
40 in
35 Report.toString location region report source
41 Report.toString location region report source
42+
43+
44+-- TO JSON
45+
46+toJson :: String -> A.Located Error -> Json.Value
47+toJson location (A.A region err) =
48+ let
49+ (maybeRegion, additionalFields) =
50+ case err of
51+ Syntax syntaxError ->
52+ reportToJson [] (Syntax.toReport syntaxError)
53+
54+ Canonicalize canonicalizeError ->
55+ let
56+ suggestions =
57+ maybe []
58+ (\s -> ["suggestions" .= s])
59+ (Canonicalize.extractSuggestions canonicalizeError)
60+ in
61+ reportToJson suggestions (Canonicalize.toReport canonicalizeError)
62+
63+ Type typeError ->
64+ reportToJson [] (Type.toReport typeError)
65+ in
66+ Json.object $
67+ [ "file" .= location
68+ , "region" .= maybe region id maybeRegion
69+ , "type" .= ("error" :: String)
70+ ]
71+ ++ additionalFields
72+
73+
74+reportToJson
75+ :: [Json.Pair]
76+ -> Report.Report
77+ -> (Maybe Region.Region, [Json.Pair])
78+reportToJson extraFields (Report.Report title subregion pre post) =
79+ let
80+ fields =
81+ [ "tag" .= title
82+ , "overview" .= pre
83+ , "details" .= post
84+ ]
85+ in
86+ (subregion, fields ++ extraFields)
src/Reporting/Error/Canonicalize.hs+26−1
@@ -1,5 +1,4 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Canonicalize where
2module Reporting.Error.Canonicalize where
4
3
5import Data.Function (on)
4import Data.Function (on)
@@ -257,3 +256,29 @@ maybeYouWant suggestions =
257 "Maybe you want one of the following?\n"
256 "Maybe you want one of the following?\n"
258 ++ concatMap ("\n "++) suggestions
257 ++ concatMap ("\n "++) suggestions
259
258
259+
260+extractSuggestions :: Error -> Maybe [String]
261+extractSuggestions err =
262+ case err of
263+ Var (VarError _ _ _ suggestions) ->
264+ Just suggestions
265+
266+ Pattern _ ->
267+ Nothing
268+
269+ Alias _ ->
270+ Nothing
271+
272+ Import _ importError ->
273+ case importError of
274+ ModuleNotFound suggestions ->
275+ Just (map Module.nameToString suggestions)
276+
277+ ValueNotFound _ suggestions ->
278+ Just suggestions
279+
280+ Export _ suggestions ->
281+ Just suggestions
282+
283+ Port _ ->
284+ Nothing