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