on master help sign in
Redo decoder failure messages to look nicer with new error reporting
23f06c9Evan Czaplicki
src/Elm/Compiler/Module.hs+1−1
@@ -121,7 +121,7 @@ decoder =
121 do txt <- Decode.text
121 do txt <- Decode.text
122 case nameFromText txt of
122 case nameFromText txt of
123 Nothing ->
123 Nothing ->
124− Decode.fail "a module name like \"Html.Events\""
124+ Decode.fail "Expecting a module name like \"Html.Events\""
125
125
126 Just name ->
126 Just name ->
127 Decode.succeed name
127 Decode.succeed name
src/Elm/Compiler/Type.hs+1−1
@@ -180,7 +180,7 @@ decoder =
180 do txt <- Decode.text
180 do txt <- Decode.text
181 case Parse.run Type.expression (Text.replace "'" "_" txt) of
181 case Parse.run Type.expression (Text.replace "'" "_" txt) of
182 Left _ ->
182 Left _ ->
183− Decode.fail "a type, like (String -> Int)"
183+ Decode.fail "Expecting a type, like (String -> Int)"
184
184
185 Right (tipe, _, _) ->
185 Right (tipe, _, _) ->
186 Decode.succeed (fromRawType tipe)
186 Decode.succeed (fromRawType tipe)
src/Elm/Docs.hs+1−1
@@ -204,4 +204,4 @@ assocDecoder =
204 "left" -> Decode.succeed Decl.Left
204 "left" -> Decode.succeed Decl.Left
205 "non" -> Decode.succeed Decl.Non
205 "non" -> Decode.succeed Decl.Non
206 "right" -> Decode.succeed Decl.Right
206 "right" -> Decode.succeed Decl.Right
207− _ -> Decode.fail "an associativity"
207+ _ -> Decode.fail "Expecting an ASSOCIATIVITY (e.g. left, non, right)"
src/Elm/Package.hs+4−4
@@ -286,8 +286,8 @@ decoder :: Decode.Decoder Name
286decoder =
286decoder =
287 do txt <- Decode.text
287 do txt <- Decode.text
288 case fromText txt of
288 case fromText txt of
289− Left _ ->
289+ Left msg ->
290− Decode.fail "a valid project name, like \"elm-lang/core\""
290+ Decode.fail $ "Expecting a PACKAGE name. " ++ msg
291
291
292 Right name ->
292 Right name ->
293 Decode.succeed name
293 Decode.succeed name
@@ -306,8 +306,8 @@ versionDecoder =
306 Right version ->
306 Right version ->
307 Decode.succeed version
307 Decode.succeed version
308
308
309− Left _ ->
309+ Left msg ->
310− Decode.fail "a version number, like 1.0.0"
310+ Decode.fail $ "Expecting a VERSION. " ++ msg
311
311
312
312
313encodeVersion :: Version -> Encode.Value
313encodeVersion :: Version -> Encode.Value
src/Json/Decode.hs+15−13
@@ -80,7 +80,7 @@ text =
80 Right txt
80 Right txt
81
81
82 _ ->
82 _ ->
83− Left (mkError (Failure value "a string"))
83+ Left (mkError (Failure value "Expecting a STRING"))
84
84
85
85
86bool :: Decoder Bool
86bool :: Decoder Bool
@@ -91,7 +91,7 @@ bool =
91 Right boolean
91 Right boolean
92
92
93 _ ->
93 _ ->
94− Left (mkError (Failure value "a boolean"))
94+ Left (mkError (Failure value "Expecting a BOOL"))
95
95
96
96
97int :: Decoder Int
97int :: Decoder Int
@@ -101,13 +101,13 @@ int =
101 Aeson.Number number ->
101 Aeson.Number number ->
102 case Scientific.toBoundedInteger number of
102 case Scientific.toBoundedInteger number of
103 Nothing ->
103 Nothing ->
104− Left (mkError (Failure value "an integer"))
104+ Left (mkError (Failure value "Expecting an INT"))
105
105
106 Just integer ->
106 Just integer ->
107 Right integer
107 Right integer
108
108
109 _ ->
109 _ ->
110− Left (mkError (Failure value "an integer"))
110+ Left (mkError (Failure value "Expecting an INT"))
111
111
112
112
113float :: Decoder Float
113float :: Decoder Float
@@ -118,7 +118,7 @@ float =
118 Right (Scientific.toRealFloat number)
118 Right (Scientific.toRealFloat number)
119
119
120 _ ->
120 _ ->
121− Left (mkError (Failure value "a float"))
121+ Left (mkError (Failure value "Expecting a FLOAT"))
122
122
123
123
124
124
@@ -134,7 +134,7 @@ list (Decoder run) =
134 Vector.imapM (\i v -> run (mkError . Index i) v) vector
134 Vector.imapM (\i v -> run (mkError . Index i) v) vector
135
135
136 _ ->
136 _ ->
137− Left (mkError (Failure value "an array"))
137+ Left (mkError (Failure value "Expecting an ARRAY"))
138
138
139
139
140dict :: Decoder a -> Decoder (HashMap.HashMap Text a)
140dict :: Decoder a -> Decoder (HashMap.HashMap Text a)
@@ -145,7 +145,7 @@ dict (Decoder run) =
145 HashMap.traverseWithKey (\k v -> run (mkError . Field k) v) hashMap
145 HashMap.traverseWithKey (\k v -> run (mkError . Field k) v) hashMap
146
146
147 _ ->
147 _ ->
148− Left (mkError (Failure value "an object"))
148+ Left (mkError (Failure value "Expecting an OBJECT"))
149
149
150
150
151maybe :: Decoder a -> Decoder (Maybe a)
151maybe :: Decoder a -> Decoder (Maybe a)
@@ -173,11 +173,11 @@ field name (Decoder run) =
173 run (mkError . Field name) v
173 run (mkError . Field name) v
174
174
175 Nothing ->
175 Nothing ->
176− Left $ mkError $
176+ Left $ mkError $ Failure value $
177− Failure value ("a \"" ++ Text.unpack name ++ "\" field")
177+ "Expecting a \"" ++ Text.unpack name ++ "\" field."
178
178
179 _ ->
179 _ ->
180− Left (mkError (Failure value "an object"))
180+ Left (mkError (Failure value "Expecting an OBJECT"))
181
181
182
182
183at :: [Text] -> Decoder a -> Decoder a
183at :: [Text] -> Decoder a -> Decoder a
@@ -199,11 +199,13 @@ index i (Decoder run) =
199 run (mkError . Index i) v
199 run (mkError . Index i) v
200
200
201 Nothing ->
201 Nothing ->
202− Left $ mkError $
202+ Left $ mkError $ Failure value $
203− Failure value ("index " ++ show i ++ " of an array")
203+ "Expecting a longer ARRAY. I need index " ++ show i
204+ ++ ", but the array only has " ++ show (Vector.length vector)
205+ ++ " elements."
204
206
205 _ ->
207 _ ->
206− Left (mkError (Failure value "an array"))
208+ Left (mkError (Failure value "Expecting an ARRAY"))
207
209
208
210
209
211