mejorar gestión de errores

This commit is contained in:
f 2020-08-29 20:21:45 -03:00
parent 475c393f46
commit e77566a3dd
5 changed files with 27 additions and 17 deletions

View file

@ -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

View file

@ -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 },

View file

@ -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

View file

@ -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

View file

@ -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