| 1 |
to_sentence_case <- function(x) { |
| 2 |
paste0( |
| 3 |
toupper(substr(x, 1, 1)), |
| 4 |
substr(x, 2, nchar(x)), |
| 5 |
ifelse(substr(x, nchar(x), nchar(x)) == ".", "", ".") |
| 6 |
) |
| 7 |
} |
| 8 |
|
| 9 |
stop_inja <- function(type, message, line, column) { |
| 10 |
cls <- c(paste0("jinjar_", type), "jinjar_error") |
| 11 |
message <- to_sentence_case(message) |
| 12 |
context <- "Error occurred on {.field line {line}} and {.field column {column}}." |
| 13 |
|
| 14 |
hypothesis <- if (message == "Object must be an array.") { |
| 15 |
"Have you forgotten to wrap a length-1 vector with I()?" |
| 16 |
} else { |
| 17 |
NULL |
| 18 |
} |
| 19 |
|
| 20 |
cli::cli_abort(c("{message}", "i" = hypothesis, "i" = context), class = cls, call = NULL) |
| 21 |
} |
| 22 |
|
| 23 |
stop_json <- function(message, data) { |
| 24 |
cls <- c("jinjar_json_error", "jinjar_error") |
| 25 |
context <- "JSON object: {.val {data}}" |
| 26 |
|
| 27 |
cli::cli_abort(c("{message}", "i" = context), class = cls, call = NULL) |
| 28 |
} |
| 29 |
|
| 30 |
with_catch_cpp_errors <- function(expr, call = caller_env()) { |
| 31 |
try_fetch( |
| 32 |
expr, |
| 33 |
jinjar_file_error = function(cnd) { |
| 34 |
abort("Problem encountered while reading template.", parent = cnd, call = call) |
| 35 |
}, |
| 36 |
jinjar_parser_error = function(cnd) { |
| 37 |
abort("Problem encountered while parsing template.", parent = cnd, call = call) |
| 38 |
}, |
| 39 |
jinjar_render_error = function(cnd) { |
| 40 |
abort("Problem encountered while rendering template.", parent = cnd, call = call) |
| 41 |
}, |
| 42 |
jinjar_json_error = function(cnd) { |
| 43 |
# use .internal because JSON was poorly encoded by jinjar |
| 44 |
abort("Problem encountered while decoding JSON data.", parent = cnd, call = call, .internal = TRUE) |
| 45 |
} |
| 46 |
) |
| 47 |
} |