on master help sign in
Create nicely formatted error messages
commitb4a568cf48e516cf4411d35f0b2035de370d62e5 authorEvan Czaplicki <info@elm-lang.org> dateTue 5 May 2015, 13:48 (11 years ago) parentdd00462
3 files changed+34−13
src/Elm/Compiler.hs+3−3 src/Reporting/Error.hs+30−9 src/Reporting/Region.hs+1−1
src/Elm/Compiler.hs+3−3
@@ -83,9 +83,9 @@ compile user packageName source interfaces =
8383newtype Error = Error (A.Located Error.Error)
8484
8585
86−errorToString :: Error -> String
87−errorToString (Error err) =
88− Error.toString err
86+errorToString :: String -> String -> Error -> String
87+errorToString location source (Error err) =
88+ Error.toString location source err
8989
9090
9191errorToJson :: Error -> String
src/Reporting/Error.hs+30−9
@@ -9,6 +9,7 @@ import qualified Reporting.Annotation as A
99import qualified Reporting.Error.Canonicalize as Canonicalize
1010import qualified Reporting.Error.Syntax as Syntax
1111import qualified Reporting.Error.Type as Type
12+import qualified Reporting.Region as R
1213
1314
1415-- ALL POSSIBLE ERRORS
@@ -21,17 +22,37 @@ data Error
2122
2223-- TO STRING
2324
24−toString :: A.Located Error -> String
25−toString (A.A region err) =
26− case err of
27− Syntax syntaxError ->
28− error "TODO" syntaxError
25+toString :: String -> String -> A.Located Error -> String
26+toString location source (A.A region err) =
27+ let
28+ (tag, hint) =
29+ case err of
30+ Syntax syntaxError ->
31+ ( "SYNTAX ERROR"
32+ , Syntax.toHint syntaxError
33+ )
2934
30− Canonicalize canonicalizeError ->
31− Canonicalize.toString region canonicalizeError
35+ Canonicalize canonicalizeError ->
36+ ( "NAMING ERROR"
37+ , Canonicalize.toHint canonicalizeError
38+ )
3239
33− Type typeError ->
34− Type.toString region typeError
40+ Type typeError ->
41+ ( "TYPE ERROR"
42+ , Type.toHint typeError
43+ )
44+
45+ usedSpace =
46+ 4 + length tag + length location + 4
47+
48+ messageBar =
49+ "-- " ++ tag ++ " "
50+ ++ replicate (max 1 (80 - usedSpace)) '-'
51+ ++ " " ++ location ++ " --"
52+ in
53+ messageBar ++ "\n\n"
54+ ++ R.select region source ++ "\n"
55+ ++ hint ++ "\n\n"
3556
3657
3758-- JSON
src/Reporting/Region.hs+1−1
@@ -93,7 +93,7 @@ select (Region start end) source =
9393 [line start .. line end]
9494 focusedRelevantLines
9595 in
96− unlines numberedLines
96+ unlines numberedLines ++ "\n"
9797
9898
9999addLineNumber :: Int -> Int -> String -> String