vendredi 3 novembre 2017

Rails 'partial not found' masks underlying error

I encountered a sporadic error, ostensibly because Rails sometimes could not find a partial. However, because of the way the error only showed up in certain cases, I suspected, and then found, a different error underlying it.

My question is whether anyone knows of a reliable way to identify the underlying error in a case like this?

The apparent error:

Rails gave this error when responding to a request for a JSON view new.json.erb:

ActionView::Template::Error (Missing partial insightly_contacts/search_result with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/path/to/project/app/views"

This was odd for two reasons:

  1. The partial in question is present, and renders happily most of the time.
  2. The partial in question is not of format :json; although new.json.erb is a JSON template obviously, it renders the partial in question like so (specifying :html format):

    form_object.possible_insightly_contacts.map { |insightly_contact|
      {
        id: insightly_contact.id,
        html: render(partial: 'insightly_contacts/search_result', locals: { insightly_contact: insightly_contact }, formats: :html)
      }
    }.to_json.html_safe
    
    

Notes:

  • possible_insightly_contacts returns an ActiveRelation.

  • The error only showed up when the form_object was given certain parameters. Basically it takes a search string, which possible_insightly_contacts uses to build a query; a search on 'x' works, while a search on 'z' gives the missing partial error (these are not placeholders, literally the strings 'x' and 'z' worked/failed respectively!)

  • I inspected both the form_object and the results of possible_insightly_contacts, and there were no errors – the request wasn't failing because of an empty set from possible_insightly_contacts, or something unexpected being returned.

The underlying error:

(Note: in a sense this is irrelevant, the point is just that it wasn't really a 'partial not found' error)

The underlying error was that rendering of another partial further down the rendering hierarchy was failing, because of a logic error which only occurred in certain insightly_contact objects. Hence the error only occurred for certain search string values: they returned an object which triggered an error because of some specific data it held.

My question:

How should I have found the underlying error? In the end I just went through all template / partial / model code that could conceivably be called from the template above, and guessed what might be wrong.

I would expect a regular HTML view which triggered a similar bug in an underlying model to give a proper backtrace, and find the issue that way.

But neither the error reported in the server console, nor the backtrace provided, bore any relation to the real issue here.

Has anyone encountered / solved a similar problem?

Is there a canonical approach to this?

Aucun commentaire:

Enregistrer un commentaire