Skip to content

Commit

Permalink
Allow suppressing error log with `SlimRenderer.log_rendering_errors=f…
Browse files Browse the repository at this point in the history
…alse`
  • Loading branch information
UweKubosch committed Apr 8, 2024
1 parent 5ef636c commit 93472a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'no.datek'
version '0.18.2'
version '0.18.3'
final String JRUBY_VERSION = '9.4.5.0';

repositories {
Expand Down
10 changes: 6 additions & 4 deletions src/main/resources/ruby/slim_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ def render(view_path, params = {})
view.render(model_map, request, response)
response.body
rescue Exception => e # rubocop: disable Lint/RescueException
LOG.error "Exception rendering partial template: #{view_path.inspect}"
LOG.error "#{e.class}: #{e.message}"
LOG.error e.to_s
LOG.info e.backtrace.join("\n")
if SlimRenderer.log_rendering_errors
LOG.error "Exception rendering partial template: #{view_path.inspect}"
LOG.error "#{e.class}: #{e.message}"
LOG.error e.to_s
LOG.info e.backtrace.join("\n")
end
raise
end

Expand Down
17 changes: 15 additions & 2 deletions src/main/resources/ruby/slim_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
require_relative 'view_context'
require_relative 'locale_helper'
require_relative 'request_context'
require 'application_setup'

module SlimRenderer
extend LocaleHelper
Expand All @@ -38,6 +37,16 @@ module SlimRenderer
LAYOUT_TEMPLATE_PATH = "/views/layouts"
DEFAULT_LAYOUT = "#{LAYOUT_TEMPLATE_PATH}/layout.slim"

@@log_rendering_errors = true

def self.log_rendering_errors
@@log_rendering_errors
end

def self.log_rendering_errors=(value)
@@log_rendering_errors = value
end

def self.render(template, model_map, rendering_context)
request = RequestContextHolder.request_attributes.request
locale = current_locale(request)
Expand Down Expand Up @@ -98,7 +107,9 @@ def self.render(template, model_map, rendering_context)
#{e.backtrace.join("\n")}
HTML
LOG.error message
if log_rendering_errors
LOG.error message
end
"<h1>Whoops!</h1><pre>#{CGI.escapeHTML(message)}</pre>"
end

Expand Down Expand Up @@ -126,3 +137,5 @@ def [](key)
if (Java::JavaLang::System.getProperty("spring.profiles.active") || Java::JavaLang::System.getenv("SPRING_PROFILES_ACTIVE"))&.include?('development')
require 'source_reloader'
end

require 'application_setup'

0 comments on commit 93472a8

Please sign in to comment.