Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use error parse/explain code from reside.utils #69

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: monty
Title: Monte Carlo Models
Version: 0.2.3
Version: 0.2.4
Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"),
email = "[email protected]"),
person("Wes", "Hinsley", role = "aut"),
Expand Down
30 changes: 6 additions & 24 deletions R/dsl-parse-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,15 @@
##' @param code The error code, as a string, in the form `Exxx` (a
##' capital "E" followed by three numbers)
##'
##' @param how How to explain the error. Options are `pretty` (render
##' pretty text in the console), `plain` (display plain text in the
##' console) and `link` (browse to the online help).
##'
##' @return Nothing, this is called for its side effect only
##'
##' @export
monty_dsl_error_explain <- function(code) {
## See odin2 for the canonical implementation of this, we're just
## shadowing it here really as our dsl is much simpler. Note that
## error codes from monty are three numbers long, whereas they are
## four long in odin so that it's easy (for us) to tell who the
## error belongs to.
assert_scalar_character(code)
if (!grepl("^E[0-9]{3}$", code)) {
cli::cli_abort("Invalid code '{code}', should match 'Exxx'",
arg = "code")
}
txt <- dsl_errors[[code]]
if (is.null(txt)) {
cli::cli_abort(
c("Error '{code}' is undocumented",
i = paste("If you were directed here from an error message, please",
"let us know (e.g., file an issue or send us a message)")),
arg = "code")
}
url <- sprintf(
"https://mrc-ide.github.io/monty/articles/dsl-errors.html#%s",
tolower(code))
utils::browseURL(url)
monty_dsl_error_explain <- function(code, how = "pretty") {
error_explain(dsl_errors, code, how)
}


Expand Down Expand Up @@ -70,7 +53,6 @@ cnd_footer.monty_parse_error <- function(cnd, ...) {
## to some number of spaces, probably.
detail <- gsub(" ", "\u00a0", detail)


code <- cnd$code
## See https://cli.r-lib.org/reference/links.html#click-to-run-code
## RStudio will only run code in namespaced form
Expand Down
75 changes: 75 additions & 0 deletions R/import-standalone-error-render.R

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 70 additions & 9 deletions R/import-standalone-utils-assert.R

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified R/sysdata.rda
Binary file not shown.
6 changes: 5 additions & 1 deletion man/monty_dsl_error_explain.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 2 additions & 32 deletions scripts/update_parse_errors
Original file line number Diff line number Diff line change
@@ -1,34 +1,4 @@
#!/usr/bin/env Rscript

read_errors <- function() {
path_rmd <- "vignettes/dsl-errors.Rmd"
txt <- readLines(path_rmd)

re <- "^# `(E[0-9]{3})`$"
i <- grep(re, txt)
if (length(setdiff(grep("^# ", txt), i)) > 0) {
stop("Some headings don't match expected pattern")
}

f <- function(from, to) {
ret <- txt[from:to]
while (ret[[1]] == "") {
ret <- ret[-1]
}
while (ret[[length(ret)]] == "") {
ret <- ret[-length(ret)]
}
ret
}

ret <- Map(f, i + 1, c(i[-1] - 1, length(txt)))
names(ret) <- sub(re, "\\1", txt[i])
ret
}


## We might parse this further, e.g., with commonmark, so that we can
## render this nicely to the console as cli would make this pretty
## easy really.
dsl_errors <- read_errors()
dsl_errors <- reside.utils::errors_parse(
"vignettes/dsl-errors.Rmd", "E[0-9]{3}", "monty::monty_dsl_error_explain")
save(list = "dsl_errors", file = file.path("R/sysdata.rda"), version = 2)
30 changes: 5 additions & 25 deletions tests/testthat/test-dsl-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,31 +202,11 @@ test_that("report back invalid distribution calls", {

test_that("can explain an error", {
skip_if_not_installed("mockery")
mock_browse <- mockery::mock()
mockery::stub(monty_dsl_error_explain, "utils::browseURL", mock_browse)
mock_explain <- mockery::mock()
mockery::stub(monty_dsl_error_explain, "error_explain", mock_explain)
monty_dsl_error_explain("E101")
mockery::expect_called(mock_browse, 1)
mockery::expect_called(mock_explain, 1)
expect_equal(
mockery::mock_args(mock_browse)[[1]],
list("https://mrc-ide.github.io/monty/articles/dsl-errors.html#e101"))
})


test_that("error if given invalid code", {
msg <- "Invalid code 'E01', should match 'Exxx'"
expect_error(monty_dsl_error_explain("E01"),
"Invalid code 'E01', should match 'Exxx'")
expect_error(monty_dsl_error_explain("e0001"),
"Invalid code 'e0001', should match 'Exxx'")
expect_error(monty_dsl_error_explain("E0001"),
"Invalid code 'E0001', should match 'Exxx'")
expect_error(monty_dsl_error_explain("anything"),
"Invalid code 'anything', should match 'Exxx'")
})


test_that("error if given unknown code", {
expect_error(
monty_dsl_error_explain("E999"),
"Error 'E999' is undocumented")
mockery::mock_args(mock_explain)[[1]],
list(dsl_errors, "E101", "pretty"))
})
Loading