@@ -0,0 +1,122 @@
1+{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+module Reporting.Error.Main
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
25+ = BadType R.Region Can.Type
26+ | BadCycle R.Region [N.Name]
27+ | BadFlags R.Region Can.Type E.InvalidPayload
34+toReport :: Code.Source -> RenderType.Localizer -> Error -> Report.Report
35+toReport source localizer err =
37+ BadType region tipe ->
38+ Report.Report "BAD MAIN TYPE" region [] $
39+ Report.toCodeSnippet source region Nothing
41+ "I cannot handle this type of `main` value:"
44+ [ "The type of `main` value I am seeing is:"
45+ , H.indent 4 (RenderType.toDoc localizer tipe)
47+ "I only know how to handle Html, Svg, and Programs\
48+ \ though. Modify `main` to be one of those types of values!"
52+ BadCycle region cycleNames ->
53+ Report.Report "BAD MAIN" region [] $
54+ Report.toCodeSnippet source region Nothing
56+ "A `main` definition cannot be defined in terms of itself."
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)
66+ BadFlags region _badType invalidPayload ->
68+ formatDetails (aBadKindOfThing, butThatIsNoGood) =
69+ Report.Report "BAD FLAGS" region [] $
70+ Report.toCodeSnippet source region Nothing
73+ "Your `main` program wants " ++ aBadKindOfThing ++ " from JavaScript."
79+ case invalidPayload of
85+ "But the exact shape of the record must be known at compile time. No type variables!"
93+ "But if I allowed functions from JS, it would be possible to sneak\
94+ \ side-effects and runtime exceptions into Elm!"
97+ E.TypeVariable name ->
99+ "an unspecified type"
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."
107+ E.UnsupportedType name ->
109+ "a `" ++ N.toString name ++ "` value"
112+ [ H.reflow $ "I cannot handle that. The types that CAN be in flags include:"
115+ "Ints, Floats, Bools, Strings, Maybes, Lists, Arrays,\
116+ \ tuples, records, and JSON values."
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."