on master help sign in
add reason in for all JSON errors
91a0efaEvan Czaplicki
compiler/src/Reporting/Error/Json.hs+42−33
@@ -4,6 +4,7 @@ module Reporting.Error.Json
4 ( toReport
4 ( toReport
5 , FailureToReport(..)
5 , FailureToReport(..)
6 , Context(..)
6 , Context(..)
7+ , Reason(..)
7 )
8 )
8 where
9 where
9
10
@@ -23,33 +24,45 @@ import qualified Reporting.Render.Code as Code
23-- TO REPORT
24-- TO REPORT
24
25
25
26
26−toReport :: FilePath -> FailureToReport x -> Error x -> Help.Report
27+toReport :: FilePath -> FailureToReport x -> Error x -> Reason -> Help.Report
27−toReport path ftr err =
28+toReport path ftr err reason =
28 case err of
29 case err of
29 DecodeProblem bytes problem ->
30 DecodeProblem bytes problem ->
30− problemToReport path ftr (Code.toSource bytes) CRoot problem
31+ problemToReport path ftr (Code.toSource bytes) CRoot problem reason
31
32
32 ParseProblem bytes parseError ->
33 ParseProblem bytes parseError ->
33− parseErrorToReport path (Code.toSource bytes) parseError
34+ parseErrorToReport path (Code.toSource bytes) parseError reason
35+
36+
37+newtype Reason =
38+ ExplicitReason String
39+
40+
41+because :: Reason -> String -> String
42+because (ExplicitReason iNeedThings) problem =
43+ iNeedThings ++ " " ++ problem
34
44
35
45
36
46
37-- PARSE ERROR TO REPORT
47-- PARSE ERROR TO REPORT
38
48
39
49
40−parseErrorToReport :: FilePath -> Code.Source -> ParseError -> Help.Report
50+parseErrorToReport :: FilePath -> Code.Source -> ParseError -> Reason -> Help.Report
41−parseErrorToReport path source parseError =
51+parseErrorToReport path source parseError reason =
42 let
52 let
43− toSnippet title row col pair =
53+ toSnippet title row col (problem, details) =
44 let pos = A.Position row col in
54 let pos = A.Position row col in
45 Help.jsonReport title (Just path) $
55 Help.jsonReport title (Just path) $
46− Code.toSnippet source (A.Region pos pos) Nothing pair
56+ Code.toSnippet source (A.Region pos pos) Nothing
57+ ( D.reflow (because reason problem)
58+ , details
59+ )
47 in
60 in
48 case parseError of
61 case parseError of
49 Start row col ->
62 Start row col ->
50 toSnippet "EXPECTING A VALUE" row col
63 toSnippet "EXPECTING A VALUE" row col
51 (
64 (
52− D.reflow $ "I was expecting to see a JSON value next:"
65+ "I was expecting to see a JSON value next:"
53 ,
66 ,
54 D.stack
67 D.stack
55 [ D.fillSep
68 [ D.fillSep
@@ -66,7 +79,7 @@ parseErrorToReport path source parseError =
66 ObjectField row col ->
79 ObjectField row col ->
67 toSnippet "UNFINISHED OBJECT" row col
80 toSnippet "UNFINISHED OBJECT" row col
68 (
81 (
69− D.reflow $ "I was partway through parsing a JSON object when I got stuck here:"
82+ "I was partway through parsing a JSON object when I got stuck here:"
70 ,
83 ,
71 D.stack
84 D.stack
72 [ D.reflow $ "I was expecting to see a field name next."
85 [ D.reflow $ "I was expecting to see a field name next."
@@ -77,7 +90,7 @@ parseErrorToReport path source parseError =
77 ObjectColon row col ->
90 ObjectColon row col ->
78 toSnippet "EXPECTING COLON" row col
91 toSnippet "EXPECTING COLON" row col
79 (
92 (
80− D.reflow $ "I was partway through parsing a JSON object when I got stuck here:"
93+ "I was partway through parsing a JSON object when I got stuck here:"
81 ,
94 ,
82 D.stack
95 D.stack
83 [ D.reflow $ "I was expecting to see a colon next."
96 [ D.reflow $ "I was expecting to see a colon next."
@@ -88,7 +101,7 @@ parseErrorToReport path source parseError =
88 ObjectEnd row col ->
101 ObjectEnd row col ->
89 toSnippet "UNFINISHED OBJECT" row col
102 toSnippet "UNFINISHED OBJECT" row col
90 (
103 (
91− D.reflow $ "I was partway through parsing a JSON object when I got stuck here:"
104+ "I was partway through parsing a JSON object when I got stuck here:"
92 ,
105 ,
93 D.stack
106 D.stack
94 [ D.reflow $
107 [ D.reflow $
@@ -103,7 +116,7 @@ parseErrorToReport path source parseError =
103 ArrayEnd row col ->
116 ArrayEnd row col ->
104 toSnippet "UNFINISHED ARRAY" row col
117 toSnippet "UNFINISHED ARRAY" row col
105 (
118 (
106− D.reflow $ "I was partway through parsing a JSON array when I got stuck here:"
119+ "I was partway through parsing a JSON array when I got stuck here:"
107 ,
120 ,
108 D.stack
121 D.stack
109 [ D.reflow $ "I was expecting to see a comma or a closing square bracket next."
122 [ D.reflow $ "I was expecting to see a comma or a closing square bracket next."
@@ -117,8 +130,7 @@ parseErrorToReport path source parseError =
117 BadStringEnd ->
130 BadStringEnd ->
118 toSnippet "ENDLESS STRING" row col
131 toSnippet "ENDLESS STRING" row col
119 (
132 (
120− D.reflow $
121− "I got to the end of the line without seeing the closing double quote:"
133+ "I got to the end of the line without seeing the closing double quote:"
122 ,
134 ,
123 D.fillSep $
135 D.fillSep $
124 ["Strings","look","like",D.green "\"this\"","with","double"
136 ["Strings","look","like",D.green "\"this\"","with","double"
@@ -130,8 +142,7 @@ parseErrorToReport path source parseError =
130 BadStringControlChar ->
142 BadStringControlChar ->
131 toSnippet "UNEXPECTED CONTROL CHARACTER" row col
143 toSnippet "UNEXPECTED CONTROL CHARACTER" row col
132 (
144 (
133− D.reflow $
134− "I ran into a control character unexpectedly:"
145+ "I ran into a control character unexpectedly:"
135 ,
146 ,
136 D.reflow $
147 D.reflow $
137 "These are characters that represent tabs, backspaces, newlines, and\
148 "These are characters that represent tabs, backspaces, newlines, and\
@@ -143,8 +154,7 @@ parseErrorToReport path source parseError =
143 BadStringEscapeChar ->
154 BadStringEscapeChar ->
144 toSnippet "UNKNOWN ESCAPE" row col
155 toSnippet "UNKNOWN ESCAPE" row col
145 (
156 (
146− D.reflow $
147− "Backslashes always start escaped characters, but I do not recognize this one:"
157+ "Backslashes always start escaped characters, but I do not recognize this one:"
148 ,
158 ,
149 D.stack
159 D.stack
150 [ D.reflow $
160 [ D.reflow $
@@ -159,8 +169,7 @@ parseErrorToReport path source parseError =
159 BadStringEscapeHex ->
169 BadStringEscapeHex ->
160 toSnippet "BAD HEX ESCAPE" row col
170 toSnippet "BAD HEX ESCAPE" row col
161 (
171 (
162− D.reflow $
163− "This is not a valid hex escape:"
172+ "This is not a valid hex escape:"
164 ,
173 ,
165 D.fillSep $
174 D.fillSep $
166 ["Valid","hex","escapes","in","JSON","are","between"
175 ["Valid","hex","escapes","in","JSON","are","between"
@@ -172,7 +181,7 @@ parseErrorToReport path source parseError =
172 NoLeadingZeros row col ->
181 NoLeadingZeros row col ->
173 toSnippet "BAD NUMBER" row col
182 toSnippet "BAD NUMBER" row col
174 (
183 (
175− D.reflow $ "Numbers cannot start with zeros like this:"
184+ "Numbers cannot start with zeros like this:"
176 ,
185 ,
177 D.reflow $ "Try deleting the leading zeros?"
186 D.reflow $ "Try deleting the leading zeros?"
178 )
187 )
@@ -180,7 +189,7 @@ parseErrorToReport path source parseError =
180 NoFloats row col ->
189 NoFloats row col ->
181 toSnippet "UNEXPECTED NUMBER" row col
190 toSnippet "UNEXPECTED NUMBER" row col
182 (
191 (
183− D.reflow $ "I got stuck while trying to parse this number:"
192+ "I got stuck while trying to parse this number:"
184 ,
193 ,
185 D.reflow $
194 D.reflow $
186 "I do not accept floating point numbers like 3.1415 right now. That kind\
195 "I do not accept floating point numbers like 3.1415 right now. That kind\
@@ -190,7 +199,7 @@ parseErrorToReport path source parseError =
190 BadEnd row col ->
199 BadEnd row col ->
191 toSnippet "JSON PROBLEM" row col
200 toSnippet "JSON PROBLEM" row col
192 (
201 (
193− D.reflow $ "I was partway through parsing some JSON when I got stuck here:"
202+ "I was partway through parsing some JSON when I got stuck here:"
194 ,
203 ,
195 D.reflow $
204 D.reflow $
196 "I am not really sure what is wrong. This sometimes means there is extra\
205 "I am not really sure what is wrong. This sometimes means there is extra\
@@ -224,14 +233,14 @@ data Context
224 | CIndex Int Context
233 | CIndex Int Context
225
234
226
235
227−problemToReport :: FilePath -> FailureToReport x -> Code.Source -> Context -> Problem x -> Help.Report
236+problemToReport :: FilePath -> FailureToReport x -> Code.Source -> Context -> Problem x -> Reason -> Help.Report
228−problemToReport path ftr source context problem =
237+problemToReport path ftr source context problem reason =
229 case problem of
238 case problem of
230 Field field prob ->
239 Field field prob ->
231− problemToReport path ftr source (CField field context) prob
240+ problemToReport path ftr source (CField field context) prob reason
232
241
233 Index index prob ->
242 Index index prob ->
234− problemToReport path ftr source (CIndex index context) prob
243+ problemToReport path ftr source (CIndex index context) prob reason
235
244
236 OneOf p ps ->
245 OneOf p ps ->
237 -- NOTE: only displays the deepest problem. This works well for the kind
246 -- NOTE: only displays the deepest problem. This works well for the kind
@@ -239,13 +248,13 @@ problemToReport path ftr source context problem =
239 let
248 let
240 (NE.List prob _) = NE.sortBy (negate . getMaxDepth) (NE.List p ps)
249 (NE.List prob _) = NE.sortBy (negate . getMaxDepth) (NE.List p ps)
241 in
250 in
242− problemToReport path ftr source context prob
251+ problemToReport path ftr source context prob reason
243
252
244 Failure region x ->
253 Failure region x ->
245 _failureToReport ftr path source context region x
254 _failureToReport ftr path source context region x
246
255
247 Expecting region expectation ->
256 Expecting region expectation ->
248− expectationToReport path source context region expectation
257+ expectationToReport path source context region expectation reason
249
258
250
259
251getMaxDepth :: Problem x -> Int
260getMaxDepth :: Problem x -> Int
@@ -262,8 +271,8 @@ newtype FailureToReport x =
262 FailureToReport { _failureToReport :: FilePath -> Code.Source -> Context -> A.Region -> x -> Help.Report }
271 FailureToReport { _failureToReport :: FilePath -> Code.Source -> Context -> A.Region -> x -> Help.Report }
263
272
264
273
265−expectationToReport :: FilePath -> Code.Source -> Context -> A.Region -> DecodeExpectation -> Help.Report
274+expectationToReport :: FilePath -> Code.Source -> Context -> A.Region -> DecodeExpectation -> Reason -> Help.Report
266−expectationToReport path source context (A.Region start end) expectation =
275+expectationToReport path source context (A.Region start end) expectation reason =
267 let
276 let
268 (A.Position sr _) = start
277 (A.Position sr _) = start
269 (A.Position er _) = end
278 (A.Position er _) = end
@@ -289,7 +298,7 @@ expectationToReport path source context (A.Region start end) expectation =
289 toSnippet title aThing =
298 toSnippet title aThing =
290 Help.jsonReport title (Just path) $
299 Help.jsonReport title (Just path) $
291 Code.toSnippet source region Nothing
300 Code.toSnippet source region Nothing
292− ( D.reflow introduction
301+ ( D.reflow (because reason introduction)
293 , D.fillSep $ ["I","was","expecting","to","run","into"] ++ aThing
302 , D.fillSep $ ["I","was","expecting","to","run","into"] ++ aThing
294 )
303 )
295 in
304 in