Skip to content

Better error upon non-existent file #122

@KronosTheLate

Description

@KronosTheLate

The current error for an invalid path is not great:

julia> using GeoJSON

julia> GeoJSON.read("not/a/path.geojson.json")
┌ Warning: Failed to parse GeoJSON as 2D, trying 3D. Set `ndim` to 3 to avoid this warning.
└ @ GeoJSON C:\Users\densb\.julia\packages\GeoJSON\73eSw\src\io.jl:21
ERROR: ArgumentError: invalid JSON at byte position 1 while parsing type GeoJSON.GeoJSONT{3, Float32}: ExpectedOpeningObjectChar
not/a/path.geojson.json

Stacktrace:
  [1] invalid(error::JSON3.Error, buf::Base.CodeUnits{UInt8, String}, pos::Int64, T::Type)
    @ JSON3 C:\Users\densb\.julia\packages\JSON3\rT1w2\src\JSON3.jl:30
  [2] #_read#57
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:836 [inlined]
  [3] _read
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:755 [inlined]
  [4] #read#56
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:752 [inlined]
  [5] read
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:746 [inlined]
  [6] #read#47
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:441 [inlined]
  [7] read
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:439 [inlined]
  [8] parse(str::String, ::Type{GeoJSON.GeoJSONWrapper{…} where X<:GeoJSON.GeoJSONT{…}}; jsonlines::Bool, kw::@Kwargs{})
    @ JSON3 C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:40
  [9] parse
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:32 [inlined]
 [10] #read#18
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:46 [inlined]
 [11] read(str::String, ::Type{GeoJSON.GeoJSONWrapper{3, Float32, X} where X<:GeoJSON.GeoJSONT{3, Float32}})
    @ JSON3 C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:45
 [12] read(io::String; lazyfc::Bool, ndim::Int64, numbertype::Type)
    @ GeoJSON C:\Users\densb\.julia\packages\GeoJSON\73eSw\src\io.jl:22
 [13] read(io::String)
    @ GeoJSON C:\Users\densb\.julia\packages\GeoJSON\73eSw\src\io.jl:13
 [14] top-level scope
    @ REPL[7]:1

caused by: ArgumentError: invalid JSON at byte position 1 while parsing type GeoJSON.GeoJSONT{2, Float32}: ExpectedOpeningObjectChar
not/a/path.geojson.json

Stacktrace:
  [1] invalid(error::JSON3.Error, buf::Base.CodeUnits{UInt8, String}, pos::Int64, T::Type)
    @ JSON3 C:\Users\densb\.julia\packages\JSON3\rT1w2\src\JSON3.jl:30
  [2] #_read#57
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:836 [inlined]
  [3] _read
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:755 [inlined]
  [4] #read#56
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:752 [inlined]
  [5] read
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:746 [inlined]
  [6] #read#47
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:441 [inlined]
  [7] read
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:439 [inlined]
  [8] parse(str::String, ::Type{GeoJSON.GeoJSONWrapper{…} where X<:GeoJSON.GeoJSONT{…}}; jsonlines::Bool, kw::@Kwargs{})
    @ JSON3 C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:40
  [9] parse
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:32 [inlined]
 [10] #read#18
    @ C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:46 [inlined]
 [11] read(str::String, ::Type{GeoJSON.GeoJSONWrapper{2, Float32, X} where X<:GeoJSON.GeoJSONT{2, Float32}})
    @ JSON3 C:\Users\densb\.julia\packages\JSON3\rT1w2\src\structs.jl:45
 [12] read(io::String; lazyfc::Bool, ndim::Int64, numbertype::Type)
    @ GeoJSON C:\Users\densb\.julia\packages\GeoJSON\73eSw\src\io.jl:18
 [13] read(io::String)
    @ GeoJSON C:\Users\densb\.julia\packages\GeoJSON\73eSw\src\io.jl:13
 [14] top-level scope
    @ REPL[7]:1
Some type information was truncated. Use `show(err)` to see complete types.

Now because GeoJSON.read accepts a string (I assume a json-string), then I guess the parser attempts to parse the incorrect path as a string, which is reasonable. It is just not very user-friendly.

So how about this: If there is no file at the given string (by isfile(path)) AND the parsing fails, state that the given string is not a valid path, nor parsable as the contents of a json file.

It is quite possible that this issue should be upstreamed to JSON3.jl (or JSON.jl I suppose).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions