ruby-on-railsrubyview-components

Errors are being swallowed inside ViewComponent, Ruby on Rails


Hoping you're having a good day. So, i have this weird bug that's been kicking my ass and i'm not sure where it is located for the love of God.

I have a ViewComponent that is rendered normally inside a view, i.e *.html.erb. For example,

 <%= render Contact::Avatar.new(
      contact:,
      size: :xl,
    ) %>

Everything is alright, the component is rendered successfully without issues. The problem arises when the component has an error, any errors raised within the component when passing it to the render function is swalloed, and instead of raising the error as it should, the error is swallowed and an empty string is returned, which prevents me from knowing what's wrong. For example, if the component view references an undefined variable, inside contact/avatar_component.html.erb i'm raising an ArgumentError just for the sake of testing

<% raise ArgumentError %>

<% if u/contact.draft? %>
  <%= render AvatarComponent.new @contact, color: Color.indigo, **@options %>
<% else %>
  <% if @contact.name.strip.blank? || @contact.discarded? %>
    <%= render AvatarComponent.new @contact, **@options %>
  <% else %>
    <%= render AvatarComponent.new @contact, text: @contact.abbreviated_name, **@options %>
  <% end %>
<% end %>

And the error is captured by the render function.

However, if the error is raised in the Ruby file, i.e

class Contact::Avatar < ViewComponent::Base
  def initialize(contact:, **options)
    @contact = contact
    @options = options
    raise ArgumentError
  end
end

the error is captured and presented

enter image description here


Solution

  • Couldn't reproduce it.

    1. Check if you experience the same problem on a blank ROR project with ViewComponent.
    2. Determine guilty lib.

    Some libs may override things:

    https://github.com/ViewComponent/view_component/issues/1981