Skip to content

Commit

Permalink
nanodbc: multi-line error message (#643)
Browse files Browse the repository at this point in the history
Fixes #564
  • Loading branch information
detule authored Dec 11, 2023
1 parent 56494c0 commit b55664b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# odbc (development version)

* ODBC errors are now spread across multiple lines, making them easier to
read (@detule, #564).

* `dbListTables()`, `dbListFields()` and `dbExistsTable()` automatically
escape underscores in identifier arguments. This leads to substantial
performance improvements for some backends (e.g. snowflake)
Expand Down
4 changes: 2 additions & 2 deletions src/nanodbc/nanodbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ recent_error(SQLHANDLE handle, SQLSMALLINT handle_type, long& native, std::strin
}

if (!result.empty())
result += ' ';
result += '\n';

result += nanodbc::string_type(sql_message.begin(), sql_message.end());
i++;
Expand All @@ -411,7 +411,7 @@ recent_error(SQLHANDLE handle, SQLSMALLINT handle_type, long& native, std::strin
state = std::string(&sql_state[0], &sql_state[arrlen(sql_state) - 1]);
native = native_error;
std::string status = state;
status += ": ";
status += "\n";
status += rvalue;

// some drivers insert \0 into error messages for unknown reasons
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-SQLServer.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,14 @@ test_that("SQLServer", {
# res <- dbGetQuery(con, paste0("SELECT * FROM ", locTblName))
# expect_equal( nrow( res ), 2 * nrow( mtcars ) )
})

test_that("Multiline error message", {
tryCatch({
DBI::dbConnect(odbc::odbc(), dsn = "does_not_exist_db")
}, error = function(e) {
# Expect to see at least one newline character in message
# ( previously one long string, #643 )
expect_true(grepl("\n", e$message))
})
})
})

0 comments on commit b55664b

Please sign in to comment.