r-lib / err

Ideas for error handling tools
1 stars 2 forks source link

Abort helpers that match style guidelines #1

Open hadley opened 6 years ago

hadley commented 6 years ago

e.g.

abort <- function(.msg, .type = NULL, ...) {
  cnd <- error_cnd(.type = .type, ..., .msg = .msg)
  stop(cnd)
}

abort_bad_argument <- function(arg, must, not = NULL) {
  msg <- glue::glue("`{arg}` must {must}")
  if (!is.null(not)) {
    msg <- glue::glue("{msg}; not {not}")
  }
  abort(msg, "error_bad_argument", arg = arg)
}

abort_bad_argument("x", must = "be numeric")
abort_bad_argument("x", must = "be numeric", not = "logical")

catch_cnd(abort_bad_argument("x", must = "be numeric"))$arg

We might want one even more specifically about types.

DavisVaughan commented 6 years ago

Just wanted to throw in a 👍 for these. I just had to write up somewhat similar functions here. glue+crayon+stop() made for a great combo but I would love some standard helpers.