require "we_bridge/exceptions/version" require 'active_record/errors' require 'action_controller' require 'action_controller/metal/exceptions' module WeBridge module Exceptions # 401 class UnauthorizedError < ActionController::ActionControllerError; end # 403 class ForbiddenError < ActionController::ActionControllerError; end # 406 class NotAcceptableError < ActionController::ActionControllerError; end def self.included(c) if c.respond_to? :rescue_from c.rescue_from Exception, with: :render_500 c.rescue_from ActiveRecord::RecordNotUnique, with: :render_409 c.rescue_from NotAcceptableError, with: :render_406 c.rescue_from ActionController::RoutingError, with: :render_404 c.rescue_from ActiveRecord::RecordNotFound, with: :render_404 c.rescue_from ForbiddenError, with: :render_403 c.rescue_from UnauthorizedError, with: :render_401 c.rescue_from ActionController::BadRequest, with: :render_400 end end def render_500(exception = nil) handle_error(exception , 500) end def render_409(exception = nil) handle_error(exception , 409) end def render_406(exception = nil) handle_error(exception , 406) end def render_404(exception = nil) handle_error(exception , 404) end def render_403(exception = nil) handle_error(exception , 403) end def render_401(exception = nil) handle_error(exception , 401) end def render_400(exception = nil) handle_error(exception , 400) end def handle_error(exception , status_code) logger.info "Rendering #{status_code} with exception: #{exception.message}" if exception render file: WeBridge::Exceptions.error_template_file_path(status_code), status: status_code, content_type: 'text/html' end def self.error_template_file_path(status_code) File.expand_path("../views/#{status_code}.html.erb",__FILE__) end end end