From 1d073a7ae973121ac892b4e6ee72f8a6d296da16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Thu, 26 Sep 2024 11:11:38 +0200 Subject: [PATCH] Remove instance variables in error helper and lint --- app/helpers/errors_helper.rb | 51 +++++++++++++++------------------ app/views/common/error.html.erb | 2 +- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/app/helpers/errors_helper.rb b/app/helpers/errors_helper.rb index a41b5fb2101d..507828b3fc99 100644 --- a/app/helpers/errors_helper.rb +++ b/app/helpers/errors_helper.rb @@ -27,34 +27,34 @@ #++ module ErrorsHelper - def render_400(options = {}) - @project = nil + def render_400(options = {}) # rubocop:disable Naming/VariableNumber + unset_template_magic render_error({ message: :notice_bad_request, status: 400 }.merge(options)) false end - def render_403(options = {}) - @project = nil + def render_403(options = {}) # rubocop:disable Naming/VariableNumber + unset_template_magic render_error({ message: :notice_not_authorized, status: 403 }.merge(options)) false end - def render_404(options = {}) + def render_404(options = {}) # rubocop:disable Naming/VariableNumber render_error({ message: :notice_file_not_found, status: 404 }.merge(options)) false end - def render_500(options = {}) - message = t(:notice_internal_server_error, app_title: Setting.app_title) - + def render_500(options = {}) # rubocop:disable Naming/VariableNumber unset_template_magic + message = t(:notice_internal_server_error, app_title: Setting.app_title) + # Append error information if current_user.admin? options[:message_details] = get_additional_message end - render_error({ message: }.merge(options)) + render_error({ message:, status: 500 }.merge(options)) false end @@ -80,39 +80,34 @@ def render_optional_error_file(status_code) end # Renders an error response - def render_error(arg) + def render_error(arg) # rubocop:disable Metrics/AbcSize arg = { message: arg } unless arg.is_a?(Hash) - @status = arg[:status] || 500 - @message = arg[:message] + status = arg[:status] || 500 + message = arg[:message] - if @status >= 500 - op_handle_error(arg[:exception] || "[Error #@status] #@message", payload: arg[:payload]) + if status >= 500 + op_handle_error(arg[:exception] || "[Error #status] #message", payload: arg[:payload]) end - @message = I18n.t(@message) if @message.is_a?(Symbol) - @message_details = arg[:message_details] + message = I18n.t(message) if message.is_a?(Symbol) + message_details = arg[:message_details] respond_to do |format| format.html do - error_message = "[#{I18n.t(:error_code, code: @status)}] #{@message}\n#{@message_details}" + error_message = "[#{I18n.t(:error_code, code: status)}] #{message}\n#{message_details}" flash.now[:op_primer_flash] = { scheme: :danger, message: error_message, dismiss_scheme: :none } - render template: "common/error", layout: use_layout, status: @status + render template: "common/error", + layout: use_layout, + status:, + locals: { status:, params: } end format.any do - head @status + head status end end end def unset_template_magic - if $ERROR_INFO.is_a?(ActionView::ActionViewError) - @template.instance_variable_set(:@project, nil) - @template.instance_variable_set(:@status, 500) - @template.instance_variable_set(:@message, message) - else - @project = nil - end - rescue StandardError - # bad luck + @project = nil # rubocop:disable Rails/HelperInstanceVariable end end diff --git a/app/views/common/error.html.erb b/app/views/common/error.html.erb index 85f38b27a718..ea265ef425a1 100644 --- a/app/views/common/error.html.erb +++ b/app/views/common/error.html.erb @@ -29,4 +29,4 @@ See COPYRIGHT and LICENSE files for more details. <%= call_hook(:view_common_error_details, { params: params, project: ((defined? @project) ? @project : nil) }) %> -<% html_title h(@status) %> +<% html_title h(status) %>