Skip to content

Standardize vec_*() error messages with check_vector_valid() #1662

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

Merged
merged 7 commits into from
May 14, 2024

Conversation

olivroy
Copy link
Collaborator

@olivroy olivroy commented May 13, 2024

Summary

This standardizes error messages for vec_*() and is inspired by the tidyverse style guide.

## before
vec_fmt_datetime(1)
#> Error in `vec_fmt_datetime()
#> ! "The `vec_fmt_datetime()` function can only be used with Date, POSIXct, or character vectors."

## now (less repetition)
vec_fmt_datetime(1)
#> Error in `vec_fmt_datetime()`
#> `x` must be a Date, POSIXct or, character vectors, not the number 1.

It gets rid of two internal functions stop_if_not_vector() and vector_class_is_valid() and consolidates them into one: check_vector_valid(), which does a mix of both.

  • stops if x is not vector is valid_classes is not present
  • stops if x is not vector or x doesn't inherit valid_classes

Overview of check_vector_valid()

pkgload::load_all()
#> ℹ Loading gt
# like stop_if_not_vector()
check_vector_valid(1)
# works
check_vector_valid(1, c("numeric", "integer"))
check_vector_valid(Sys.Date(), c("Date"))
check_vector_valid(Sys.Date(), "integer")
#> Error:
#> ! `x` must be integer vectors, not a <Date> object.
check_vector_valid(rep(Sys.Date(), length.out = 100), c("integer", "character"))
#> Error:
#> ! `x` must be integer or character vectors, not a <Date> object.

Created on 2024-05-13 with reprex v2.1.0

Checklist

@olivroy olivroy changed the title Standardize vec_*() error messages. Standardize vec_*() error messages with check_vector_valid() May 14, 2024
@rich-iannone rich-iannone self-requested a review May 14, 2024 14:52
Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@rich-iannone
Copy link
Member

Thanks again!

@rich-iannone rich-iannone merged commit 311f7ae into rstudio:master May 14, 2024
12 checks passed
@olivroy olivroy deleted the vec-error branch July 16, 2024 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants