assertthat
assertthat copied to clipboard
error occurs in the inner function, but only reported in the outer function
When calling nested functions, the error is reported in the outer function outer_f()
, while it happens in the inner function inner_f()
. In complex cases this is very difficult to debug.
The correct error message ("inner_f"
) is reported if we:
- use
stopifnot()
instead ofassert_that()
- do not override the
msg
argument
I suspect this has something to do with lazy evaluation, but not sure.
inner_f <- function(argument) {
assertthat::assert_that(argument > 0, msg = "inner_f")
argument
}
outer_f <- function(argument) {
assertthat::assert_that(argument > 0, msg = "outer_f")
}
outer_f(inner_f(0))
#> Error: outer_f
Created on 2021-11-04 by the reprex package (v2.0.1)
Session info
sessioninfo::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────────
#> hash: raised back of hand: light skin tone, non-potable water, right arrow
#>
#> setting value
#> version R version 4.0.1 (2020-06-06)
#> os CentOS Linux 7 (Core)
#> system x86_64, linux-gnu
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz Europe/Zurich
#> date 2021-11-04
#> pandoc 2.7.3 @ <...>
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.0.1)
#> backports 1.3.0 2021-10-27 [2] CRAN (R 4.0.1)
#> cli 3.1.0 2021-10-27 [2] CRAN (R 4.0.1)
#> crayon 1.4.2 2021-10-29 [2] CRAN (R 4.0.1)
#> digest 0.6.28 2021-09-23 [2] CRAN (R 4.0.1)
#> ellipsis 0.3.2 2021-04-29 [2] CRAN (R 4.0.1)
#> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.1)
#> fansi 0.5.0 2021-05-25 [2] CRAN (R 4.0.1)
#> fastmap 1.1.0 2021-01-25 [2] CRAN (R 4.0.1)
#> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.1)
#> glue 1.4.2 2020-08-27 [2] CRAN (R 4.0.1)
#> highr 0.9 2021-04-16 [2] CRAN (R 4.0.1)
#> htmltools 0.5.2 2021-08-25 [2] CRAN (R 4.0.1)
#> knitr 1.36 2021-09-29 [2] CRAN (R 4.0.1)
#> lifecycle 1.0.1 2021-09-24 [2] CRAN (R 4.0.1)
#> magrittr 2.0.1 2020-11-17 [2] CRAN (R 4.0.1)
#> pillar 1.6.4 2021-10-18 [2] CRAN (R 4.0.1)
#> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.0.1)
#> purrr 0.3.4 2020-04-17 [2] CRAN (R 4.0.1)
#> R.cache 0.15.0 2021-04-30 [2] CRAN (R 4.0.1)
#> R.methodsS3 1.8.1 2020-08-26 [2] CRAN (R 4.0.1)
#> R.oo 1.24.0 2020-08-26 [2] CRAN (R 4.0.1)
#> R.utils 2.11.0 2021-09-26 [2] CRAN (R 4.0.1)
#> reprex 2.0.1 2021-08-05 [2] CRAN (R 4.0.1)
#> rlang 0.4.12 2021-10-18 [2] CRAN (R 4.0.1)
#> rmarkdown 2.11 2021-09-14 [2] CRAN (R 4.0.1)
#> rstudioapi 0.13 2020-11-12 [2] CRAN (R 4.0.1)
#> sessioninfo 1.2.1 2021-11-02 [2] CRAN (R 4.0.1)
#> stringi 1.7.5 2021-10-04 [2] CRAN (R 4.0.1)
#> stringr 1.4.0 2019-02-10 [2] CRAN (R 4.0.1)
#> styler 1.6.2 2021-09-23 [2] CRAN (R 4.0.1)
#> tibble 3.1.5 2021-09-30 [2] CRAN (R 4.0.1)
#> utf8 1.2.2 2021-07-24 [2] CRAN (R 4.0.1)
#> vctrs 0.3.8 2021-04-29 [2] CRAN (R 4.0.1)
#> withr 2.4.2 2021-04-18 [2] CRAN (R 4.0.1)
#> xfun 0.27 2021-10-18 [2] CRAN (R 4.0.1)
#> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.1)
#>
#> [1] <...>
#> [2] <...>
#> [3] <...>
#>
#> ──────────────────────────────────────────────────────────────────────────────
@idavydov it's not a problem with lazy evaluation (because it's working properly when there's no msg
), but a problem with the propagation of msg
from the inner to the outer
see #71 for a fix