我正在构建一个包,我希望更好地控制错误消息
我读过哈德利关于处理异常的章节,但我还是迷路了
这是一个简单的例子:
myfunc = function(x){
intial_result = x + 1
final_result = initial_result + 1
return(final_result)
}
如果你运行myfunc("1")
你会得到错误:
> myfunc("1")
Error in x + 1 : non-numeric argument to binary operator
但这并没有告诉用户错误发生在哪里。
所以我尝试使用tryCatch
,它的工作原理是:
myfunc = function(x){
r = tryCatch(
expr = {x + 1},
error = function(e){
message = paste0("argument must be a number", "\nFor example, x = 2")
e = simpleError(message)
stop(e)
}
)
r2 = r + 1
return(r2)
}
但这是处理自定义错误消息的最佳方式吗?
此外,有没有办法只抛出一个message()
并且仍然stop()
是程序?(因为stop()
会激活调试器)
编辑我希望有像以下错误消息dplyr
:
library(dplyr)
data('starwars')
select(starwars, x)
Error: Can't subset columns that don't exist.
x Column `x` doesn't exist.
Run `rlang::last_error()` to see where the error occur