Skip to content

Commit

Permalink
Merge pull request #10002 from vpereira/webui_rescues_concerns
Browse files Browse the repository at this point in the history
Move Webui::WebuiController rescue_from blocks to a concern
  • Loading branch information
vpereira committed Aug 11, 2020
2 parents 1771423 + 0c9bd7c commit ff98a74
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/api/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Metrics/BlockLength:
- '**/*.rake'
# FIXME: Since exclusions in `.rubocop_todo.yml` are simply ignored when we also exclude files here, the following exclusions are TODOs
- 'app/controllers/concerns/rescue_handler.rb'
- 'app/controllers/concerns/webui/rescue_handler.rb'
- 'app/models/binary_release.rb'
- 'app/models/branch_package.rb'
- 'app/models/bs_request.rb'
Expand Down
56 changes: 56 additions & 0 deletions src/api/app/controllers/concerns/webui/rescue_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Webui::RescueHandler
extend ActiveSupport::Concern

included do
rescue_from Pundit::NotAuthorizedError do |exception|
pundit_action = case exception.try(:query).to_s
when 'index?' then 'list'
when 'show?' then 'view'
when 'create?' then 'create'
when 'new?' then 'create'
when 'update?' then 'update'
when 'edit?' then 'edit'
when 'destroy?' then 'delete'
when 'create_branch?' then 'create_branch'
else exception.try(:query)
end
message = if pundit_action && exception.record
"Sorry, you are not authorized to #{pundit_action} this #{exception.record.class}."
else
'Sorry, you are not authorized to perform this action.'
end
if request.xhr?
render json: { error: message }, status: 400
else
flash[:error] = message
redirect_back(fallback_location: root_path)
end
end

rescue_from Backend::Error, Timeout::Error do |exception|
Airbrake.notify(exception)
message = case exception
when Backend::Error
'There has been an internal error. Please try again.'
when Timeout::Error
'The request timed out. Please try again.'
end

if request.xhr?
render json: { error: message }, status: 400
else
flash[:error] = message
redirect_back(fallback_location: root_path)
end
end

# FIXME: just because there is some data missing to compute the request?
# Please check:
# http://guides.rubyonrails.org/active_record_validations.html
class MissingParameterError < RuntimeError; end
rescue_from MissingParameterError do |exception|
logger.debug "#{exception.class.name} #{exception.message} #{exception.backtrace.join('\n')}"
render file: Rails.root.join('public/404'), status: 404, layout: false, formats: [:html]
end
end
end
52 changes: 1 addition & 51 deletions src/api/app/controllers/webui/webui_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Webui::WebuiController < ActionController::Base

include Pundit
include FlipperFeature
include Webui::RescueHandler
protect_from_forgery

before_action :set_influxdb_data
Expand All @@ -24,57 +25,6 @@ class Webui::WebuiController < ActionController::Base
# :notice and :alert are default, we add :success and :error
add_flash_types :success, :error

rescue_from Pundit::NotAuthorizedError do |exception|
pundit_action = case exception.try(:query).to_s
when 'index?' then 'list'
when 'show?' then 'view'
when 'create?' then 'create'
when 'new?' then 'create'
when 'update?' then 'update'
when 'edit?' then 'edit'
when 'destroy?' then 'delete'
when 'create_branch?' then 'create_branch'
else exception.try(:query)
end
if pundit_action && exception.record
message = "Sorry, you are not authorized to #{pundit_action} this #{exception.record.class}."
else
message = 'Sorry, you are not authorized to perform this action.'
end
if request.xhr?
render json: { error: message }, status: 400
else
flash[:error] = message
redirect_back(fallback_location: root_path)
end
end

rescue_from Backend::Error, Timeout::Error do |exception|
Airbrake.notify(exception)
message = case exception
when Backend::Error
'There has been an internal error. Please try again.'
when Timeout::Error
'The request timed out. Please try again.'
end

if request.xhr?
render json: { error: message }, status: 400
else
flash[:error] = message
redirect_back(fallback_location: root_path)
end
end

# FIXME: This is more than stupid. Why do we tell the user that something isn't found
# just because there is some data missing to compute the request? Someone needs to read
# http://guides.rubyonrails.org/active_record_validations.html
class MissingParameterError < RuntimeError; end
rescue_from MissingParameterError do |exception|
logger.debug "#{exception.class.name} #{exception.message} #{exception.backtrace.join('\n')}"
render file: Rails.root.join('public/404'), status: 404, layout: false, formats: [:html]
end

def valid_xml_id(rawid)
rawid = "_#{rawid}" if rawid !~ /^[A-Za-z_]/ # xs:ID elements have to start with character or '_'
CGI.escapeHTML(rawid.gsub(/[+&: .\/~()@#]/, '_'))
Expand Down

0 comments on commit ff98a74

Please sign in to comment.