mirror of
https://github.com/glitch-soc/mastodon.git
synced 2026-03-29 03:00:33 +02:00
Extract ErrorResponses from application controller (#38105)
This commit is contained in:
@@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
|
||||
include UserTrackingConcern
|
||||
include SessionTrackingConcern
|
||||
include CacheConcern
|
||||
include ErrorResponses
|
||||
include PreloadingConcern
|
||||
include DomainControlHelper
|
||||
include DatabaseHelper
|
||||
@@ -23,21 +24,6 @@ class ApplicationController < ActionController::Base
|
||||
helper_method :limited_federation_mode?
|
||||
helper_method :skip_csrf_meta_tags?
|
||||
|
||||
rescue_from ActionController::ParameterMissing, Paperclip::AdapterRegistry::NoHandlerError, with: :bad_request
|
||||
rescue_from Mastodon::NotPermittedError, with: :forbidden
|
||||
rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, with: :not_found
|
||||
rescue_from ActionController::UnknownFormat, with: :not_acceptable
|
||||
rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_content
|
||||
rescue_from Mastodon::RateLimitExceededError, with: :too_many_requests
|
||||
|
||||
rescue_from(*Mastodon::HTTP_CONNECTION_ERRORS, with: :internal_server_error)
|
||||
rescue_from Mastodon::RaceConditionError, Stoplight::Error::RedLight, ActiveRecord::SerializationFailure, with: :service_unavailable
|
||||
|
||||
rescue_from Seahorse::Client::NetworkingError do |e|
|
||||
Rails.logger.warn "Storage server error: #{e}"
|
||||
service_unavailable
|
||||
end
|
||||
|
||||
before_action :check_self_destruct!
|
||||
|
||||
before_action :store_referrer, except: :raise_not_found, if: :devise_controller?
|
||||
@@ -118,42 +104,6 @@ class ApplicationController < ActionController::Base
|
||||
ActiveModel::Type::Boolean.new.cast(params[key])
|
||||
end
|
||||
|
||||
def forbidden
|
||||
respond_with_error(403)
|
||||
end
|
||||
|
||||
def not_found
|
||||
respond_with_error(404)
|
||||
end
|
||||
|
||||
def gone
|
||||
respond_with_error(410)
|
||||
end
|
||||
|
||||
def unprocessable_content
|
||||
respond_with_error(422)
|
||||
end
|
||||
|
||||
def not_acceptable
|
||||
respond_with_error(406)
|
||||
end
|
||||
|
||||
def bad_request
|
||||
respond_with_error(400)
|
||||
end
|
||||
|
||||
def internal_server_error
|
||||
respond_with_error(500)
|
||||
end
|
||||
|
||||
def service_unavailable
|
||||
respond_with_error(503)
|
||||
end
|
||||
|
||||
def too_many_requests
|
||||
respond_with_error(429)
|
||||
end
|
||||
|
||||
def single_user_mode?
|
||||
@single_user_mode ||= Rails.configuration.x.single_user_mode && Account.without_internal.exists?
|
||||
end
|
||||
@@ -178,13 +128,6 @@ class ApplicationController < ActionController::Base
|
||||
@current_session = SessionActivation.find_by(session_id: cookies.signed['_session_id']) if cookies.signed['_session_id'].present?
|
||||
end
|
||||
|
||||
def respond_with_error(code)
|
||||
respond_to do |format|
|
||||
format.any { render "errors/#{code}", layout: 'error', status: code, formats: [:html] }
|
||||
format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code }
|
||||
end
|
||||
end
|
||||
|
||||
def check_self_destruct!
|
||||
return unless self_destruct?
|
||||
|
||||
|
||||
68
app/controllers/concerns/error_responses.rb
Normal file
68
app/controllers/concerns/error_responses.rb
Normal file
@@ -0,0 +1,68 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ErrorResponses
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_content
|
||||
rescue_from ActionController::ParameterMissing, Paperclip::AdapterRegistry::NoHandlerError, with: :bad_request
|
||||
rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, with: :not_found
|
||||
rescue_from ActionController::UnknownFormat, with: :not_acceptable
|
||||
rescue_from Mastodon::NotPermittedError, with: :forbidden
|
||||
rescue_from Mastodon::RaceConditionError, Stoplight::Error::RedLight, ActiveRecord::SerializationFailure, with: :service_unavailable
|
||||
rescue_from Mastodon::RateLimitExceededError, with: :too_many_requests
|
||||
rescue_from(*Mastodon::HTTP_CONNECTION_ERRORS, with: :internal_server_error)
|
||||
|
||||
rescue_from Seahorse::Client::NetworkingError do |e|
|
||||
Rails.logger.warn "Storage server error: #{e}"
|
||||
service_unavailable
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def bad_request
|
||||
respond_with_error(400)
|
||||
end
|
||||
|
||||
def forbidden
|
||||
respond_with_error(403)
|
||||
end
|
||||
|
||||
def gone
|
||||
respond_with_error(410)
|
||||
end
|
||||
|
||||
def internal_server_error
|
||||
respond_with_error(500)
|
||||
end
|
||||
|
||||
def not_acceptable
|
||||
respond_with_error(406)
|
||||
end
|
||||
|
||||
def not_found
|
||||
respond_with_error(404)
|
||||
end
|
||||
|
||||
def service_unavailable
|
||||
respond_with_error(503)
|
||||
end
|
||||
|
||||
def too_many_requests
|
||||
respond_with_error(429)
|
||||
end
|
||||
|
||||
def unprocessable_content
|
||||
respond_with_error(422)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def respond_with_error(code)
|
||||
respond_to do |format|
|
||||
format.any { render "errors/#{code}", layout: 'error', formats: [:html], status: code }
|
||||
format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code }
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user