@@ -1,10 +1,16 @@
1+{-# OPTIONS_GHC -Wall #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
2module Reporting.Error where
3module Reporting.Error where
5+import Data.Aeson ((.=))
6+import qualified Data.Aeson as Json
7+import qualified Data.Aeson.Types as Json
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
@@ -33,3 +39,48 @@ toString location source (A.A region err) =
33 Type.toReport typeError
39 Type.toReport typeError
35 Report.toString location region report source
41 Report.toString location region report source
46+toJson :: String -> A.Located Error -> Json.Value
47+toJson location (A.A region err) =
49+ (maybeRegion, additionalFields) =
51+ Syntax syntaxError ->
52+ reportToJson [] (Syntax.toReport syntaxError)
54+ Canonicalize canonicalizeError ->
58+ (\s -> ["suggestions" .= s])
59+ (Canonicalize.extractSuggestions canonicalizeError)
61+ reportToJson suggestions (Canonicalize.toReport canonicalizeError)
64+ reportToJson [] (Type.toReport typeError)
68+ , "region" .= maybe region id maybeRegion
69+ , "type" .= ("error" :: String)
77+ -> (Maybe Region.Region, [Json.Pair])
78+reportToJson extraFields (Report.Report title subregion pre post) =
86+ (subregion, fields ++ extraFields)