diff --git a/R-package/R/utils.R b/R-package/R/utils.R index 38827b3fe9f2..cfc26bc25734 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -25,38 +25,15 @@ lgb.encode.char <- function(arr, len) { return(rawToChar(arr[seq_len(len)])) } -# [description] Raise an error. Before raising that error, check for any error message -# stored in a buffer on the C++ side. +# [description] Get the most recent error stored on the C++ side and raise it +# as an R error. lgb.last_error <- function() { - # Perform text error buffering - buf_len <- 200L - act_len <- 0L - err_msg <- raw(buf_len) err_msg <- .Call( LGBM_GetLastError_R - , buf_len - , act_len - , err_msg ) - - # Check error buffer - if (act_len > buf_len) { - buf_len <- act_len - err_msg <- raw(buf_len) - err_msg <- .Call( - LGBM_GetLastError_R - , buf_len - , act_len - , err_msg - ) - } - - stop("api error: ", lgb.encode.char(arr = err_msg, len = act_len)) - + stop("api error: ", err_msg) return(invisible(NULL)) - } - lgb.params2str <- function(params, ...) { # Check for a list as input diff --git a/R-package/src/lightgbm_R.cpp b/R-package/src/lightgbm_R.cpp index d52c7bbeba97..3bffdc40dda7 100644 --- a/R-package/src/lightgbm_R.cpp +++ b/R-package/src/lightgbm_R.cpp @@ -56,8 +56,12 @@ LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, LGBM_SE actua return dest; } -LGBM_SE LGBM_GetLastError_R(LGBM_SE buf_len, LGBM_SE actual_len, LGBM_SE err_msg) { - return EncodeChar(err_msg, LGBM_GetLastError(), buf_len, actual_len, std::strlen(LGBM_GetLastError()) + 1); +SEXP LGBM_GetLastError_R() { + SEXP out; + out = PROTECT(Rf_allocVector(STRSXP, 1)); + SET_STRING_ELT(out, 0, Rf_mkChar(LGBM_GetLastError())); + UNPROTECT(1); + return out; } SEXP LGBM_DatasetCreateFromFile_R(LGBM_SE filename, @@ -640,7 +644,7 @@ SEXP LGBM_BoosterDumpModel_R(LGBM_SE handle, // .Call() calls static const R_CallMethodDef CallEntries[] = { - {"LGBM_GetLastError_R" , (DL_FUNC) &LGBM_GetLastError_R , 3}, + {"LGBM_GetLastError_R" , (DL_FUNC) &LGBM_GetLastError_R , 0}, {"LGBM_DatasetCreateFromFile_R" , (DL_FUNC) &LGBM_DatasetCreateFromFile_R , 4}, {"LGBM_DatasetCreateFromCSC_R" , (DL_FUNC) &LGBM_DatasetCreateFromCSC_R , 9}, {"LGBM_DatasetCreateFromMat_R" , (DL_FUNC) &LGBM_DatasetCreateFromMat_R , 6}, diff --git a/R-package/src/lightgbm_R.h b/R-package/src/lightgbm_R.h index 32910df59168..c42dfcc42098 100644 --- a/R-package/src/lightgbm_R.h +++ b/R-package/src/lightgbm_R.h @@ -15,11 +15,7 @@ * \return err_msg error information * \return error information */ -LIGHTGBM_C_EXPORT LGBM_SE LGBM_GetLastError_R( - LGBM_SE buf_len, - LGBM_SE actual_len, - LGBM_SE err_msg -); +LIGHTGBM_C_EXPORT SEXP LGBM_GetLastError_R(); // --- start Dataset interface diff --git a/build_r.R b/build_r.R index 9f8b95941a57..f7765eb411a0 100644 --- a/build_r.R +++ b/build_r.R @@ -397,7 +397,7 @@ c_api_contents <- gsub( , x = c_api_contents ) c_api_symbols <- gsub( - pattern = "\\(" + pattern = "\\(.*" , replacement = "" , x = c_api_contents )