Skip to content

Commit

Permalink
0.8 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
richpeck committed Jul 12, 2018
1 parent d8eca36 commit 04fffc9
Show file tree
Hide file tree
Showing 94 changed files with 2,191 additions and 215 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tmp
*.a
mkmf.log
*.tmp
.rspec_status

##########################################################
##########################################################
Expand Down
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format progress
--color
--require spec_helper
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
language: ruby
rvm:
- 2.2.4
- 2.3.2
- 2.4.0
- 2.3.7
- 2.4.4
- 2.5.1
- ruby-head
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ group :test do
gem 'coveralls', require: false
end

# => Required for Windows
gem 'tzinfo-data' if Gem.win_platform? # => TZInfo For Windows

###########################################
449 changes: 323 additions & 126 deletions README.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions app/assets/stylesheets/exception_handler.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
*/
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/*
*= link_tree ../images
*/
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
9 changes: 1 addition & 8 deletions app/assets/stylesheets/styles/_base.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */

/*
*= link_tree ../../images
*/

/* ---------------------------------------------------- */
/* ---------------------------------------------------- */

* { margin: 0; }
html, body { height: 100%; }
html {
height: 100%;
color: #fff;
background: #010008 url(<%= asset_url("exception_handler/bg.jpg") %>) top left no-repeat;
background: #121212;
background-size: 100% 100%;
box-sizing: border-box;
}
Expand Down
3 changes: 2 additions & 1 deletion app/assets/stylesheets/styles/_exception.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
border-color: rgba(255,255,255,0.09);
border-style: solid
}
.exception:hover { cursor: pointer; }
.exception:hover { cursor: pointer; }
.exception:hover:after { text-decoration: underline; }

.exception span:before {
display: block;
Expand Down
13 changes: 10 additions & 3 deletions app/controllers/exception_handler/exceptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ class ExceptionsController < ApplicationController
layout :layout

####################
# Action #
# Actions #
####################

# => General Show Functionality
# => Introduced new "action" config option in 0.8.0.0
def show
respond_with @exception, status: @exception.status
end
Expand All @@ -48,8 +50,13 @@ def show

private

def layout
ExceptionHandler.config.layouts[@exception.status]
# => Pulls from Exception class
# => Spanner in the works is nil
# => .present? validates against empty strings (IE a string is present)
# => .nil? validates to see if the returned data is "nil"
# => nil required to facilitate inheritance of the layout w/ ApplicationController
def layout option = ExceptionHandler.config.options(@exception.status, :layout)
(option.present? || option.nil?) ? option : 'exception'
end

##################################
Expand Down
41 changes: 21 additions & 20 deletions app/models/exception_handler/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ module ExceptionHandler
# => Attributes
# => Determine schema etc
ATTRS = %i(class_name status message trace target referrer params user_agent)

# => Exceptions to be rescued by ExceptionHandler
EXCEPTIONS_TO_BE_RESCUED = [ActionController::RoutingError, AbstractController::ActionNotFound].tap do |list|
list << ActiveRecord::RecordNotFound if defined?(ActiveRecord)
list << Mongoid::Errors::DocumentNotFound if defined?(Mongoid)
end

############################################################
############################################################

# => Class (inheritance dependent on whether db option is available)
self::Exception = Class.new(
(ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object
) do
self::Exception =
Class.new( (ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object ) do

# => Include individual elements
# => Only required if no db present (no ActiveRecord)
Expand Down Expand Up @@ -97,15 +90,14 @@ def self.table_name
# => Email
# => after_initialize invoked after .new method called
# => Should have been after_create but user may not save
after_initialize Proc.new { |e| ExceptionHandler::ExceptionMailer.new_exception(e).deliver } if ExceptionHandler.config.try(:email).try(:is_a?, String)
after_initialize -> (e) { ExceptionHandler::ExceptionMailer.new_exception(e).deliver }, if: :email? # => see bottom of file

# => Attributes
attr_accessor :request, :klass, :exception, :description
attr_accessor *ATTRS unless ExceptionHandler.config.try(:db)

# => Validations
validates :klass, exclusion: { in: EXCEPTIONS_TO_BE_RESCUED, message: "%{value}" }, if: -> { referer.blank? } # => might need full Proc syntax
validates :user_agent, format: { without: Regexp.new( BOTS.join("|"), Regexp::IGNORECASE ) }
validates :user_agent, format: { without: Regexp.new( BOTS.join("|"), Regexp::IGNORECASE ) }

##################################
##################################
Expand All @@ -114,21 +106,22 @@ def self.table_name
# Virtual
####################################

# => Exception (virtual)
# => Basis on which all the class is built
def exception
request.env['action_dispatch.exception']
end

# => Klass
# => Used for validation (needs to be cleaned up in 0.7.0)
def klass
exception.class
end

# => Exception (virtual)
def exception
request.env['action_dispatch.exception']
end

# => Description
def description
I18n.with_options scope: [:exception_handler], message: message, status: status do |i18n|
i18n.t response, default: Rack::Utils::HTTP_STATUS_CODES[status] || status
i18n.t response, default: Rack::Utils::HTTP_STATUS_CODES[status]
end
end

Expand All @@ -143,7 +136,7 @@ def class_name

# => Message
def message
exception.message
exception ? exception.message : Rack::Utils::HTTP_STATUS_CODES[status]
end

# => Trace
Expand Down Expand Up @@ -181,7 +174,7 @@ def user_agent

# => Status code (404, 500 etc)
def status
ActionDispatch::ExceptionWrapper.new(request.env, exception).status_code
exception ? ActionDispatch::ExceptionWrapper.new(request.env, exception).try(:status_code) : request.env["PATH_INFO"][1..-1].to_i
end

# => Server Response ("Not Found" etc)
Expand All @@ -192,6 +185,14 @@ def response
##################################
##################################

private

# => Email
# => should be on the same line as after_initialize but too long
def email?
ExceptionHandler.config.try(:email).try(:is_a?, String) && ExceptionHandler.config.options(status, :notification) != false
end

end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/exception_handler/exceptions/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= content_tag :div, class: "exception", data: { status: @exception.status, response: @exception.response.to_s.humanize, rails: Rails.version }, onclick: ("location.href=\"#{root_url}\";" if @exception.status == "500" && Rails.application.routes.recognize_path("/")), title: ("Return Home" if @exception.status == "500" && Rails.application.routes.recognize_path("/")) do %>
<%= content_tag :div, class: "exception", data: { status: @exception.status, response: @exception.response.to_s.humanize, rails: Rails.version }, onclick: ("location.href=\"#{root_url}\";" if @exception.status.to_s.first == "5" && Rails.application.routes.recognize_path("/")) do %>
<%= content_tag :span, @exception.description.html_safe %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/layouts/exception.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<%= stylesheet_link_tag :exception_handler %>
<%= favicon_link_tag "exception_handler/favicon.ico" %>
<% if x = ExceptionHandler.config.options(@exception.status) %>
<style>body { background: url("<%= asset_path x %>") center center no-repeat !important; background-size: cover !important; }</style>
<% end %>

<!-- Auth -->
<%= csrf_meta_tags %>
</head>
Expand All @@ -24,4 +28,5 @@
<% end %>
<% end %>
<% end %>

</html>
5 changes: 5 additions & 0 deletions app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<html>
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /></head>
<body><%= yield %></body>
</html>
1 change: 1 addition & 0 deletions app/views/layouts/mailer.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= yield %>
2 changes: 1 addition & 1 deletion config/locales/exception_handler.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

en:
exception_handler:
internal_server_error: "<strong>%{status} Error</strong> %{message} <p>Return home</p>"
internal_server_error: "<strong>%{status} Error</strong> %{message}"

#######################################################################################
#######################################################################################
42 changes: 42 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
########################################
########################################
## _____ _ ##
## | ___ \ | | ##
## | |_/ /___ _ _| |_ ___ ___ ##
## | // _ \| | | | __/ _ \/ __| ##
## | |\ \ (_) | |_| | || __/\__ \ ##
## \_| \_\___/ \__,_|\__\___||___/ ##
## ##
########################################
########################################

## Good resource
## https://gist.github.com/maxivak/5d428ade54828836e6b6#merge-engine-and-app-routes

########################################
########################################

## Routes ##
Rails.application.routes.draw do

########################################
########################################

# => ExceptionHandler
# => Used to provide error page examples in "dev" mode
if Object.const_defined?('ExceptionHandler') && ExceptionHandler.config.try(:dev)

# => Items
Rack::Utils::SYMBOL_TO_STATUS_CODE.select{ |key, value| value.to_s.match('\b(?:4[0-9]{2}|5[0-9]{2}|599)\b') }.each do |code, status|
get status.to_s, to: 'exception_handler/exceptions#show', as: code, code: code
end

end

########################################
########################################

end

########################################
########################################
17 changes: 9 additions & 8 deletions exception_handler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ require_relative 'lib/exception_handler/version'
Gem::Specification.new do |s|

## General ##
s.name = "exception_handler"
s.authors = ["R.Peck"]
s.email = ["rpeck@fl.co.uk"]
s.version = ExceptionHandler::VERSION::STRING
s.platform = Gem::Platform::RUBY
s.name = "exception_handler"
s.authors = ["R.Peck"]
s.email = ["rpeck@fl.co.uk"]
s.version = ExceptionHandler::VERSION::STRING
s.platform = Gem::Platform::RUBY

## Details ##
s.summary = %q{Rails gem to show custom error pages in production. Also logs errors in db & sends notification emails}
s.description = %q{Rails gem to create custom error pages. Captures exceptions using "exception_app" callback, routing to "Exception" controller, rendering the view as required.}
s.homepage = "https://github.com/richpeck/exception_handler"
s.summary = %q{Rails gem to show custom error pages in production. Also logs errors in db & sends notification emails}
s.description = %q{Rails gem to create custom error pages. Captures exceptions using "exception_app" callback, routing to "Exception" controller, rendering the view as required.}
s.post_install_message = %q{ExceptionHandler 0.8.0.0 → New "config" (config.exception_handler = {exceptions: {layout: 'x', notification: true, action: {redirect_to root_path} }} ). https://www.github.com/richpeck/exception_handler#config for more info. }
s.homepage = "https://github.com/richpeck/exception_handler"

## License ##
s.license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions lib/exception_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Error < StandardError; end
##############################
##############################


end

#########################################################
Expand Down

0 comments on commit 04fffc9

Please sign in to comment.