r-dbi / odbc

Connect to ODBC databases (using the DBI interface)
https://odbc.r-dbi.org/
Other
387 stars 107 forks source link

rethrow database errors #789

Closed simonpcouch closed 4 months ago

simonpcouch commented 4 months ago

A follow-up to #643. At the time, parsing the message any further was agreed to be more painful than it was worth. Following up on PRs since then that refine the appearance of errors, this PR proposes rethrowing nanodbc::database_errors with cli formatting.

Before this PR:

library(DBI)
library(odbc)
con <- dbConnect(odbc(), dsn = "sillyhead")
#> Error: nanodbc/nanodbc.cpp:1138: 00000
#> [unixODBC][Driver Manager]Data source name not found and no default driver specified

con <- dbConnect(odbc(), dsn = "MicrosoftSQLServer", uid = "SA", pwd = Sys.getenv("sqlServerPass"))

dbExecute(con, "SELECT * FROM boopbopbopbeep")
#> Error in eval(expr, envir, enclos): nanodbc/nanodbc.cpp:1710: 00000
#> [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid object name 'boopbopbopbeep'.

With this PR:

library(DBI)
library(odbc)
con <- dbConnect(odbc(), dsn = "sillyhead")
#> Error in `dbConnect()`:
#> ℹ At 'nanodbc/nanodbc.cpp:1147', error 00000 from [unixODBC][Driver
#>   Manager].
#> ✖ Data source name not found and no default driver specified

con <- dbConnect(odbc(), dsn = "MicrosoftSQLServer", uid = "SA", pwd = Sys.getenv("sqlServerPass"))

dbExecute(con, "SELECT * FROM boopbopbopbeep")
#> Error in `dbExecute()`:
#> ℹ At 'nanodbc/nanodbc.cpp:1719', error 00000 from [Microsoft][ODBC Driver
#>   18 for SQL Server][SQL Server].
#> ✖ Invalid object name 'boopbopbopbeep'.

Created on 2024-04-16 with reprex v2.1.0

In the RStudio terminal, this looks like:

Screenshot 2024-04-16 at 3 08 30 PM

simonpcouch commented 4 months ago

Mostly marked as draft because I know exception handling is... A Topic... but I'm not sure I'll make much more progress here without review. Requesting now.

simonpcouch commented 4 months ago

Confirmed GHA failures on macOS are unrelated in the issue linked above.