Browse Source

fix monkey_patches

main
pvincent 3 months ago
parent
commit
2190d23ca9
  1. 15
      config/initializers/monkey_patches.rb
  2. 1
      config/initializers/semantic_logger.rb
  3. 51
      lib/monkey_patches/action_dispatch/middleware/debug_exceptions.rb
  4. 17
      lib/monkey_patches/monkey_patcher.rb
  5. 2
      lib/monkey_patches/rails_live_reload/watcher.rb

15
config/initializers/monkey_patches.rb

@ -1,12 +1,19 @@
return unless Rails.application.server? || Rails.application.console? return unless Rails.application.server? || Rails.application.console?
require_relative '../../lib/monkey_patches/monkey_patcher'
MonkeyPatcher.run do |patch|
def grant(patch)
case patch case patch
when 'action_dispatch/middleware/debug_exceptions.rb' then Rails.application.server? when 'action_dispatch/middleware/debug_exceptions.rb' then Rails.application.server?
when 'rails_live_reload/watcher.rb' then Rails.application.server? && Rails.env.development? when 'rails_live_reload/watcher.rb' then Rails.application.server? && Rails.env.development?
when /^semantic/ then true when /^semantic/ then true
else false
end end
end end
puts 'MonkeyPatcher runs:'
patches = Dir.glob(Rails.root.join('lib', 'monkey_patches', '**', '*.rb'))
patches.each do |file|
patch = Pathname.new(file).relative_path_from(Rails.root.join('lib', 'monkey_patches')).to_s
next unless grant patch
puts "🐵 patching... #{patch}"
require file
end

1
config/initializers/semantic_logger.rb

@ -9,6 +9,7 @@ Rails.autoloaders.main.on_load('ApplicationController') do
end end
Rails.configuration.after_initialize do Rails.configuration.after_initialize do
Rails.logger.name = 'error_trap'
Semantic::InstrumentationManager.clear Semantic::InstrumentationManager.clear
bootstrap = [{ kind: :restored, constant: 'ACTION_VIEW', type: :boolean, old_value: false, new_value: true }, bootstrap = [{ kind: :restored, constant: 'ACTION_VIEW', type: :boolean, old_value: false, new_value: true },

51
lib/monkey_patches/action_dispatch/middleware/debug_exceptions.rb

@ -3,35 +3,34 @@ require 'action_dispatch/routing/inspector'
require 'action_view' require 'action_view'
module ActionDispatch module ActionDispatch
module Middleware
# THIS IS A HACK TO unwrap TagWrapError when detected!
# TagWrapError contains a 'tag' for SemanticLogger and the cause itself
class DebugExceptions
private
# THIS IS A HACK TO unwrap TagWrapError when detected!
# TagWrapError contains a 'tag' for SemanticLogger and the cause itself
class DebugExceptions
private
def render_exception(request, exception, wrapper)
## PVINCENT's ADDITION for TagWrapError
log_error(request, wrapper)
if exception.is_a?(Semantic::TagWrapError)
backtrace_cleaner = request.get_header('action_dispatch.backtrace_cleaner')
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception.error)
end
## HERE it was commented off
# log_error(request, wrapper)
## END OF ADDITION
def render_exception(request, exception, wrapper)
## PVINCENT's ADDITION for TagWrapError
raise exception unless request.get_header('action_dispatch.show_detailed_exceptions')
log_error(request, wrapper)
if exception.is_a?(Semantic::TagWrapError)
backtrace_cleaner = request.get_header('action_dispatch.backtrace_cleaner')
wrapper = ExceptionWrapper.new(backtrace_cleaner, exception.error)
end
## HERE it was commented off
# log_error(request, wrapper)
## END OF ADDITION
raise exception unless request.get_header('action_dispatch.show_detailed_exceptions')
begin
content_type = request.formats.first
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
content_type = Mime[:text]
end
if api_request?(content_type)
render_for_api_request(content_type, wrapper)
else
render_for_browser_request(request, wrapper)
end
begin
content_type = request.formats.first
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
content_type = Mime[:text]
end
if api_request?(content_type)
render_for_api_request(content_type, wrapper)
else
render_for_browser_request(request, wrapper)
end end
end end
end end

17
lib/monkey_patches/monkey_patcher.rb

@ -1,17 +0,0 @@
class MonkeyPatcher
class << self
MONKEY_PATCHES = Rails.root.join('lib', 'monkey_patches')
def run(&)
patches = Dir.glob(MONKEY_PATCHES.join('**', '*.rb'))
patches.each do |patch|
file = Pathname.new(patch).relative_path_from(MONKEY_PATCHES).to_s
next if file == 'monkey_patcher.rb' # filter off its own file!
if !block_given? || yield(file)
puts "🐵 patching... #{file}"
require file
end
end
end
end
end

2
lib/monkey_patches/rails_live_reload/watcher.rb

@ -5,7 +5,7 @@ module RailsLiveReload
@files = {} @files = {}
@sockets = [] @sockets = []
# puts "Watching: #{root}"
# puts "Watching AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA: #{root}"
# RailsLiveReload.patterns.each do |pattern, rule| # RailsLiveReload.patterns.each do |pattern, rule|
# puts " #{pattern} => #{rule}" # puts " #{pattern} => #{rule}"
# end # end

Loading…
Cancel
Save