You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by pa...@apache.org on 2023/05/24 14:34:35 UTC

[arrow-adbc] branch main updated: feat(r): Improve error communication (#703)

This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new d26aeb0  feat(r): Improve error communication (#703)
d26aeb0 is described below

commit d26aeb09edcfa0029d3ffd1bbf6b99b55cf66666
Author: Dewey Dunnington <de...@voltrondata.com>
AuthorDate: Wed May 24 10:34:30 2023 -0400

    feat(r): Improve error communication (#703)
    
    Before this PR, errors coming from ADBC were rather unhelpfully
    labelled:
    
    ``` r
    library(adbcdrivermanager)
    
    db <- adbc_database_init(adbc_driver_void())
    adbc_database_release(db)
    adbc_database_release(db)
    #> Error in stop_for_error(status, error): ADBC_STATUS_INVALID_STATE (6)
    ```
    
    After this PR, the source of the error is better communicated and errors
    are given a custom class so they can be caught appropriately:
    
    ``` r
    library(adbcdrivermanager)
    
    db <- adbc_database_init(adbc_driver_void())
    adbc_database_release(db)
    adbc_database_release(db)
    #> Error in adbc_database_release(db): ADBC_STATUS_INVALID_STATE (6)
    
    tryCatch({
      db <- adbc_database_init(adbc_driver_void())
      adbc_database_release(db)
      adbc_database_release(db)
    }, adbc_status_invalid_state = function(e) {
      message(sprintf("Just warning for ADBC status with code %d", e$error$status))
    })
    #> Just warning for ADBC status with code 6
    ```
    
    <sup>Created on 2023-05-24 with [reprex
    v2.0.2](https://reprex.tidyverse.org)</sup>
---
 r/adbcdrivermanager/R/error.R                      | 22 ++++++++++++++++++++--
 .../tests/testthat/_snaps/driver_log.md            |  2 +-
 r/adbcdrivermanager/tests/testthat/test-error.R    | 16 ++++++++++++++++
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/r/adbcdrivermanager/R/error.R b/r/adbcdrivermanager/R/error.R
index bc20eda..8187a55 100644
--- a/r/adbcdrivermanager/R/error.R
+++ b/r/adbcdrivermanager/R/error.R
@@ -21,8 +21,26 @@ adbc_allocate_error <- function(shelter = NULL) {
 
 stop_for_error <- function(status, error) {
   if (!identical(status, 0L)) {
-    msg <- if (!is.null(error$message)) error$message else .Call(RAdbcStatusCodeMessage, status)
-    stop(msg)
+    error <- .Call(RAdbcErrorProxy, error)
+    error$status <- status
+    error$status_code_message <- .Call(RAdbcStatusCodeMessage, status)
+    msg <- if (!is.null(error$message)) error$message else error$status_code_message
+
+    # Gives an error class like "adbc_status_invalid_state", "adbc_status",
+    # "simpleError", ...
+    cnd_class <- c(
+      tolower(gsub("\\s+.*", "", error$status_code_message)),
+      "adbc_status"
+    )
+
+    # Strips stop_for_error() call from the error output
+    cnd <- simpleError(msg, call = sys.call(-1))
+    class(cnd) <- union(cnd_class, class(cnd))
+
+    # Store the listified error information
+    cnd$error <- error
+
+    stop(cnd)
   }
 }
 
diff --git a/r/adbcdrivermanager/tests/testthat/_snaps/driver_log.md b/r/adbcdrivermanager/tests/testthat/_snaps/driver_log.md
index 9ddaa7b..fdbbe93 100644
--- a/r/adbcdrivermanager/tests/testthat/_snaps/driver_log.md
+++ b/r/adbcdrivermanager/tests/testthat/_snaps/driver_log.md
@@ -21,7 +21,7 @@
       try(adbc_statement_execute_query(stmt))
     Output
       LogStatementExecuteQuery()
-      Error in stop_for_error(result$status, error) : 
+      Error in adbc_statement_execute_query(stmt) : 
         ADBC_STATUS_NOT_IMPLEMENTED (2)
     Code
       adbc_statement_release(stmt)
diff --git a/r/adbcdrivermanager/tests/testthat/test-error.R b/r/adbcdrivermanager/tests/testthat/test-error.R
index 49f19a5..e6b1cc1 100644
--- a/r/adbcdrivermanager/tests/testthat/test-error.R
+++ b/r/adbcdrivermanager/tests/testthat/test-error.R
@@ -27,3 +27,19 @@ test_that("error allocator works", {
   expect_identical(err$vendor_code, 0L)
   expect_identical(err$sqlstate, as.raw(c(0x00, 0x00, 0x00, 0x00, 0x00)))
 })
+
+test_that("stop_for_error() gives a custom error class with extra info", {
+  had_error <- FALSE
+  tryCatch({
+    db <- adbc_database_init(adbc_driver_void())
+    adbc_database_release(db)
+    adbc_database_release(db)
+  }, adbc_status = function(e) {
+    had_error <<- TRUE
+    expect_s3_class(e, "adbc_status")
+    expect_s3_class(e, "adbc_status_invalid_state")
+    expect_identical(e$error$status, 6L)
+  })
+
+  expect_true(had_error)
+})