on master help sign in
add reason in for all JSON errors

So that some context can be given about why you are seeing a JSON error in the first place. "What does my elm.json have to do with diffing?"

commit91a0efacd6ceaacb15f408c31ced59f68de39ec5 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 14 Jun 2019, 11:21 (7 years ago) parentd7153e7
1 file changed+42−33
compiler/src/Reporting/Error/Json.hs+42−33
compiler/src/Reporting/Error/Json.hs+42−33
@@ -4,6 +4,7 @@ module Reporting.Error.Json
44 ( toReport
55 , FailureToReport(..)
66 , Context(..)
7+ , Reason(..)
78 )
89 where
910
@@ -23,33 +24,45 @@ import qualified Reporting.Render.Code as Code
2324-- TO REPORT
2425
2526
26−toReport :: FilePath -> FailureToReport x -> Error x -> Help.Report
27−toReport path ftr err =
27+toReport :: FilePath -> FailureToReport x -> Error x -> Reason -> Help.Report
28+toReport path ftr err reason =
2829 case err of
2930 DecodeProblem bytes problem ->
30− problemToReport path ftr (Code.toSource bytes) CRoot problem
31+ problemToReport path ftr (Code.toSource bytes) CRoot problem reason
3132
3233 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
3444
3545
3646
3747-- PARSE ERROR TO REPORT
3848
3949
40−parseErrorToReport :: FilePath -> Code.Source -> ParseError -> Help.Report
41−parseErrorToReport path source parseError =
50+parseErrorToReport :: FilePath -> Code.Source -> ParseError -> Reason -> Help.Report
51+parseErrorToReport path source parseError reason =
4252 let
43− toSnippet title row col pair =
53+ toSnippet title row col (problem, details) =
4454 let pos = A.Position row col in
4555 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+ )
4760 in
4861 case parseError of
4962 Start row col ->
5063 toSnippet "EXPECTING A VALUE" row col
5164 (
52− D.reflow $ "I was expecting to see a JSON value next:"
65+ "I was expecting to see a JSON value next:"
5366 ,
5467 D.stack
5568 [ D.fillSep
@@ -66,7 +79,7 @@ parseErrorToReport path source parseError =
6679 ObjectField row col ->
6780 toSnippet "UNFINISHED OBJECT" row col
6881 (
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:"
7083 ,
7184 D.stack
7285 [ D.reflow $ "I was expecting to see a field name next."
@@ -77,7 +90,7 @@ parseErrorToReport path source parseError =
7790 ObjectColon row col ->
7891 toSnippet "EXPECTING COLON" row col
7992 (
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:"
8194 ,
8295 D.stack
8396 [ D.reflow $ "I was expecting to see a colon next."
@@ -88,7 +101,7 @@ parseErrorToReport path source parseError =
88101 ObjectEnd row col ->
89102 toSnippet "UNFINISHED OBJECT" row col
90103 (
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:"
92105 ,
93106 D.stack
94107 [ D.reflow $
@@ -103,7 +116,7 @@ parseErrorToReport path source parseError =
103116 ArrayEnd row col ->
104117 toSnippet "UNFINISHED ARRAY" row col
105118 (
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:"
107120 ,
108121 D.stack
109122 [ D.reflow $ "I was expecting to see a comma or a closing square bracket next."
@@ -117,8 +130,7 @@ parseErrorToReport path source parseError =
117130 BadStringEnd ->
118131 toSnippet "ENDLESS STRING" row col
119132 (
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:"
122134 ,
123135 D.fillSep $
124136 ["Strings","look","like",D.green "\"this\"","with","double"
@@ -130,8 +142,7 @@ parseErrorToReport path source parseError =
130142 BadStringControlChar ->
131143 toSnippet "UNEXPECTED CONTROL CHARACTER" row col
132144 (
133− D.reflow $
134− "I ran into a control character unexpectedly:"
145+ "I ran into a control character unexpectedly:"
135146 ,
136147 D.reflow $
137148 "These are characters that represent tabs, backspaces, newlines, and\
@@ -143,8 +154,7 @@ parseErrorToReport path source parseError =
143154 BadStringEscapeChar ->
144155 toSnippet "UNKNOWN ESCAPE" row col
145156 (
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:"
148158 ,
149159 D.stack
150160 [ D.reflow $
@@ -159,8 +169,7 @@ parseErrorToReport path source parseError =
159169 BadStringEscapeHex ->
160170 toSnippet "BAD HEX ESCAPE" row col
161171 (
162− D.reflow $
163− "This is not a valid hex escape:"
172+ "This is not a valid hex escape:"
164173 ,
165174 D.fillSep $
166175 ["Valid","hex","escapes","in","JSON","are","between"
@@ -172,7 +181,7 @@ parseErrorToReport path source parseError =
172181 NoLeadingZeros row col ->
173182 toSnippet "BAD NUMBER" row col
174183 (
175− D.reflow $ "Numbers cannot start with zeros like this:"
184+ "Numbers cannot start with zeros like this:"
176185 ,
177186 D.reflow $ "Try deleting the leading zeros?"
178187 )
@@ -180,7 +189,7 @@ parseErrorToReport path source parseError =
180189 NoFloats row col ->
181190 toSnippet "UNEXPECTED NUMBER" row col
182191 (
183− D.reflow $ "I got stuck while trying to parse this number:"
192+ "I got stuck while trying to parse this number:"
184193 ,
185194 D.reflow $
186195 "I do not accept floating point numbers like 3.1415 right now. That kind\
@@ -190,7 +199,7 @@ parseErrorToReport path source parseError =
190199 BadEnd row col ->
191200 toSnippet "JSON PROBLEM" row col
192201 (
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:"
194203 ,
195204 D.reflow $
196205 "I am not really sure what is wrong. This sometimes means there is extra\
@@ -224,14 +233,14 @@ data Context
224233 | CIndex Int Context
225234
226235
227−problemToReport :: FilePath -> FailureToReport x -> Code.Source -> Context -> Problem x -> Help.Report
228−problemToReport path ftr source context problem =
236+problemToReport :: FilePath -> FailureToReport x -> Code.Source -> Context -> Problem x -> Reason -> Help.Report
237+problemToReport path ftr source context problem reason =
229238 case problem of
230239 Field field prob ->
231− problemToReport path ftr source (CField field context) prob
240+ problemToReport path ftr source (CField field context) prob reason
232241
233242 Index index prob ->
234− problemToReport path ftr source (CIndex index context) prob
243+ problemToReport path ftr source (CIndex index context) prob reason
235244
236245 OneOf p ps ->
237246 -- NOTE: only displays the deepest problem. This works well for the kind
@@ -239,13 +248,13 @@ problemToReport path ftr source context problem =
239248 let
240249 (NE.List prob _) = NE.sortBy (negate . getMaxDepth) (NE.List p ps)
241250 in
242− problemToReport path ftr source context prob
251+ problemToReport path ftr source context prob reason
243252
244253 Failure region x ->
245254 _failureToReport ftr path source context region x
246255
247256 Expecting region expectation ->
248− expectationToReport path source context region expectation
257+ expectationToReport path source context region expectation reason
249258
250259
251260getMaxDepth :: Problem x -> Int
@@ -262,8 +271,8 @@ newtype FailureToReport x =
262271 FailureToReport { _failureToReport :: FilePath -> Code.Source -> Context -> A.Region -> x -> Help.Report }
263272
264273
265−expectationToReport :: FilePath -> Code.Source -> Context -> A.Region -> DecodeExpectation -> Help.Report
266−expectationToReport path source context (A.Region start end) expectation =
274+expectationToReport :: FilePath -> Code.Source -> Context -> A.Region -> DecodeExpectation -> Reason -> Help.Report
275+expectationToReport path source context (A.Region start end) expectation reason =
267276 let
268277 (A.Position sr _) = start
269278 (A.Position er _) = end
@@ -289,7 +298,7 @@ expectationToReport path source context (A.Region start end) expectation =
289298 toSnippet title aThing =
290299 Help.jsonReport title (Just path) $
291300 Code.toSnippet source region Nothing
292− ( D.reflow introduction
301+ ( D.reflow (because reason introduction)
293302 , D.fillSep $ ["I","was","expecting","to","run","into"] ++ aThing
294303 )
295304 in