From e77566a3ddc82de5281aedb905424f8991de915d Mon Sep 17 00:00:00 2001 From: f Date: Sat, 29 Aug 2020 20:21:45 -0300 Subject: [PATCH] =?UTF-8?q?mejorar=20gesti=C3=B3n=20de=20errores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/base_controller.rb | 16 ++-------------- app/controllers/api/v1/protected_controller.rb | 2 +- app/controllers/concerns/exception_handler.rb | 8 ++++++++ app/controllers/private_controller.rb | 14 ++++++++++++-- app/models/site.rb | 4 ++++ 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 48368d3..3ca3016 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -4,11 +4,11 @@ module Api module V1 # API class BaseController < ActionController::Base + include ExceptionHandler + protect_from_forgery with: :null_session respond_to :json - rescue_from ActionController::RoutingError, with: :page_not_found - private # Realiza la inversa de Site#hostname @@ -26,18 +26,6 @@ module Api def origin request.headers['Origin'] end - - # El primer sitio es el sitio por defecto - # - # @return [Site] - def default_site - Site.first - end - - # Muestra una página 404 - def page_not_found - render 'application/page_not_found', status: :not_found - end end end end diff --git a/app/controllers/api/v1/protected_controller.rb b/app/controllers/api/v1/protected_controller.rb index 579732f..bce9555 100644 --- a/app/controllers/api/v1/protected_controller.rb +++ b/app/controllers/api/v1/protected_controller.rb @@ -128,7 +128,7 @@ module Api # # @return [TrueClass] def log_entry - LogEntry.create site: site || default_site, text: { + LogEntry.create site: site || Site.default, text: { reason: @reason, status: response.status, headers: request.headers.to_h.select { |k, _| /\A[A-Z]/ =~ k }, diff --git a/app/controllers/concerns/exception_handler.rb b/app/controllers/concerns/exception_handler.rb index 3be8412..8c4f54c 100644 --- a/app/controllers/concerns/exception_handler.rb +++ b/app/controllers/concerns/exception_handler.rb @@ -5,12 +5,20 @@ module ExceptionHandler extend ActiveSupport::Concern class SiteNotFound < StandardError; end + class PageNotFound < StandardError; end included do rescue_from SiteNotFound, with: :site_not_found + rescue_from PageNotFound, with: :page_not_found + rescue_from ActionController::RoutingError, with: :page_not_found + rescue_from Pundit::NilPolicyError, with: :page_not_found end def site_not_found redirect_to sites_path end + + def page_not_found + send_file Rails.root.join('public', '404.html') + end end diff --git a/app/controllers/private_controller.rb b/app/controllers/private_controller.rb index 7cca131..36a7a78 100644 --- a/app/controllers/private_controller.rb +++ b/app/controllers/private_controller.rb @@ -7,7 +7,6 @@ class PrivateController < ApplicationController skip_forgery_protection include Pundit - rescue_from Pundit::NilPolicyError, with: :page_not_found # Enviar el archivo si existe, agregar una / al final siempre para no # romper las direcciones relativas. @@ -56,7 +55,7 @@ class PrivateController < ApplicationController @path rescue Errno::ENOENT - File.join(deploy_private.destination, '404.html') + not_found_path end # Devuelve la ruta del archivo, limpieza copiada desde Jekyll @@ -75,4 +74,15 @@ class PrivateController < ApplicationController @file = @file.gsub('..', '/').gsub('./', '').squeeze('/') end + + # Devuelve una página 404.html + def not_found_path + return site_not_found_path if File.exist? site_not_found_path + + raise PageNotFound + end + + def site_not_found_path + @site_not_found_path ||= File.join(deploy_private.destination, '404.html') + end end diff --git a/app/models/site.rb b/app/models/site.rb index 8f74b3c..c99bc14 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -384,6 +384,10 @@ class Site < ApplicationRecord File.join(Rails.root, '_sites') end + def self.default + find_by(name: Site.domain + '.') + end + def reset @read = false @layouts = nil