show_json_exceptions 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +54 -0
- data/Rakefile +2 -0
- data/lib/show_json_exceptions.rb +34 -0
- data/lib/show_json_exceptions/railtie.rb +10 -0
- data/lib/show_json_exceptions/version.rb +3 -0
- data/show_json_exceptions.gemspec +20 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Matthew Savage
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# ShowJsonExceptions - For Rails, of course
|
2
|
+
|
3
|
+
Rails catches exceptions within a ShowExceptions middleware and renders out a HTML error - in some cases you want an error be rendered as JSON for APIs consuming the app in question. This does just that.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'show_json_exceptions'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
There is nothing to do here, the railtie hooks in the middleware automatically. You can confirm the middleware has been loaded by running:
|
18
|
+
|
19
|
+
rake middleware
|
20
|
+
|
21
|
+
When an error occurs in your application you will see a response similar to the following:
|
22
|
+
|
23
|
+
{
|
24
|
+
"successful": false,
|
25
|
+
"result": {},
|
26
|
+
"error_messages": ["Exception: No route matches [GET] \"/poo/poo\"]
|
27
|
+
}
|
28
|
+
|
29
|
+
If your application is running in development mode you will also see a backtrace in your error_messages array:
|
30
|
+
|
31
|
+
{
|
32
|
+
"successful": false,
|
33
|
+
"result": {},
|
34
|
+
"error_messages": [
|
35
|
+
"Exception: No route matches [GET] \"/poo/poo\",
|
36
|
+
{
|
37
|
+
"backtrace": [
|
38
|
+
# ... Backtrace lines here as an array...
|
39
|
+
]
|
40
|
+
}
|
41
|
+
]
|
42
|
+
}
|
43
|
+
|
44
|
+
## No tests?
|
45
|
+
|
46
|
+
Yes well, this is really a simple gem.. in fact you can just add the module and the config.middleware statements yourself, but I wanted to wrap this in a gem for reuse. If you really think it needs tests then bug me and I'll see what I can do...
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "show_json_exceptions/version"
|
2
|
+
require 'show_json_exceptions/railtie' if defined?(Rails)
|
3
|
+
|
4
|
+
require 'action_dispatch/http/request'
|
5
|
+
|
6
|
+
module ShowJsonExceptions
|
7
|
+
class Middleware < ActionDispatch::ShowExceptions
|
8
|
+
def render_exception(env, exception)
|
9
|
+
log_error(exception)
|
10
|
+
exception = original_exception(exception)
|
11
|
+
|
12
|
+
request = ActionDispatch::Request.new(env)
|
13
|
+
exception_data = ["Exception: #{exception.message}"]
|
14
|
+
|
15
|
+
if @consider_all_requests_local || request.local?
|
16
|
+
exception_data << {backtrace: exception.backtrace}
|
17
|
+
end
|
18
|
+
|
19
|
+
render status_code(exception), {
|
20
|
+
successful: false,
|
21
|
+
result: {},
|
22
|
+
error_messages: exception_data
|
23
|
+
}.to_json
|
24
|
+
rescue Exception => failsafe_error
|
25
|
+
$stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}"
|
26
|
+
FAILSAFE_RESPONSE
|
27
|
+
end
|
28
|
+
|
29
|
+
def render_json(status, body)
|
30
|
+
[status, {'Content-Type' => "application/json; charset=#{Response.default_charset}", 'Content-Length' => body.bytesize.to_s}, [body]]
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module ShowJsonExceptions
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer "show_json_exceptions.config_middleware" do |app|
|
6
|
+
app.middleware.insert_after ActionDispatch::ShowExceptions, ShowJsonExceptions::Middleware
|
7
|
+
app.middleware.delete ActionDispatch::ShowExceptions
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/show_json_exceptions/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matthew Savage"]
|
6
|
+
gem.email = ["matt@amasses.net"]
|
7
|
+
gem.description = %q{Changes the default rails 'exception' behavior to render JSON instead of a HTML view (e.g. 404, 500 errors etc)}
|
8
|
+
gem.summary = %q{Changes the default rails 'exception' behavior to render JSON instead of a HTML view (e.g. 404, 500 errors etc)}
|
9
|
+
gem.homepage = "https://github.com/amasses/show_json_exceptions"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "show_json_exceptions"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ShowJsonExceptions::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "rails"
|
19
|
+
gem.add_dependency "multi_json"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: show_json_exceptions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthew Savage
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70239099802860 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70239099802860
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: multi_json
|
27
|
+
requirement: &70239099802160 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70239099802160
|
36
|
+
description: Changes the default rails 'exception' behavior to render JSON instead
|
37
|
+
of a HTML view (e.g. 404, 500 errors etc)
|
38
|
+
email:
|
39
|
+
- matt@amasses.net
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- lib/show_json_exceptions.rb
|
50
|
+
- lib/show_json_exceptions/railtie.rb
|
51
|
+
- lib/show_json_exceptions/version.rb
|
52
|
+
- show_json_exceptions.gemspec
|
53
|
+
homepage: https://github.com/amasses/show_json_exceptions
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
hash: 3302308707003729891
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: 3302308707003729891
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.17
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Changes the default rails 'exception' behavior to render JSON instead of
|
83
|
+
a HTML view (e.g. 404, 500 errors etc)
|
84
|
+
test_files: []
|