on 42f3721 help sign in
123 lines 4.0 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.Doc as D
13import qualified Reporting.Error.Canonicalize as E
14import qualified Reporting.Region as R
15import qualified Reporting.Render.Code as Code
16import qualified Reporting.Render.Type as RT
17import qualified Reporting.Render.Type.Localizer as L
18import qualified Reporting.Report as Report
22-- ERROR
25data Error
26 = BadType R.Region Can.Type
27 | BadCycle R.Region [N.Name]
28 | BadFlags R.Region Can.Type E.InvalidPayload
32-- TO REPORT
35toReport :: L.Localizer -> Code.Source -> Error -> Report.Report
36toReport localizer source err =
37 case err of
38 BadType region tipe ->
39 Report.Report "BAD MAIN TYPE" region [] $
40 Report.toCodeSnippet source region Nothing
41 (
42 "I cannot handle this type of `main` value:"
43 ,
44 D.stack
45 [ "The type of `main` value I am seeing is:"
46 , D.indent 4 $ D.dullyellow $ RT.canToDoc localizer RT.None tipe
47 , D.reflow $
48 "I only know how to handle Html, Svg, and Programs\
49 \ though. Modify `main` to be one of those types of values!"
50 ]
51 )
53 BadCycle region cycleNames ->
54 Report.Report "BAD MAIN" region [] $
55 Report.toCodeSnippet source region Nothing
56 (
57 "A `main` definition cannot be defined in terms of itself."
58 ,
59 D.stack
60 [ D.reflow $
61 "It should be a boring value with no recursion. But\
62 \ instead it is involved in this cycle of definitions:"
63 , D.cycle 4 cycleNames
64 ]
65 )
67 BadFlags region _badType invalidPayload ->
68 let
69 formatDetails (aBadKindOfThing, butThatIsNoGood) =
70 Report.Report "BAD FLAGS" region [] $
71 Report.toCodeSnippet source region Nothing
72 (
73 D.reflow $
74 "Your `main` program wants " ++ aBadKindOfThing ++ " from JavaScript."
75 ,
76 butThatIsNoGood
77 )
78 in
79 formatDetails $
80 case invalidPayload of
81 E.ExtendedRecord ->
82 (
83 "an extended record"
84 ,
85 D.reflow $
86 "But the exact shape of the record must be known at compile time. No type variables!"
87 )
89 E.Function ->
90 (
91 "a function"
92 ,
93 D.reflow $
94 "But if I allowed functions from JS, it would be possible to sneak\
95 \ side-effects and runtime exceptions into Elm!"
96 )
98 E.TypeVariable name ->
99 (
100 "an unspecified type"
101 ,
102 D.reflow $
103 "But type variables like `" ++ N.toString name ++ "` cannot be given as flags.\
104 \ I need to know exactly what type of data I am getting, so I can guarantee that\
105 \ unexpected data cannot sneak in and crash the Elm program."
106 )
108 E.UnsupportedType name ->
109 (
110 "a `" ++ N.toString name ++ "` value"
111 ,
112 D.stack
113 [ D.reflow $ "I cannot handle that. The types that CAN be in flags include:"
114 , D.indent 4 $
115 D.reflow $
116 "Ints, Floats, Bools, Strings, Maybes, Lists, Arrays,\
117 \ tuples, records, and JSON values."
118 , D.reflow $
119 "Since JSON values can flow through, you can use JSON encoders and decoders\
120 \ to allow other types through as well. More advanced users often just do\
121 \ everything with encoders and decoders for more control and better errors."
122 ]
123 )