You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Classed error conditions for programmatic error handling (#7139)
* add classed error conditions
* invalid_input in place of invalid_type
* right place invalid_input
* typo and unsortable in place of unsupported
* specify join type
* added vignettes
* tweak wording
* handle wasted space after \n by adding sep=""
* added classed error to bmerge and setops
* prepared docs page for error handling with classed conditions
* added linter page for sync
* add reference link of test as well trycatch2
* for all condition rather errors only
* remove redundant test
* remove redundant test of length, too
* included other *f calls
* typo
* simplify by checking string
* rename for consistency
* sync to linter implementation
* forgot to -a renamed file
* sync docs with code
* future-proof wording
* remove superfluous reference
---------
Co-authored-by: Michael Chirico <[email protected]>
stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, x_merge_type, iname, i_merge_type)
93
+
stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, x_merge_type, iname, i_merge_type, class="dt_join_type_mismatch_error")
94
94
}
95
95
if (x_merge_type==i_merge_type) {
96
96
if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, x_merge_type, xname)
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and cannot be coerced to integer64 (e.g. has fractions)", nm[2L], nm[1L])
117
+
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and cannot be coerced to integer64 (e.g. has fractions)", nm[2L], nm[1L], class="dt_join_type_mismatch_error")
118
118
} else {
119
119
# just integer and double left
120
120
ic_idx= which(icol==icols) # check if on is joined on multiple conditions, #6602
if (".xi" %chin% names(x)) stopf("x contains a column called '.xi'. Conflicts with internal use by data.table.")
55
55
for (iincols) {
56
56
.xi=x[[i]] # [[ is copy on write, otherwise checking type would be copying each column
57
-
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported as a key column type, currently.", i, typeof(.xi))
57
+
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported as a key column type, currently.", i, typeof(.xi), class="dt_unsortable_type_error")
58
58
}
59
59
if (!is.character(cols) || length(cols)<1L) internal_error("'cols' should be character at this point") # nocov
if (any(miss)) stopf("some columns are not in the data.table: %s", brackify(cols[miss]))
269
+
if (any(miss)) stopf("some columns are not in the data.table: %s", brackify(cols[miss]), class="dt_missing_column_error")
270
270
if (".xi" %chin% colnames(x)) stopf("x contains a column called '.xi'. Conflicts with internal use by data.table.")
271
271
for (iincols) {
272
272
.xi=x[[i]] # [[ is copy on write, otherwise checking type would be copying each column
273
-
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported for ordering currently.", i, typeof(.xi))
273
+
if (!typeof(.xi) %chin% ORDERING_TYPES) stopf("Column '%s' is type '%s' which is not supported for ordering currently.", i, typeof(.xi), class="dt_unsortable_type_error")
274
274
}
275
275
if (!is.character(cols) || length(cols)<1L) internal_error("'cols' should be character at this point") # nocov
if ( is.character(x[[rc]]) &&!(is.character(y[[lc]]) || is.factor(y[[lc]])) ) {
17
-
stopf("When x's column ('%s') is character, the corresponding column in y ('%s') should be factor or character, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]))
17
+
stopf("When x's column ('%s') is character, the corresponding column in y ('%s') should be factor or character, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]), class="dt_join_type_mismatch_error")
stopf("When x's column ('%s') is factor, the corresponding column in y ('%s') should be character or factor, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]))
19
+
stopf("When x's column ('%s') is factor, the corresponding column in y ('%s') should be character or factor, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]), class="dt_join_type_mismatch_error")
stopf("When x's column ('%s') is integer or numeric, the corresponding column in y ('%s') can not be character or logical types, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]))
21
+
stopf("When x's column ('%s') is integer or numeric, the corresponding column in y ('%s') can not be character or logical types, but found incompatible type '%s'.", xcnam, icnam, typeof(y[[lc]]), class="dt_join_type_mismatch_error")
22
22
}
23
23
}
24
24
ux= unique(shallow(x, by.x))
@@ -52,7 +52,7 @@ funique = function(x) {
52
52
}
53
53
if (!identical(sx<-sapply(x, super), sy<-sapply(y, super))) {
54
54
w= which.first(sx!=sy)
55
-
stopf("Item %d of x is '%s' but the corresponding item of y is '%s'.", w, class1(x[[w]]), class1(y[[w]]))
55
+
stopf("Item %d of x is '%s' but the corresponding item of y is '%s'.", w, class1(x[[w]]), class1(y[[w]]), class="dt_join_type_mismatch_error")
56
56
}
57
57
if (.seqn&&".seqn" %chin% names(x)) stopf("None of the datasets should contain a column named '.seqn'")
\code{data.table} providesspecificconditionclassesforcommonoperations, makingiteasiertohandleconditionsprogrammatically.Thisisparticularlyusefulwhenwritingrobustcodeorpackagesthatuse \code{data.table}.Relyingontheexacttextofconditionmessagesis fragile (itisnotuncommontochangethewordingslightly, orfortheuser's session not to be in English); prefer using the signal class where possible.
6
+
}
7
+
\details{
8
+
\subsection{Available Condition Classes}{
9
+
\code{data.table} provides the following specific condition classes:
10
+
11
+
Error Classes:
12
+
\itemize{
13
+
\item \code{dt_missing_column_error}: When referencing columns that don'texist
AllconditionclassesinheritfrombaseR's condition system, so existing \code{tryCatch(..., error = ...)} code continues to work unchanged. The new classes simply provide more specific handling options when needed.
0 commit comments