errawr-rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +15 -0
  5. data/Gemfile +33 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +133 -0
  8. data/Rakefile +10 -0
  9. data/errawr-rails.gemspec +28 -0
  10. data/lib/errawr/rails.rb +24 -0
  11. data/lib/errawr/rails/renderable.rb +23 -0
  12. data/lib/errawr/rails/renderers/json.rb +16 -0
  13. data/lib/errawr/rails/version.rb +5 -0
  14. data/spec/dummy/.gitignore +16 -0
  15. data/spec/dummy/README.rdoc +28 -0
  16. data/spec/dummy/Rakefile +6 -0
  17. data/spec/dummy/app/assets/images/.keep +0 -0
  18. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  19. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  20. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  21. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  22. data/spec/dummy/app/controllers/metadata_controller.rb +8 -0
  23. data/spec/dummy/app/controllers/renderable_controller.rb +7 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/mailers/.keep +0 -0
  26. data/spec/dummy/app/models/.keep +0 -0
  27. data/spec/dummy/app/models/concerns/.keep +0 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/config.ru +4 -0
  33. data/spec/dummy/config/application.rb +28 -0
  34. data/spec/dummy/config/boot.rb +4 -0
  35. data/spec/dummy/config/database.yml +25 -0
  36. data/spec/dummy/config/environment.rb +5 -0
  37. data/spec/dummy/config/environments/development.rb +29 -0
  38. data/spec/dummy/config/environments/production.rb +80 -0
  39. data/spec/dummy/config/environments/test.rb +36 -0
  40. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/spec/dummy/config/initializers/inflections.rb +16 -0
  43. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  44. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  45. data/spec/dummy/config/initializers/session_store.rb +3 -0
  46. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/spec/dummy/config/locales/en.yml +23 -0
  48. data/spec/dummy/config/routes.rb +56 -0
  49. data/spec/dummy/db/schema.rb +16 -0
  50. data/spec/dummy/db/seeds.rb +7 -0
  51. data/spec/dummy/lib/assets/.keep +0 -0
  52. data/spec/dummy/lib/tasks/.keep +0 -0
  53. data/spec/dummy/log/.keep +0 -0
  54. data/spec/dummy/public/404.html +58 -0
  55. data/spec/dummy/public/422.html +58 -0
  56. data/spec/dummy/public/500.html +57 -0
  57. data/spec/dummy/public/favicon.ico +0 -0
  58. data/spec/dummy/public/robots.txt +5 -0
  59. data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
  60. data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
  61. data/spec/json_spec.rb +47 -0
  62. data/spec/spec_helper.rb +28 -0
  63. metadata +238 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd2b23965005ffb5d9e4571a2be49f22ec27f646
4
+ data.tar.gz: 852a944a1062feeb9192ed35cac1d7e6a216414f
5
+ SHA512:
6
+ metadata.gz: 6f2f2f341f97eb8c6c081a1daee730861e6d6b52bd02d57f3d98f32e73f52d9595cad7c8352c126a570f8796e41ad20aaecddeed2283a142fcde2498a54f2798
7
+ data.tar.gz: 68bc6b55bda42802df8b346f28212c3532ca119bd7a3e9a43772ac7f34e4003fc67b82aa1c509b66f950397bd68155251cc919dbeab6f200e33ea96e31d610a5
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv*
19
+ .rvmrc
20
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+ - jruby-19mode
7
+ - jruby-head
8
+ - rbx
9
+ env:
10
+ - "RAILS_VERSION=3.2.0"
11
+ - "RAILS_VERSION=4.0.0"
12
+ - "RAILS_VERSION=master"
13
+ matrix:
14
+ allow_failures:
15
+ - env: "RAILS_VERSION=master"
data/Gemfile ADDED
@@ -0,0 +1,33 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in errawr-rails.gemspec
4
+ gemspec
5
+
6
+ rails_version = case ENV['RAILS_VERSION'] || 'default'
7
+ when 'master'
8
+ { git: 'https://github.com/rails/rails.git' }
9
+ when 'default'
10
+ '~> 4.0'
11
+ else
12
+ "~> #{ENV['RAILS_VERSION']}"
13
+ end
14
+
15
+ gem 'rails', rails_version
16
+
17
+ platforms :ruby do
18
+ gem 'sqlite3'
19
+ end
20
+
21
+ platforms :jruby do
22
+ gem 'activerecord-jdbcsqlite3-adapter'
23
+ end
24
+
25
+ platforms :rbx do
26
+ gem 'racc'
27
+ gem 'rubinius-coverage', github: 'rubinius/rubinius-coverage'
28
+ gem 'rubysl'
29
+ end
30
+
31
+ group :development do
32
+ gem 'coveralls', require: false
33
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anthony Smith
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.
@@ -0,0 +1,133 @@
1
+ # Errawr::Rails
2
+
3
+ Raise and render errors in Rails using Errawr
4
+
5
+ [![Build Status](https://travis-ci.org/anthonator/errawr-rails.png?branch=master)](https://travis-ci.org/anthonator/errawr-rails) [![Dependency Status](https://gemnasium.com/anthonator/errawr-rails.png)](https://gemnasium.com/anthonator/errawr-rails) [![Coverage Status](https://coveralls.io/repos/anthonator/errawr-rails/badge.png?branch=master)](https://coveralls.io/r/anthonator/errawr-rails?branch=master) [![Code Climate](https://codeclimate.com/github/anthonator/errawr-rails.png)](https://codeclimate.com/github/anthonator/errawr-rails)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'errawr-rails'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install errawr-rails
20
+
21
+ ## Usage
22
+
23
+ ### Getting Started
24
+
25
+ To start raising errors in Rails just include ```Errawr::Rails``` in a controller. This will provide access to the ```#error!``` method in your controller.
26
+
27
+ ```ruby
28
+ class SomeController < ApplicationController
29
+ include Errawr::Rails
30
+
31
+ def index
32
+ if params[:dont_work] == true
33
+ error!(:bad_request)
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ ### Rendering Error Responses
40
+
41
+ If you'd like to catch and render errors in a particular format include ```Errawr::Rails``` using the ```#with_renderer``` method.
42
+
43
+ ```ruby
44
+ class SomeController < ApplicationController
45
+ include Errawr::Rails.with_renderer(Rails::Errawr::Renderers::JSON)
46
+
47
+ def index
48
+ if params[:dont_work] == true
49
+ error!(:bad_request)
50
+ end
51
+ end
52
+ end
53
+ ```
54
+
55
+ The above example will render the error as JSON using the following format:
56
+
57
+ ```json
58
+ {
59
+ "error": "bad_request",
60
+ "description": "Bad Request"
61
+ }
62
+ ```
63
+
64
+ Depending on what renderer is used additional metadata may be added to the response output:
65
+
66
+ ```ruby
67
+ class SomeController < ApplicationController
68
+ include Errawr::Rails.with_renderer(Rails::Errawr::Renderers::JSON)
69
+
70
+ def index
71
+ if params[:dont_work] == true
72
+ error!(:bad_request, metadata: { extra_info: 'I like candy' })
73
+ end
74
+ end
75
+ end
76
+ ```
77
+
78
+ ```json
79
+ {
80
+ "error": "bad_request",
81
+ "description": "Bad Request",
82
+ "extra_info": "I like candy"
83
+ }
84
+ ```
85
+
86
+ Currently the only renderer that ships with Errawr::Rails is ```Rails::Errawr::Renderers::JSON```.
87
+
88
+
89
+ ### Custom Renderers
90
+
91
+ To create a custom renderer simple create a class with that specifies a ```call``` method that accepts a single parameter. The method can return anything that the Rails ```render``` method will accept.
92
+
93
+ ```ruby
94
+ class MyCustomRenderer
95
+ def call(error)
96
+ {
97
+ json: {
98
+ hello: 'world'
99
+ }
100
+ }
101
+ end
102
+ end
103
+ ```
104
+
105
+ ```ruby
106
+ class SomeController < ApplicationController
107
+ include Errawr::Rails.with_renderer(MyCustomRenderer)
108
+
109
+ def index
110
+ if params[:dont_work] == true
111
+ error!(:bad_request)
112
+ end
113
+ end
114
+ end
115
+ ```
116
+
117
+ ```json
118
+ {
119
+ "hello": "world"
120
+ }
121
+ ```
122
+
123
+ ### HTTP Status Codes
124
+
125
+ Errawr::Rails uses the [Errawr::HTTP](https://github.com/anthonator/errawr-http) gem to add support for [4xx](https://github.com/anthonator/errawr-http#supported-4xx-status-codes) and [5xx](https://github.com/anthonator/errawr-http#supported-5xx-status-codes) HTTP status code errors.
126
+
127
+ ## Contributing
128
+
129
+ 1. Fork it
130
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
131
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
132
+ 4. Push to the branch (`git push origin my-new-feature`)
133
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task default: :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'errawr/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'errawr-rails'
8
+ spec.version = Errawr::Rails::VERSION
9
+ spec.authors = ['Anthony Smith']
10
+ spec.email = ['anthony@sticksnleaves.com']
11
+ spec.description = %q{Raise and render errors in Rails using Errawr}
12
+ spec.summary = %q{Errawr support for Rails}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'activesupport', '>= 3.2'
22
+ spec.add_runtime_dependency 'errawr', '>= 1.1.3'
23
+ spec.add_runtime_dependency 'errawr-http', '>= 1.0.1'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec-rails'
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/rescuable'
2
+ require 'errawr'
3
+ require 'errawr/http'
4
+
5
+ require 'errawr/rails/renderable'
6
+ require 'errawr/rails/renderers/json'
7
+ require 'errawr/rails/version'
8
+
9
+ module Errawr
10
+ module Rails
11
+ def self.included(base)
12
+ base.send(:include, Errawr::ClassMethods)
13
+ end
14
+
15
+ def self.with_renderer(renderer)
16
+ mod = Module.new
17
+ mod.define_singleton_method :included do |object|
18
+ object.send(:include, Errawr::Rails)
19
+ object.send(:include, Errawr::Rails::Renderable.render_with(renderer))
20
+ end
21
+ mod
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module Errawr
2
+ module Rails
3
+ module Renderable
4
+ def self.render_with(handler)
5
+ mod = Module.new
6
+ mod.define_singleton_method :included do |object|
7
+ object.class_eval do
8
+ object.const_set(:ERRAWR_HANDLER, handler)
9
+ rescue_from Errawr::Error, with: :render_errawr
10
+
11
+ private
12
+ def render_errawr(error)
13
+ handler = self.class.const_get(:ERRAWR_HANDLER)
14
+ self.status = error.context[:http_status] || 500
15
+ render handler.new.call(error)
16
+ end
17
+ end
18
+ end
19
+ mod
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ module Errawr
2
+ module Rails
3
+ module Renderers
4
+ class JSON
5
+ def call(error)
6
+ {
7
+ json: {
8
+ error: error.context[:name] || error.key,
9
+ description: error.message
10
+ }.merge(error.metadata)
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Errawr
2
+ module Rails
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */