on master help sign in
Support conversion from errors to JSON
commitcaa8283ad2eae64cde39850028af25ee9997863a authorEvan Czaplicki <info@elm-lang.org> dateTue 12 May 2015, 12:14 (11 years ago) parentaf717eb
3 files changed+84−2
src/Elm/Compiler.hs+7−1 src/Reporting/Error.hs+51 src/Reporting/Error/Canonicalize.hs+26−1
src/Elm/Compiler.hs+7−1
@@ -2,10 +2,11 @@
22module Elm.Compiler
33 ( version, rawVersion
44 , parseDependencies, compile
5− , Error, errorToString
5+ , Error, errorToString, errorToJson
66 , Warning, warningToString
77 ) where
88
9+import qualified Data.Aeson as Json
910import qualified Data.Map as Map
1011
1112import qualified AST.Module as Module
@@ -89,6 +90,11 @@ errorToString location source (Error err) =
8990 Error.toString location source err
9091
9192
93+errorToJson :: String -> Error -> Json.Value
94+errorToJson location (Error err) =
95+ Error.toJson location err
96+
97+
9298-- WARNINGS
9399
94100newtype Warning = Warning (A.Located Warning.Warning)
src/Reporting/Error.hs+51
@@ -1,10 +1,16 @@
1+{-# OPTIONS_GHC -Wall #-}
12{-# LANGUAGE OverloadedStrings #-}
23module Reporting.Error where
34
5+import Data.Aeson ((.=))
6+import qualified Data.Aeson as Json
7+import qualified Data.Aeson.Types as Json
8+
49import qualified Reporting.Annotation as A
510import qualified Reporting.Error.Canonicalize as Canonicalize
611import qualified Reporting.Error.Syntax as Syntax
712import qualified Reporting.Error.Type as Type
13+import qualified Reporting.Region as Region
814import qualified Reporting.Report as Report
915
1016
@@ -33,3 +39,48 @@ toString location source (A.A region err) =
3339 Type.toReport typeError
3440 in
3541 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 @@
11{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE OverloadedStrings #-}
32module Reporting.Error.Canonicalize where
43
54import Data.Function (on)
@@ -257,3 +256,29 @@ maybeYouWant suggestions =
257256 "Maybe you want one of the following?\n"
258257 ++ concatMap ("\n "++) suggestions
259258
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