on master help sign in
Check in Error.Main with errors for bad flags
commit5aa56362bc56ae4146d5b985df35bef75c0379f5 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 2 Feb 2018, 00:12 (9 years ago) parenta3099a7
1 file changed+122
src/Reporting/Error/Main.hs+122
src/Reporting/Error/Main.hsnew file+122
@@ -0,0 +1,122 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module Reporting.Error.Main
4+ ( Error(..)
5+ , toReport
6+ )
7+ where
8+
9+
10+import qualified AST.Canonical as Can
11+import qualified Elm.Name as N
12+import qualified Reporting.Error.Canonicalize as E
13+import qualified Reporting.Helpers as H
14+import qualified Reporting.Region as R
15+import qualified Reporting.Render.Code as Code
16+import qualified Reporting.Render.Type as RenderType
17+import qualified Reporting.Report as Report
18+
19+
20+
21+-- ERROR
22+
23+
24+data Error
25+ = BadType R.Region Can.Type
26+ | BadCycle R.Region [N.Name]
27+ | BadFlags R.Region Can.Type E.InvalidPayload
28+
29+
30+
31+-- TO REPORT
32+
33+
34+toReport :: Code.Source -> RenderType.Localizer -> Error -> Report.Report
35+toReport source localizer err =
36+ case err of
37+ BadType region tipe ->
38+ Report.Report "BAD MAIN TYPE" region [] $
39+ Report.toCodeSnippet source region Nothing
40+ (
41+ "I cannot handle this type of `main` value:"
42+ ,
43+ H.stack
44+ [ "The type of `main` value I am seeing is:"
45+ , H.indent 4 (RenderType.toDoc localizer tipe)
46+ , H.reflow $
47+ "I only know how to handle Html, Svg, and Programs\
48+ \ though. Modify `main` to be one of those types of values!"
49+ ]
50+ )
51+
52+ BadCycle region cycleNames ->
53+ Report.Report "BAD MAIN" region [] $
54+ Report.toCodeSnippet source region Nothing
55+ (
56+ "A `main` definition cannot be defined in terms of itself."
57+ ,
58+ H.stack
59+ [ H.reflow $
60+ "It should be a boring value with no recursion. But\
61+ \ instead it is involved in this cycle of definitions:"
62+ , H.indent 4 (H.drawCycle cycleNames)
63+ ]
64+ )
65+
66+ BadFlags region _badType invalidPayload ->
67+ let
68+ formatDetails (aBadKindOfThing, butThatIsNoGood) =
69+ Report.Report "BAD FLAGS" region [] $
70+ Report.toCodeSnippet source region Nothing
71+ (
72+ H.reflow $
73+ "Your `main` program wants " ++ aBadKindOfThing ++ " from JavaScript."
74+ ,
75+ butThatIsNoGood
76+ )
77+ in
78+ formatDetails $
79+ case invalidPayload of
80+ E.ExtendedRecord ->
81+ (
82+ "an extended record"
83+ ,
84+ H.reflow $
85+ "But the exact shape of the record must be known at compile time. No type variables!"
86+ )
87+
88+ E.Function ->
89+ (
90+ "a function"
91+ ,
92+ H.reflow $
93+ "But if I allowed functions from JS, it would be possible to sneak\
94+ \ side-effects and runtime exceptions into Elm!"
95+ )
96+
97+ E.TypeVariable name ->
98+ (
99+ "an unspecified type"
100+ ,
101+ H.reflow $
102+ "But type variables like `" ++ N.toString name ++ "` cannot be given as flags.\
103+ \ I need to know exactly what type of data I am getting, so I can guarantee that\
104+ \ unexpected data cannot sneak in and crash the Elm program."
105+ )
106+
107+ E.UnsupportedType name ->
108+ (
109+ "a `" ++ N.toString name ++ "` value"
110+ ,
111+ H.stack
112+ [ H.reflow $ "I cannot handle that. The types that CAN be in flags include:"
113+ , H.indent 4 $
114+ H.reflow $
115+ "Ints, Floats, Bools, Strings, Maybes, Lists, Arrays,\
116+ \ tuples, records, and JSON values."
117+ , H.reflow $
118+ "Since JSON values can flow through, you can use JSON encoders and decoders\
119+ \ to allow other types through as well. More advanced users often just do\
120+ \ everything with encoders and decoders for more control and better errors."
121+ ]
122+ )