app/controllers/lookbook/previews_controller.rb in lookbook-0.7.2.beta.0 vs app/controllers/lookbook/previews_controller.rb in lookbook-0.7.2.beta.1
- old
+ new
@@ -11,11 +11,14 @@
if @example
set_params
begin
render html: render_examples(examples_data)
rescue => exception
- render_in_layout "lookbook/error", layout: "lookbook/basic", error: exception, disable_header: true
+ render_in_layout "lookbook/error",
+ layout: "lookbook/basic",
+ error: prettify_error(exception),
+ disable_header: true
end
else
render_in_layout "not_found"
end
end
@@ -26,11 +29,11 @@
set_params
@examples = examples_data
@drawer_panels = drawer_panels.filter { |name, panel| panel[:show] }
@preview_panels = preview_panels.filter { |name, panel| panel[:show] }
rescue => exception
- render_in_layout "lookbook/error", error: exception
+ render_in_layout "lookbook/error", error: prettify_error(exception)
end
else
render_in_layout "not_found"
end
end
@@ -149,8 +152,31 @@
@preview_controller ||= controller
end
def render_in_layout(path, layout: nil, **locals)
render path, layout: layout.presence || (params[:lookbook_embed] ? "lookbook/basic" : "lookbook/application"), locals: locals
+ end
+
+ def prettify_error(exception)
+ error_params = if exception.is_a?(ViewComponent::PreviewTemplateError)
+ {
+ file_path: @preview&.full_path,
+ line_number: 0,
+ source_code: @example&.source
+ }
+ elsif exception.is_a?(ActionView::Template::Error) & exception.message.include?("implements a reserved method")
+ message_parts = exception.message.split("\n").first.split
+ component_class = message_parts.first.constantize
+ naughty_method = message_parts.last.delete("#").delete("`").delete(".")
+ p naughty_method
+ method = component_class.instance_method(naughty_method.to_sym)
+ if method
+ {
+ file_path: method.source_location.first,
+ line_number: method.source_location[1]
+ }
+ end
+ end
+ Lookbook::Error.new(exception, error_params || {})
end
end
end