rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 68 forks source link

Better Error Assertions and Messages for `tags` #284

Open lz100 opened 3 years ago

lz100 commented 3 years ago

If I do htmltools::tags$p(list(1:2))

Error in writeImpl(text) : 
  Text to be written must be a length-one character vector
  1. I understand, because I have a list, but the error is coming from this writeImpl function, not the tags$p function. Imagine we are writing deep levels of UI, giving this error is very hard to debug. I have no idea where it comes from.

  2. It doesn't give you the error right away, let's say we are writing a Shiny app

    ui <- fluidPage(
    htmltools::tags$p(list(1:2))
    )

    This actually works when you assign it. It error only happens when the UI gets evaluated somehow later. This creates the false impression that my UI has no problem, but it has problems.

  3. If I use htmltools::tags$p(list(1)), this will work with no problem, but it is not a character and the item inside the list is not character either, so the error message is misleading. People will ask why do my list and non-character value work here?

Hope we can have some better error messages and better assertions before value assignment.

hadley commented 3 years ago

Yeah, way too much error checking appears to happen at print time, not at construction time. This makes errors hard to track down.

Here's a small reprex:

x <- htmltools::p(mean)
x
#> Error in as.character(x): cannot coerce type 'closure' to vector of type 'character'

x <- htmltools::p(classes = letters, "x")
x
#> Error in if (!is.na(attribValue)) {: the condition has length > 1

Created on 2021-08-11 by the reprex package (v2.0.0)