diff --git a/R-package/R/utils.R b/R-package/R/utils.R index 9b036f91db8d..1e0e759d653b 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -19,6 +19,8 @@ lgb.encode.char <- function(arr, len) { } +# [description] Raise an error. Before raising that error, check for any error message +# stored in a buffer on the C++ side. lgb.last_error <- function() { # Perform text error buffering buf_len <- 200L diff --git a/R-package/tests/testthat/test_utils.R b/R-package/tests/testthat/test_utils.R index a16e6f742061..9765356a0df8 100644 --- a/R-package/tests/testthat/test_utils.R +++ b/R-package/tests/testthat/test_utils.R @@ -48,3 +48,23 @@ test_that("lgb.params2str() works as expected for a key in params with multiple , "objective=magic metric=a,ab,abc,abcdefg nrounds=10 learning_rate=0.0000001" ) }) + +context("lgb.last_error") + +test_that("lgb.last_error() throws an error if there are no errors", { + expect_error({ + lgb.last_error() + }, regexp = "Everything is fine") +}) + +test_that("lgb.last_error() correctly returns errors from the C++ side", { + data(agaricus.train, package = "lightgbm") + train <- agaricus.train + dvalid1 <- lgb.Dataset( + data = train$data + , label = as.matrix(rnorm(5L)) + ) + expect_error({ + dvalid1$construct() + }, regexp = "[LightGBM] [Fatal] Length of label is not same with #data", fixed = TRUE) +})