Skip to content

Commit 6e2e582

Browse files
authored
Include value in error message about invalid value from register_method() (#460)
* Include value in error message about invalid value from `register_method()` Closes #447
1 parent 4c3cb81 commit 6e2e582

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

R/generic-spec.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ as_S3_generic <- function(x) {
2828
}
2929
}
3030

31-
stop("`generic` is a function, but not an S3 generic function", call. = FALSE)
31+
stop("`generic` is a function, but not an S3 generic function: \n",
32+
deparse_trunc(x, 100), call. = FALSE)
3233
}
3334

3435
S3_generic <- function(generic, name) {

R/utils.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ modify_list <- function (x, new_vals) {
144144
x
145145
}
146146

147+
deparse_trunc <- function(x, width, collapse = "\n") {
148+
x <- deparse1(x, collapse)
149+
if (nchar(x)> width)
150+
x <- sprintf("%s....", substr(x, 0, width-4))
151+
x
152+
}
153+
147154

148155
# For older versions of R ----------------------------------------------------
149156
deparse1 <- function(expr, collapse = " ", width.cutoff = 500L, ...) {

tests/testthat/_snaps/generic-spec.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
as_generic(function() { })
55
Condition
66
Error:
7-
! `generic` is a function, but not an S3 generic function
7+
! `generic` is a function, but not an S3 generic function:
8+
function ()
9+
{
10+
}
811

912
---
1013

0 commit comments

Comments
 (0)