-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Classed error conditions for programmatic error handling #7139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
68cc421
add classed error conditions
Mukulyadav2004 7041d8d
invalid_input in place of invalid_type
Mukulyadav2004 0a583fa
right place invalid_input
Mukulyadav2004 42d63ad
typo and unsortable in place of unsupported
Mukulyadav2004 0602e72
specify join type
Mukulyadav2004 555fcc7
added vignettes
Mukulyadav2004 cc12e82
Merge branch 'issue#5913' of https://github.com/Rdatatable/data.table…
Mukulyadav2004 a210f23
tweak wording
MichaelChirico 21a3c51
handle wasted space after \n by adding sep=""
Mukulyadav2004 99f5bff
added classed error to bmerge and setops
Mukulyadav2004 71d2cbd
prepared docs page for error handling with classed conditions
Mukulyadav2004 75d48d9
added linter page for sync
Mukulyadav2004 390fda6
add reference link of test as well trycatch2
Mukulyadav2004 88ce984
for all condition rather errors only
Mukulyadav2004 cf21327
remove redundant test
MichaelChirico 404bd0a
remove redundant test of length, too
MichaelChirico 6a3d11f
included other *f calls
Mukulyadav2004 5244b56
typo
Mukulyadav2004 7694219
simplify by checking string
MichaelChirico 1cd1600
rename for consistency
MichaelChirico 71563f8
sync to linter implementation
MichaelChirico 3508e73
forgot to -a renamed file
MichaelChirico e013435
sync docs with code
Mukulyadav2004 6507f39
future-proof wording
MichaelChirico 5be1427
remove superfluous reference
MichaelChirico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Ensure that data.table condition classes in code match documentation | ||
| condition_classes_documentation_linter = function(rd_file) { | ||
| if (!grepl("\\name{datatable-condition-classes}", readChar(rd_file, 100L), fixed = TRUE)) return(invisible()) | ||
|
|
||
| # Find condition classes in R code | ||
| walk_r_ast_for_classes = function(expr) { | ||
| if (is.call(expr) && identical(expr[[1L]], quote(stopf)) && is.character(class_arg <- expr[["class"]]) && startsWith(class_arg, "dt_")) { | ||
| class_arg | ||
| } else if (is.recursive(expr)) { | ||
| unlist(lapply(expr, walk_r_ast_for_classes)) | ||
| } | ||
| } | ||
|
|
||
| # Find condition classes in documentation | ||
| walk_rd_ast_for_classes = function(rd_element) { | ||
| if (!is.list(rd_element)) return(character()) | ||
|
|
||
| result = character() | ||
| if (isTRUE(attr(rd_element, "Rd_tag") == "\\code") && length(rd_element) >= 1L) { | ||
| content = rd_element[[1L]] | ||
| if (is.character(content) && startsWith(content, "dt_")) { | ||
| result = content | ||
| } | ||
| } | ||
| c(result, unlist(lapply(rd_element, walk_rd_ast_for_classes))) | ||
| } | ||
|
|
||
| code_classes = list.files("R", pattern = "\\.R$", full.names = TRUE) |> | ||
| lapply(\(f) lapply(parse(f), walk_r_ast_for_classes)) |> | ||
| unlist() |> | ||
| unique() | ||
|
|
||
| doc_classes = rd_file |> | ||
| tools::parse_Rd() |> | ||
| walk_rd_ast_for_classes() |> | ||
| unique() | ||
|
|
||
| miss_in_doc = setdiff(code_classes, doc_classes) | ||
| miss_in_code = setdiff(doc_classes, code_classes) | ||
|
|
||
| if (length(miss_in_doc) > 0L || length(miss_in_code) > 0L) { | ||
| if (length(miss_in_doc) > 0L) { | ||
| cat(sprintf("Condition classes in code but missing from docs: %s\n", toString(miss_in_doc))) | ||
| } | ||
| if (length(miss_in_code) > 0L) { | ||
| cat(sprintf("Condition classes in docs but not in code: %s\n", toString(miss_in_code))) | ||
| } | ||
| stop("Please sync man/datatable-condition-classes.Rd with code condition classes") | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| \name{data.table-error-handling} | ||
| \alias{data.table-error-handling} | ||
| \title{Condition Handling with Classed Conditions} | ||
| \description{ | ||
| \code{data.table} provides specific condition classes for common operations, making it easier to handle comditions programmatically. This is particularly useful when writing robust code or packages that use \code{data.table}. Relying on the exact text of condition messages is fragile (it is not uncommon to change the wording slightly, or for the user's session not to be in English); prefer using the signal class where possible. | ||
|
MichaelChirico marked this conversation as resolved.
Outdated
|
||
| } | ||
| \details{ | ||
| \subsection{Available error Classes}{ | ||
|
MichaelChirico marked this conversation as resolved.
Outdated
|
||
| \code{data.table} provides four specific error classes: | ||
|
|
||
| \itemize{ | ||
| \item \code{dt_missing_column_error}: When referencing columns that don't exist | ||
| \item \code{dt_invalid_input_error}: When providing invalid input types or empty required arguments | ||
| \item \code{dt_unsortable_type_error}: When trying to sort/key unsupported types | ||
| \item \code{dt_join_type_mismatch_error}: When column types are incompatible in joins/set operations | ||
| } | ||
| } | ||
|
|
||
| \subsection{Backward Compatibility}{ | ||
| All condition classes inherit from base R's condition system, so existing \code{tryCatch(..., error = ...)} code continues to work unchanged. The new classes simply provide more specific handling options when needed. | ||
| } | ||
| } | ||
| \examples{ | ||
|
|
||
| # Handle missing column errors specifically | ||
| DT <- data.table(a = 1:3, b = 4:6) | ||
| tryCatch({ | ||
| setkey(DT, nonexistent_col) | ||
| }, dt_missing_column_error = function(e) { | ||
| cat("Missing column detected:", conditionMessage(e), "\n") | ||
| }, error = function(e) { | ||
| cat("Other error:", conditionMessage(e), "\n") | ||
| }) | ||
| } | ||
| \seealso{ | ||
| \code{\link{tryCatch}}, \code{\link{test}}, \url{https://adv-r.hadley.nz/conditions.html} | ||
| For advanced exception handling patterns, see data.table's internal helper `tryCatch2` here - \url{https://github.com/jangorecki/logR/blob/master/R/logR.R} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.