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

Improve error messages with cli #560

Closed
wants to merge 4 commits into from
Closed

Conversation

olivroy
Copy link
Contributor

@olivroy olivroy commented Sep 10, 2023

Description

This is just a little proof of concept of how we can improve messages in janitor, using the power of cli.

It is a little difficult to easily see the diff since we decided against using snapshot tests for errors, but I will add before and after screenshots.

If you think this is a reasonable approach, I will try replicating for other common error messages in janitor.

Related Issue

Addresses #549

Example

with this PR

convert_to_date(LETTERS)

image

Before:
image

@codecov
Copy link

codecov bot commented Sep 10, 2023

Codecov Report

Merging #560 (579290a) into main (bb23615) will not change coverage.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #560   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           26        26           
  Lines         1184      1179    -5     
=========================================
- Hits          1184      1179    -5     
Files Changed Coverage Δ
R/convert_to_date.R 100.00% <100.00%> (ø)

Copy link
Contributor Author

@olivroy olivroy left a comment

Choose a reason for hiding this comment

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

Precisions on what I did.

@@ -111,7 +111,7 @@ convert_to_datetime_helper.Date <- function(x, ..., tz = "UTC", out_class = c("P
ret
}

convert_to_datetime_helper.character <- function(x, ..., tz = "UTC", character_fun = lubridate::ymd_hms, string_conversion_failure = c("error", "warning"), out_class = c("POSIXct", "Date")) {
convert_to_datetime_helper.character <- function(x, ..., tz = "UTC", character_fun = lubridate::ymd_hms, string_conversion_failure = c("error", "warning"), out_class = c("POSIXct", "Date"), error_call = rlang::caller_env()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to ensure that it print Error in convert_date() / convert_datetime() instead of `Error in convert_to_datetime_helper

Comment on lines +150 to +152
c(
"Not all character strings converted to class {.cls {out_class}}.",
i = "{n_not_converted} value{?s} not converted: {.val {not_converted_values}}"
Copy link
Contributor Author

@olivroy olivroy Sep 10, 2023

Choose a reason for hiding this comment

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

Using cli pluralization + inline markup https://cli.r-lib.org/index.html#pluralization https://cli.r-lib.org/reference/inline-markup.html. Also cli automatically makes the display of values shorter, hence the removal of the code above that prepares printing of the diagnostic.

expect_warning(
expect_error(
convert_to_date(LETTERS),
regexp = "Not all character strings converted to class Date."
),
regexp = "All formats failed to parse." # lubridate warning
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

duplicated test.

@olivroy olivroy mentioned this pull request Sep 6, 2023
9 tasks
@olivroy
Copy link
Contributor Author

olivroy commented Nov 3, 2023

Maybe later! the task is large and may not be worth the trouble currently.

@olivroy olivroy closed this Nov 3, 2023
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.

1 participant