Skip to content

Commit

Permalink
Fixes #26758 - unify renderer_error helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Aug 20, 2019
1 parent 19fd5af commit 2b306bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
Expand Up @@ -9,26 +9,24 @@ def render_template(template:, type:)
return safe_render(template) if template

error_message = N_("unable to find %{type} template for %{host} running %{os}")
render_error(:not_found, error_message, {type: type, host: @host.name, os: @host.operatingsystem})
render_error(error_message, type: type, host: @host.name, os: @host.operatingsystem, status: :not_found)
end

def safe_render(template)
render plain: template.render(host: @host, params: params).html_safe
rescue StandardError => error
Foreman::Logging.exception("Error rendering the #{template.name} template", error)
render_error(
:message => 'There was an error rendering the %{name} template: %{error}',
render_error('There was an error rendering the %{name} template: %{error}',
:name => template.name,
:error => error.message,
:status => :internal_server_error
)
end

def render_error(options)
message = options.delete(:message)
def render_error(message, options)
status = options.delete(:status) || :not_found
logger.error message % options
render plain: "#{message % options}\n", :status => status
logger.error(message % options)
render(plain: "#{message % options}\n", :status => status)
end
end
end
Expand Down
15 changes: 7 additions & 8 deletions app/controllers/unattended_controller.rb
Expand Up @@ -80,12 +80,12 @@ def preview?
params.key?(:spoof) || params.key?(:hostname)
end

def render_error(status, error_message, params)
logger.error error_message % params
message = _(error_message) % params
return render_ipxe_message(message: message, status: status) if ipxe_request?
# add a comment character (works with Red Hat and Debian systems) to avoid parsing errors
render(:plain => "# #{message}", :status => status, :content_type => 'text/plain')
def render_error(message, options)
if ipxe_request?
render_ipxe_message(message: message, status: options[:status] || :not_found)
else
super
end
end

def render_intermediate_template
Expand Down Expand Up @@ -171,8 +171,7 @@ def verify_found_host
end

error = host_verifier.errors.first
render_error(error[:type], error[:message], error[:params])

render_error(error[:message], { :status => error[:type] }.merge(error[:params]))
false
end

Expand Down
3 changes: 1 addition & 2 deletions app/controllers/userdata_controller.rb
Expand Up @@ -65,8 +65,7 @@ def find_host
@host = Foreman::UnattendedInstallation::HostFinder.new(query_params: query_params).search

return true if @host
render_error(
:message => 'Could not find host for request %{request_ip}',
render_error('Could not find host for request %{request_ip}',
:status => :not_found,
:request_ip => ip_from_request_env
)
Expand Down

0 comments on commit 2b306bf

Please sign in to comment.