LibPQ.jl icon indicating copy to clipboard operation
LibPQ.jl copied to clipboard

Add boundserror when indexing into an invalid column

Open iamed2 opened this issue 5 years ago • 1 comments

Fix #211

@c42f Is this a good enough error or do I need something better here? I wanted to use BoundsError but it's quite limited for display.

iamed2 avatar Dec 09 '20 17:12 iamed2

I think throwing any exception is a great improvement already, so I'd be happy with this.

Having said that, I think this error would be a bit mysterious to users as they're dealing with the column name in using getproperty, but the error emitted here has no mention of the column name:

julia> row = execute(conn, "select * from (values (1, 2), (3, 4)) as test (a,b)") |> first
LibPQ.Row(PostgreSQL result, 1)

julia> row.a
1

julia> row.x
ERROR: BoundsError: attempt to access 2-element Array{String,1} at index [0]
Stacktrace:
 [1] column_number at /home/ubuntu/.julia/dev/LibPQ/src/results.jl:481 [inlined]
 [2] getindex(::LibPQ.Result, ::Int64, ::Int64) at /home/ubuntu/.julia/dev/LibPQ/src/results.jl:509
 [3] getproperty(::LibPQ.Row, ::Symbol) at /home/ubuntu/.julia/dev/LibPQ/src/tables.jl:37
 [4] top-level scope at REPL[35]:1

A nice extension could be to add some @boundscheck logic to getproperty as well. I'm not sure what error type to throw though. It seems that the error type used for Base when accessing a nonexistent field of a type is ErrorException... which is nice and descriptive but nonspecific in the type.

c42f avatar Dec 10 '20 02:12 c42f