fsprojects / SQLProvider

A general F# SQL database erasing type provider, supporting LINQ queries, schema exploration, individuals, CRUD operations and much more besides.
https://fsprojects.github.io/SQLProvider
Other
572 stars 146 forks source link

Errors as events #585

Closed Thorium closed 5 years ago

Thorium commented 5 years ago

If there is an error related to execution of SQL (e.g. timeout, something wrong with query parameters, or what ever) it would be nice to get the failed SQL clause also. Sometimes when executed as async there is no stack either, so it's hard to reason about what went wrong.

Currently the SQLProvider raises Event<SqlEventData> before executing any query. Maybe it could also raise some error events to evaluate failed SQL-clauses in a catch-rethrow manner?

Thorium commented 5 years ago

Turns out this is quite easy to do already by little wrapping in the user code. For example:

type sql.dataContext with
    /// SubmitUpdates() but on error ClearUpdates()
    member x.SubmitUpdatesAsyncDisplayErrors() =
        async {
            let sqlList = System.Collections.Concurrent.ConcurrentBag<FSharp.Data.Sql.Common.QueryEvents.SqlEventData>()
            use o = FSharp.Data.Sql.Common.QueryEvents.SqlQueryEvent |> Observable.subscribe(fun e -> sqlList.Add e)
            let! res = x.SubmitUpdatesAsync() |> Async.Catch
            match res with
            | Choice1Of2 _ -> ()
            | Choice2Of2 err ->
                printfn "Failed to update. %O" err
                sqlList |> Seq.iter(fun e -> printfn "SQL executed when error: %O" e)
        }