Skip to content

Commit 04fffc9

Browse files
committed
0.8 Update
1 parent d8eca36 commit 04fffc9

File tree

94 files changed

+2191
-215
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2191
-215
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tmp
2121
*.a
2222
mkmf.log
2323
*.tmp
24+
.rspec_status
2425

2526
##########################################################
2627
##########################################################

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format progress
2+
--color
3+
--require spec_helper

.travis.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
language: ruby
12
rvm:
23
- 2.2.4
3-
- 2.3.2
4-
- 2.4.0
4+
- 2.3.7
5+
- 2.4.4
6+
- 2.5.1
57
- ruby-head

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ group :test do
1010
gem 'coveralls', require: false
1111
end
1212

13+
# => Required for Windows
14+
gem 'tzinfo-data' if Gem.win_platform? # => TZInfo For Windows
15+
1316
###########################################

README.md

+323-126
Large diffs are not rendered by default.

app/assets/stylesheets/exception_handler.css.erb

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@
1515
*/
1616
/* ---------------------------------------------------- */
1717
/* ---------------------------------------------------- */
18+
/*
19+
*= link_tree ../images
20+
*/
21+
/* ---------------------------------------------------- */
22+
/* ---------------------------------------------------- */
1823
/* ---------------------------------------------------- */

app/assets/stylesheets/styles/_base.css.erb

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44
/* ---------------------------------------------------- */
55
/* ---------------------------------------------------- */
66

7-
/*
8-
*= link_tree ../../images
9-
*/
10-
11-
/* ---------------------------------------------------- */
12-
/* ---------------------------------------------------- */
13-
147
* { margin: 0; }
158
html, body { height: 100%; }
169
html {
1710
height: 100%;
1811
color: #fff;
19-
background: #010008 url(<%= asset_url("exception_handler/bg.jpg") %>) top left no-repeat;
12+
background: #121212;
2013
background-size: 100% 100%;
2114
box-sizing: border-box;
2215
}

app/assets/stylesheets/styles/_exception.css.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
border-color: rgba(255,255,255,0.09);
5454
border-style: solid
5555
}
56-
.exception:hover { cursor: pointer; }
56+
.exception:hover { cursor: pointer; }
57+
.exception:hover:after { text-decoration: underline; }
5758

5859
.exception span:before {
5960
display: block;

app/controllers/exception_handler/exceptions_controller.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class ExceptionsController < ApplicationController
3636
layout :layout
3737

3838
####################
39-
# Action #
39+
# Actions #
4040
####################
4141

42+
# => General Show Functionality
43+
# => Introduced new "action" config option in 0.8.0.0
4244
def show
4345
respond_with @exception, status: @exception.status
4446
end
@@ -48,8 +50,13 @@ def show
4850

4951
private
5052

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

5562
##################################

app/models/exception_handler/exception.rb

+21-20
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@ module ExceptionHandler
1010
# => Attributes
1111
# => Determine schema etc
1212
ATTRS = %i(class_name status message trace target referrer params user_agent)
13-
14-
# => Exceptions to be rescued by ExceptionHandler
15-
EXCEPTIONS_TO_BE_RESCUED = [ActionController::RoutingError, AbstractController::ActionNotFound].tap do |list|
16-
list << ActiveRecord::RecordNotFound if defined?(ActiveRecord)
17-
list << Mongoid::Errors::DocumentNotFound if defined?(Mongoid)
18-
end
1913

2014
############################################################
2115
############################################################
2216

2317
# => Class (inheritance dependent on whether db option is available)
24-
self::Exception = Class.new(
25-
(ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object
26-
) do
18+
self::Exception =
19+
Class.new( (ExceptionHandler.config.try(:db) && defined?(ActiveRecord)) ? ActiveRecord::Base : Object ) do
2720

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

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

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

110102
##################################
111103
##################################
@@ -114,21 +106,22 @@ def self.table_name
114106
# Virtual
115107
####################################
116108

109+
# => Exception (virtual)
110+
# => Basis on which all the class is built
111+
def exception
112+
request.env['action_dispatch.exception']
113+
end
114+
117115
# => Klass
118116
# => Used for validation (needs to be cleaned up in 0.7.0)
119117
def klass
120118
exception.class
121119
end
122120

123-
# => Exception (virtual)
124-
def exception
125-
request.env['action_dispatch.exception']
126-
end
127-
128121
# => Description
129122
def description
130123
I18n.with_options scope: [:exception_handler], message: message, status: status do |i18n|
131-
i18n.t response, default: Rack::Utils::HTTP_STATUS_CODES[status] || status
124+
i18n.t response, default: Rack::Utils::HTTP_STATUS_CODES[status]
132125
end
133126
end
134127

@@ -143,7 +136,7 @@ def class_name
143136

144137
# => Message
145138
def message
146-
exception.message
139+
exception ? exception.message : Rack::Utils::HTTP_STATUS_CODES[status]
147140
end
148141

149142
# => Trace
@@ -181,7 +174,7 @@ def user_agent
181174

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

187180
# => Server Response ("Not Found" etc)
@@ -192,6 +185,14 @@ def response
192185
##################################
193186
##################################
194187

188+
private
189+
190+
# => Email
191+
# => should be on the same line as after_initialize but too long
192+
def email?
193+
ExceptionHandler.config.try(:email).try(:is_a?, String) && ExceptionHandler.config.options(status, :notification) != false
194+
end
195+
195196
end
196197
end
197198

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<%= 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 %>
1+
<%= 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 %>
22
<%= content_tag :span, @exception.description.html_safe %>
33
<% end %>

app/views/layouts/exception.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<%= stylesheet_link_tag :exception_handler %>
1010
<%= favicon_link_tag "exception_handler/favicon.ico" %>
1111

12+
<% if x = ExceptionHandler.config.options(@exception.status) %>
13+
<style>body { background: url("<%= asset_path x %>") center center no-repeat !important; background-size: cover !important; }</style>
14+
<% end %>
15+
1216
<!-- Auth -->
1317
<%= csrf_meta_tags %>
1418
</head>
@@ -24,4 +28,5 @@
2428
<% end %>
2529
<% end %>
2630
<% end %>
31+
2732
</html>

app/views/layouts/mailer.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /></head>
4+
<body><%= yield %></body>
5+
</html>

app/views/layouts/mailer.text.erb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= yield %>

config/locales/exception_handler.en.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
en:
2424
exception_handler:
25-
internal_server_error: "<strong>%{status} Error</strong> %{message} <p>Return home</p>"
25+
internal_server_error: "<strong>%{status} Error</strong> %{message}"
2626

2727
#######################################################################################
2828
#######################################################################################

config/routes.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
########################################
2+
########################################
3+
## _____ _ ##
4+
## | ___ \ | | ##
5+
## | |_/ /___ _ _| |_ ___ ___ ##
6+
## | // _ \| | | | __/ _ \/ __| ##
7+
## | |\ \ (_) | |_| | || __/\__ \ ##
8+
## \_| \_\___/ \__,_|\__\___||___/ ##
9+
## ##
10+
########################################
11+
########################################
12+
13+
## Good resource
14+
## https://gist.github.com/maxivak/5d428ade54828836e6b6#merge-engine-and-app-routes
15+
16+
########################################
17+
########################################
18+
19+
## Routes ##
20+
Rails.application.routes.draw do
21+
22+
########################################
23+
########################################
24+
25+
# => ExceptionHandler
26+
# => Used to provide error page examples in "dev" mode
27+
if Object.const_defined?('ExceptionHandler') && ExceptionHandler.config.try(:dev)
28+
29+
# => Items
30+
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|
31+
get status.to_s, to: 'exception_handler/exceptions#show', as: code, code: code
32+
end
33+
34+
end
35+
36+
########################################
37+
########################################
38+
39+
end
40+
41+
########################################
42+
########################################

exception_handler.gemspec

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ require_relative 'lib/exception_handler/version'
1111
Gem::Specification.new do |s|
1212

1313
## General ##
14-
s.name = "exception_handler"
15-
s.authors = ["R.Peck"]
16-
s.email = ["[email protected]"]
17-
s.version = ExceptionHandler::VERSION::STRING
18-
s.platform = Gem::Platform::RUBY
14+
s.name = "exception_handler"
15+
s.authors = ["R.Peck"]
16+
s.email = ["[email protected]"]
17+
s.version = ExceptionHandler::VERSION::STRING
18+
s.platform = Gem::Platform::RUBY
1919

2020
## Details ##
21-
s.summary = %q{Rails gem to show custom error pages in production. Also logs errors in db & sends notification emails}
22-
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.}
23-
s.homepage = "https://github.com/richpeck/exception_handler"
21+
s.summary = %q{Rails gem to show custom error pages in production. Also logs errors in db & sends notification emails}
22+
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.}
23+
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. }
24+
s.homepage = "https://github.com/richpeck/exception_handler"
2425

2526
## License ##
2627
s.license = "MIT"

lib/exception_handler.rb

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Error < StandardError; end
3737
##############################
3838
##############################
3939

40+
4041
end
4142

4243
#########################################################

0 commit comments

Comments
 (0)