Return an error tuple from Geo.JSON.decode/1 for any decode failure - #254
Open
youdie006 wants to merge 1 commit into
Open
Return an error tuple from Geo.JSON.decode/1 for any decode failure#254youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
decode/1 is the non-raising variant, but its rescue only catches
DecodeError, so anything else raised while decoding escapes:
Geo.JSON.decode(%{"type" => "Point", "coordinates" => "1,2"})
** (ArgumentError) expected a numeric coordinate, got: "1,2"
Geo.JSON.decode(%{"type" => "FeatureCollection", "features" => nil})
** (Protocol.UndefinedError)
ensure_numeric/1 raises ArgumentError, and Enum.map on a nil "features"
raises Protocol.UndefinedError. Neither is a DecodeError.
The sibling codecs already rescue everything - Geo.WKT.decode/1 and
Geo.WKB.encode/1 both use a bare rescue with an Exception.t() spec - so
this brings the GeoJSON decoder in line with them rather than inventing
a pattern.
The ArgumentError path arrived with ensure_numeric/1 in felt#218 and is
reachable from decode/1, but decode/1's rescue was not widened with it.
The tests split either side of the gap: the only decode/1 test uses an
unknown type, which is a DecodeError and is caught, while the
ArgumentError cases are covered only through decode!/1 under
assert_raise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
Geo.JSON.decode/1is the non-raising variant, but its rescue only catchesDecodeError, so anything else raised while decoding escapes:Across a few inputs, all through the public
Geo.JSON.decode/1:ensure_numeric/1(lib/geo/json/decoder.ex:302,:308) raisesArgumentError, andEnum.mapover a nil"features"(:84) raisesProtocol.UndefinedError. Neither is aDecodeError, so both escape a function whose spec says it returns{:error, _}.Why this is the code and not the spec
The two sibling codecs already rescue everything:
Geo.WKB.encode/1(lib/geo/wkb.ex:37-44) is the same shape. The GeoJSON decoder is the only one with a narrowed rescue, so this brings it in line rather than inventing a pattern.The
ArgumentErrorpath arrived withensure_numeric/1in #218, which made a new raise reachable fromdecode/1without widening its rescue.The change
Bare rescue in
decode/1, and the spec widened toException.t()to match WKT and WKB — inlib/geo/json/decoder.exand the delegating spec inlib/geo/json.ex.The alternative would be to keep
DecodeError.t()and wrap escapees in aDecodeError(it already carries a:valuefield for exactly that shape). I went with matching the siblings because it preserves the original exception, which is more useful when debugging, but I am happy to switch if you would rather the GeoJSON decoder keep the narrower return type.Why the tests miss it
The coverage splits either side of the gap and never crosses:
test/geo/json_test.exs:330is the only test driving non-bangdecode/1, and it uses{"type": "random_type"}— aDecodeError, which is caught.test/geo/json_test.exs:561-599covers theArgumentErrorpath, but only throughGeo.JSON.decode!/1underassert_raise.No test calls
decode/1with a non-numeric coordinate.Verification
Added a test asserting
{:error, _}for both escaping exception types, plus one pinning that aDecodeErrorstill comes back as aDecodeError.Reverting only the rescue clause and keeping the test fails with
** (ArgumentError) expected a numeric coordinate, got: "1,2".Full suite: 3 doctests, 16 properties, 170 tests, 0 failures before — 171 tests, 0 failures after.
Disclosure: this patch was prepared with AI assistance. The reproduction, the red/green check and the suite runs above were executed against this branch; happy to adjust anything on request.