JuliaDatabases / LibPQ.jl

A Julia wrapper for libpq
MIT License
217 stars 51 forks source link

`execute` and `async_execute` exception handling difference #279

Open krynju opened 1 year ago

krynju commented 1 year ago

So when using async_execute you basically need to unwrap TaskFailedException, which contains a CompositeException, which contains one (or many?) exceptions. With execute it's much simpler

Should we make it more similiar?

julia> LibPQ.execute(conn, "select from x")
[error | LibPQ]: UndefinedTable: ERROR:  relation "x" does not exist
LINE 1: select from x
                    ^
ERROR: UndefinedTable: ERROR:  relation "x" does not exist
LINE 1: select from x
                    ^
Stacktrace:
 [1] error(logger::Memento.Logger, exc::LibPQ.Errors.PQResultError{LibPQ.Errors.C42, LibPQ.Errors.E42P01})
   @ Memento ~/.julia/packages/Memento/xnHxE/src/loggers.jl:463
 [2] handle_result(jl_result::LibPQ.Result{false}; throw_error::Bool)
   @ LibPQ ~/.julia/packages/LibPQ/1oHrl/src/results.jl:238
 [3] _multi_execute(jl_conn::LibPQ.Connection, query::String; throw_error::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ LibPQ ~/.julia/packages/LibPQ/1oHrl/src/results.jl:305
 [4] _multi_execute
   @ ~/.julia/packages/LibPQ/1oHrl/src/results.jl:301 [inlined]
 [5] #execute#51
   @ ~/.julia/packages/LibPQ/1oHrl/src/results.jl:294 [inlined]
 [6] execute(conn::LibPQ.Connection, query::String)
   @ LibPQ ~/.julia/packages/LibPQ/1oHrl/src/results.jl:291
 [7] top-level scope
   @ REPL[4]:1

julia> LibPQ.async_execute(conn, "select from x") |> fetch
[error | LibPQ]: UndefinedTable: ERROR:  relation "x" does not exist
LINE 1: select from x
                    ^
ERROR: TaskFailedException
Stacktrace:
 [1] wait
   @ ./task.jl:322 [inlined]
 [2] fetch
   @ ./task.jl:337 [inlined]
 [3] fetch
   @ ~/.julia/packages/LibPQ/1oHrl/src/asyncresults.jl:183 [inlined]
 [4] |>(x::LibPQ.AsyncResult{false}, f::typeof(fetch))
   @ Base ./operators.jl:858
 [5] top-level scope
   @ REPL[5]:1

    nested task error: UndefinedTable: ERROR:  relation "x" does not exist
    LINE 1: select from x
                        ^
    Stacktrace:
     [1] handle_result(async_result::LibPQ.AsyncResult{false}; throw_error::Bool)
       @ LibPQ ~/.julia/packages/LibPQ/1oHrl/src/asyncresults.jl:74
     [2] (::LibPQ.var"#106#108"{Bool, LibPQ.var"#98#99"{String}, LibPQ.Connection, LibPQ.AsyncResult{false}})()
       @ LibPQ ~/.julia/packages/LibPQ/1oHrl/src/asyncresults.jl:280
     [3] lock(f::LibPQ.var"#106#108"{Bool, LibPQ.var"#98#99"{String}, LibPQ.Connection, LibPQ.AsyncResult{false}}, conn::LibPQ.Connection)
       @ LibPQ ~/.julia/packages/LibPQ/1oHrl/src/connections.jl:337
     [4] (::LibPQ.var"#105#107"{Bool, LibPQ.var"#98#99"{String}, LibPQ.Connection, LibPQ.AsyncResult{false}})()
       @ LibPQ ./task.jl:417