egregious 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +44 -9
- data/lib/egregious.rb +21 -2
- data/lib/egregious/version.rb +1 -1
- metadata +3 -3
data/README
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
Egregious is a rails based exception handling gem for well defined http exception handling for json, xml and html.
|
2
2
|
|
3
3
|
If you have a json or xml api into your rails application, you probably have added your own exception handling to map
|
4
|
-
exceptions to a http status and formatting your json and xml output. We
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
exceptions to a http status and formatting your json and xml output. We decided to create egregious. One of the goals
|
5
|
+
is to start providing a more consistent api error experience for all rails applications. As of the creation of
|
6
|
+
egregious the behavior of rails was to return html when an exception is thrown with the status code of 500. With
|
7
|
+
egregious proper json and html of the error will be returned with a good default mapping of exceptions to http status
|
8
|
+
codes. This allows api developers to respond to the status code properly, instead of scratching their head with 500's
|
9
|
+
coming back all the time. If the problem was the api caller then the result codes are in the 300 range. If the problem
|
10
|
+
was on the server then the status codes are in the 500 range. The returned exception message and exception type
|
11
|
+
provide the caller context information.
|
12
12
|
|
13
13
|
What egregious can do:
|
14
14
|
|
@@ -20,6 +20,8 @@ message and exception type providing more context information.
|
|
20
20
|
falling back to the 500.html page.
|
21
21
|
* Defines exceptions for all http status codes allowing you to throw these exceptions anywhere in your code.
|
22
22
|
* Allows you to change the exception mapping to fit your needs, adding exceptions and changing status mapping.
|
23
|
+
* If Hoptoad is defined it will send the errors to Hoptoad/Airbrake.
|
24
|
+
* The error will be logged with stack trace
|
23
25
|
|
24
26
|
|
25
27
|
REQUIRES:
|
@@ -44,4 +46,37 @@ In your code if you want to send an error back just throw an exception. For exam
|
|
44
46
|
raise Egregious::BadRequest.new("You can not created an order without a customer.") unless customer_id
|
45
47
|
|
46
48
|
All the http status codes have exception classes named after them in the Egregious module. You can throw any exception,
|
47
|
-
or define your own exceptions. You can find a list in the Rack::Utils::HTTP_STATUS_CODES class.
|
49
|
+
or define your own exceptions. You can find a list in the Rack::Utils::HTTP_STATUS_CODES class.
|
50
|
+
|
51
|
+
If you want to change the behavior then you can override the following methods in your ApplicationController.
|
52
|
+
|
53
|
+
# override this if you want your flash to behave differently
|
54
|
+
def egregious_flash(exception)
|
55
|
+
flash.now[:alert] = exception.message
|
56
|
+
end
|
57
|
+
|
58
|
+
# override this if you want your logging to behave differently
|
59
|
+
def egregious_log(exception)
|
60
|
+
logger.fatal(
|
61
|
+
"\n\n" + exception.class.to_s + ' (' + exception.message.to_s + '):\n ' +
|
62
|
+
clean_backtrace(exception).join("\n ") +
|
63
|
+
"\n\n")
|
64
|
+
HoptoadNotifier.notify(exception) if defined?(HoptoadNotifier)
|
65
|
+
end
|
66
|
+
|
67
|
+
# override this if you want to change your respond_to behavior
|
68
|
+
def egregious_respond_to(exception)
|
69
|
+
respond_to do |format|
|
70
|
+
status = status_code_for_exception(exception)
|
71
|
+
format.xml { render :xml=> exception.to_xml, :status => status }
|
72
|
+
format.json { render :json=> exception.to_json, :status => status }
|
73
|
+
# render the html page for the status we are returning it exists...if not then render the 500.html page.
|
74
|
+
format.html { render :file => File.exists?(build_html_file_path(status)) ?
|
75
|
+
build_html_file_path(status) : build_html_file_path('500')}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# override this if you want to change what html static file gets returned.
|
80
|
+
def build_html_file_path(status)
|
81
|
+
File.expand_path(Rails.root, 'public', status + '.html')
|
82
|
+
end
|
data/lib/egregious.rb
CHANGED
@@ -133,23 +133,42 @@ module Egregious
|
|
133
133
|
self.exception_codes[exception.class] ? self.exception_codes[exception.class] : '500'
|
134
134
|
end
|
135
135
|
|
136
|
+
# this is the method that handles all the exceptions we have mapped
|
136
137
|
def egregious_exception_handler(exception)
|
138
|
+
egregious_flash(exception)
|
139
|
+
egregious_log(exception)
|
140
|
+
egregious_respond_to(exception)
|
141
|
+
end
|
142
|
+
|
143
|
+
# override this if you want your flash to behave differently
|
144
|
+
def egregious_flash(exception)
|
137
145
|
flash.now[:alert] = exception.message
|
146
|
+
end
|
147
|
+
|
148
|
+
# override this if you want your logging to behave differently
|
149
|
+
def egregious_log(exception)
|
138
150
|
logger.fatal(
|
139
151
|
"\n\n" + exception.class.to_s + ' (' + exception.message.to_s + '):\n ' +
|
140
152
|
clean_backtrace(exception).join("\n ") +
|
141
153
|
"\n\n")
|
142
154
|
HoptoadNotifier.notify(exception) if defined?(HoptoadNotifier)
|
155
|
+
end
|
156
|
+
|
157
|
+
# override this if you want to change your respond_to behavior
|
158
|
+
def egregious_respond_to(exception)
|
143
159
|
respond_to do |format|
|
144
160
|
status = status_code_for_exception(exception)
|
145
161
|
format.xml { render :xml=> exception.to_xml, :status => status }
|
146
162
|
format.json { render :json=> exception.to_json, :status => status }
|
147
163
|
# render the html page for the status we are returning it exists...if not then render the 500.html page.
|
148
|
-
format.html { render :file => File.exists?(
|
149
|
-
|
164
|
+
format.html { render :file => File.exists?(build_html_file_path(status)) ?
|
165
|
+
build_html_file_path(status) : build_html_file_path('500')}
|
150
166
|
end
|
151
167
|
end
|
152
168
|
|
169
|
+
def build_html_file_path(status)
|
170
|
+
File.expand_path(Rails.root, 'public', status + '.html')
|
171
|
+
end
|
153
172
|
|
154
173
|
def self.included(base)
|
155
174
|
base.class_eval do
|
data/lib/egregious/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: egregious
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Russell Edens
|