on 2fbfb20 help sign in
elk/src/Reporting/Error.hs
69 lines 1.8 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error where
5import Data.Aeson ((.=))
6import qualified Data.Aeson as Json
8import qualified Reporting.Annotation as A
9import qualified Reporting.Error.Canonicalize as Canonicalize
10import qualified Reporting.Error.Syntax as Syntax
11import qualified Reporting.Error.Type as Type
12import qualified Reporting.Report as Report
15-- ALL POSSIBLE ERRORS
17data Error
18 = Syntax Syntax.Error
19 | Canonicalize Canonicalize.Error
20 | Type Type.Error
23-- TO STRING
25toString :: String -> String -> A.Located Error -> String
26toString location source (A.A region err) =
27 let
28 report =
29 case err of
30 Syntax syntaxError ->
31 Syntax.toReport syntaxError
33 Canonicalize canonicalizeError ->
34 Canonicalize.toReport canonicalizeError
36 Type typeError ->
37 Type.toReport typeError
38 in
39 Report.toString location region report source
42-- TO JSON
44toJson :: FilePath -> A.Located Error -> Json.Value
45toJson filePath (A.A region err) =
46 let
47 (maybeRegion, additionalFields) =
48 case err of
49 Syntax syntaxError ->
50 Report.toJson [] (Syntax.toReport syntaxError)
52 Canonicalize canonicalizeError ->
53 let
54 suggestions =
55 maybe []
56 (\s -> ["suggestions" .= s])
57 (Canonicalize.extractSuggestions canonicalizeError)
58 in
59 Report.toJson suggestions (Canonicalize.toReport canonicalizeError)
61 Type typeError ->
62 Report.toJson [] (Type.toReport typeError)
63 in
64 Json.object $
65 [ "file" .= filePath
66 , "region" .= maybe region id maybeRegion
67 , "type" .= ("error" :: String)
68 ]
69 ++ additionalFields