5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 08:26:23 +00:00

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 module V1
# API # API
class BaseController < ActionController::Base class BaseController < ActionController::Base
include ExceptionHandler
protect_from_forgery with: :null_session protect_from_forgery with: :null_session
respond_to :json respond_to :json
rescue_from ActionController::RoutingError, with: :page_not_found
private private
# Realiza la inversa de Site#hostname # Realiza la inversa de Site#hostname
@ -26,18 +26,6 @@ module Api
def origin def origin
request.headers['Origin'] request.headers['Origin']
end 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 end
end end

View file

@ -128,7 +128,7 @@ module Api
# #
# @return [TrueClass] # @return [TrueClass]
def log_entry def log_entry
LogEntry.create site: site || default_site, text: { LogEntry.create site: site || Site.default, text: {
reason: @reason, reason: @reason,
status: response.status, status: response.status,
headers: request.headers.to_h.select { |k, _| /\A[A-Z]/ =~ k }, headers: request.headers.to_h.select { |k, _| /\A[A-Z]/ =~ k },

View file

@ -5,12 +5,20 @@ module ExceptionHandler
extend ActiveSupport::Concern extend ActiveSupport::Concern
class SiteNotFound < StandardError; end class SiteNotFound < StandardError; end
class PageNotFound < StandardError; end
included do included do
rescue_from SiteNotFound, with: :site_not_found 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 end
def site_not_found def site_not_found
redirect_to sites_path redirect_to sites_path
end end
def page_not_found
send_file Rails.root.join('public', '404.html')
end
end end

View file

@ -7,7 +7,6 @@ class PrivateController < ApplicationController
skip_forgery_protection skip_forgery_protection
include Pundit include Pundit
rescue_from Pundit::NilPolicyError, with: :page_not_found
# Enviar el archivo si existe, agregar una / al final siempre para no # Enviar el archivo si existe, agregar una / al final siempre para no
# romper las direcciones relativas. # romper las direcciones relativas.
@ -56,7 +55,7 @@ class PrivateController < ApplicationController
@path @path
rescue Errno::ENOENT rescue Errno::ENOENT
File.join(deploy_private.destination, '404.html') not_found_path
end end
# Devuelve la ruta del archivo, limpieza copiada desde Jekyll # Devuelve la ruta del archivo, limpieza copiada desde Jekyll
@ -75,4 +74,15 @@ class PrivateController < ApplicationController
@file = @file.gsub('..', '/').gsub('./', '').squeeze('/') @file = @file.gsub('..', '/').gsub('./', '').squeeze('/')
end 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 end

View file

@ -384,6 +384,10 @@ class Site < ApplicationRecord
File.join(Rails.root, '_sites') File.join(Rails.root, '_sites')
end end
def self.default
find_by(name: Site.domain + '.')
end
def reset def reset
@read = false @read = false
@layouts = nil @layouts = nil