返回 R 中抛出的最后一个错误消息以外的错误消息
Returning an error message other than the last one thrown in R
我有两个构造 R S3 对象的函数。一个对象封装另一个对象。当用户将错误的 num
参数传递给 fun2()
时,它会在 fun2()
中抛出错误断言。我认为用户在 fun1()
中看到消息会提供更多信息。有哪些选项可以保留 fun1()
中提出的消息并提出该消息而不是 fun2()
中提出的消息?
fun1 <- function(num) {
assert_that(num %% 2 == 0, msg = "num should be even")
structure(num,
class = "fun1-Class"
)
}
fun2 <- function(text, num) {
assert_that(class(num) == "fun1-Class", msg = "Bad Class")
structure(list(text, num),
class = "fun2-Class"
)
}
fun1(1.2)
# Throws error "num should be even"
x <- fun2("myText", fun1(1.2))
# Throws error "Bad Class"
追溯在下方
Error: Bad Class
3.
stop(assertError(attr(res, "msg")))
2.
assert_that(class(num) == "fun1-Class", msg = "Bad Class")
1.
fun2("text", fun1(1.2))
@aurèle 在评论中回答了这个问题。
发生这种情况是因为函数参数的惰性求值。 force(num)
将更改行为并评估第一个构造函数。
我有两个构造 R S3 对象的函数。一个对象封装另一个对象。当用户将错误的 num
参数传递给 fun2()
时,它会在 fun2()
中抛出错误断言。我认为用户在 fun1()
中看到消息会提供更多信息。有哪些选项可以保留 fun1()
中提出的消息并提出该消息而不是 fun2()
中提出的消息?
fun1 <- function(num) {
assert_that(num %% 2 == 0, msg = "num should be even")
structure(num,
class = "fun1-Class"
)
}
fun2 <- function(text, num) {
assert_that(class(num) == "fun1-Class", msg = "Bad Class")
structure(list(text, num),
class = "fun2-Class"
)
}
fun1(1.2)
# Throws error "num should be even"
x <- fun2("myText", fun1(1.2))
# Throws error "Bad Class"
追溯在下方
Error: Bad Class
3.
stop(assertError(attr(res, "msg")))
2.
assert_that(class(num) == "fun1-Class", msg = "Bad Class")
1.
fun2("text", fun1(1.2))
@aurèle 在评论中回答了这个问题。
发生这种情况是因为函数参数的惰性求值。 force(num)
将更改行为并评估第一个构造函数。