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

fix #1487: better error handling for insertUI when selector does not match anything in DOM #1488

Merged
merged 1 commit into from Dec 7, 2016

Conversation

bborgesr
Copy link
Contributor

@bborgesr bborgesr commented Nov 28, 2016

This makes the JS error more descriptive. (Still no error is printed to the R console or any indication appears on app that something went wrong).

Example app (notice that I use the wrong selector #add1, instead of #add):

library(shiny)
options(shiny.launch.browser=F, shiny.minified=F, shiny.port = 9000)

## Only run this example in interactive R sessions
if (interactive()) {
  # Define UI
  ui <- fluidPage(
    actionButton("add", "Add UI")
  )
  
  # Server logic
  server <- function(input, output, session) {
    observeEvent(input$add, {
      insertUI(
        selector = "#add1",
        where = "afterEnd",
        ui = textInput(paste0("txt", input$add),
                       "Insert some text")
      )
    })
  }
  
  # Complete app with UI and server components
  shinyApp(ui, server)
}

cc @wch

try {
exports.renderHtml($([]), message.content.html, message.content.deps);
} catch(err) {
throw "The selector you chose could not be found in the DOM.";
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I understand correctly, if targets.length is 0, it will always reach this throw statement. This is because in line 192 of output_binding_html.js, it calls val.replace(self._reSingleton, findNewPayload), but val is [], so there's no replace method.

If I'm right about that, then this code path (where targets.length===0) never worked, and that should be fixed. That should probably be done first. When would it be desirable to render this stuff when there are no targets?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah it looks like this call has transposed arguments, should be:

exports.renderHtml(message.content.html, $([]), message.content.deps)

@jcheng5
Copy link
Member

jcheng5 commented Nov 28, 2016

I don't think we should throw, rather just log a warning. The dispatchMessage mechanism isn't designed for message handlers to throw exceptions (though maybe/probably it should be) and anyway this isn't that crazy of a thing to do, especially with multiple = TRUE.

@bborgesr
Copy link
Contributor Author

@jcheng5, just console.log, then?

@bborgesr bborgesr force-pushed the barbara/insert branch 4 times, most recently from 4c35a7b to e092cab Compare December 7, 2016 04:58
@bborgesr bborgesr merged commit a63d798 into master Dec 7, 2016
@bborgesr bborgesr deleted the barbara/insert branch December 7, 2016 05:02
@bborgesr bborgesr removed the review label Dec 7, 2016
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.

None yet

3 participants