blueprinter icon indicating copy to clipboard operation
blueprinter copied to clipboard

Better errors

Open wollistik opened this issue 4 years ago • 0 comments

I came across several issues when writing Blueprinter classes.

When there are issues in the Blueprint, the error message are not very expressive and debugging is hard. See following example:

class ErrorBlueprint < Blueprinter::Base
  view :with_invalid_field do
    field :no_such_method
  end

  view :with_invalid_included_view do
    include_view :not_existing
  end
end

When calling

ErrorBlueprint.render(Object.new, view: :with_invalid_field)

you get a

NoMethodError: undefined method `no_such_method' for #Object:0x00007fc665c0d1a8

The second case is even worse:

NoMethodError: undefined method `fields' for nil:NilClass

I opened a PR for this #254

Then a BlueprinterError is raised with more expressive messages:

Blueprinter::BlueprinterError: The included view not_existing does not exist - Called from view with_invalid_included_view from /home/wwedelich/git/blueprinter/lib/blueprinter/view_collection.rb:55:in `block in sortable_fields'

And

Blueprinter::BlueprinterError: Error when extracting value. Blueprint: 'ErrorBlueprint'; View: 'with_invalid_field'; Field: 'no_such_method' from /home/wwedelich/git/blueprinter/lib/blueprinter/helpers/base_helpers.rb:51:in rescue in block in object_to_hash' Caused by NoMethodError: undefined method no_such_method' for #Object:0x00005576c088f270

wollistik avatar Jan 21 '21 14:01 wollistik