on ca2be8a help sign in
122 lines 3.9 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Main
4 ( Error(..)
5 , toReport
6 )
7 where
10import qualified AST.Canonical as Can
11import qualified Elm.Name as N
12import qualified Reporting.Error.Canonicalize as E
13import qualified Reporting.Helpers as H
14import qualified Reporting.Region as R
15import qualified Reporting.Render.Code as Code
16import qualified Reporting.Render.Type as RT
17import qualified Reporting.Report as Report
21-- ERROR
24data Error
25 = BadType R.Region Can.Type
26 | BadCycle R.Region [N.Name]
27 | BadFlags R.Region Can.Type E.InvalidPayload
31-- TO REPORT
34toReport :: Code.Source -> Error -> Report.Report
35toReport source 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 $ H.dullyellow $ RT.canToDoc RT.None 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 )
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 )
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 )
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 )
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 )
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 )