add better_errors customizer

arnaud sellenet committed Apr 14, 2014
commit b45c626719ccccefeab7f0c028a0766a8a5ca12c
Showing 1 changed file with 72 additions and 0 deletions
locomotive/wagon/initializers/better_errors.rb b/lib/locomotive/wagon/initializers/better_errors.rb +72 -0
@@ @@ -0,0 +1,72 @@
+ BetterErrors.application_root = reader.mounting_point.path
+ require 'better_errors'
+ require 'ostruct'
+
+ module BetterErrors
+ class MiddlewareWrapper
+
+ def initialize(app)
+ @@middleware ||= BetterErrors::Middleware.new(app)
+ @@middleware.instance_variable_set(:@app, app)
+ end
+
+ def call(env)
+ env['action_dispatch.request.parameters'] = Rack::Request.new(env).params
+
+ @@middleware.call(env)
+ end
+
+ end
+
+ module FrameWithLiquidContext
+
+ extend ActiveSupport::Concern
+
+ included do
+
+ attr_accessor :liquid_context
+
+ alias_method_chain :local_variables, :liquid_context
+
+ class << self
+
+ alias_method_chain :from_exception, :liquid_context
+
+ end
+ end
+
+ def local_variables_with_liquid_context
+ if self.liquid_context
+ scope = self.liquid_context.scopes.last.clone
+
+ scope.delete_if { |k, _| %w(models contents params session).include?(k) }.tap do |_scope|
+ _scope['site'] = _scope['site'].send(:_source).to_hash
+ _scope['page'] = _scope['page'].to_hash.delete_if { |k, _| %w(template).include?(k) }
+ end
+ else
+ self.local_variables_without_liquid_context
+ end
+ rescue Exception => e
+ puts "[BetterError] Fatal error: #{e.message}".red
+ puts e.backtrace.join("\n")
+ {}
+ end
+
+ module ClassMethods
+
+ def from_exception_with_liquid_context(exception)
+ from_exception_without_liquid_context(exception).tap do |list|
+ if exception.respond_to?(:liquid_context)
+ list.first.liquid_context = exception.liquid_context
+ end
+ end
+ end
+
+ end
+ end
+
+ class StackFrame
+ include FrameWithLiquidContext
+ end
+
+ end
\ No newline at end of file