renderror 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf0849a4098632c1107b831164c2eb590c6bf9e0
4
+ data.tar.gz: 43e8eb0da0171280e300820eee7e6597f98247c1
5
+ SHA512:
6
+ metadata.gz: 2bf62640dc1aab88e8496493693bb0bd65a06fcbd5d8fc2c6d126fcd6dea7d5c93d13d6b994c1a9523ce209aa2bbc643808bf5cb5d647d79c089da28b3b71308
7
+ data.tar.gz: ba1ef634c009863693e954f12bed8cc1a369938a4ce8bd8b79fbbb1501db3340702a61a0d98ad0fa9a1dcf012a2f9fc80024197904b0aa0be7864efc0bc45cf1
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.overcommit.yml ADDED
@@ -0,0 +1,33 @@
1
+ # Use this file to configure the Overcommit hooks you wish to use. This will
2
+ # extend the default configuration defined in:
3
+ # https://github.com/brigade/overcommit/blob/master/config/default.yml
4
+ #
5
+ # At the topmost level of this YAML file is a key representing type of hook
6
+ # being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7
+ # customize each hook, such as whether to only run it on certain files (via
8
+ # `include`), whether to only display output if it fails (via `quiet`), etc.
9
+ #
10
+ # For a complete list of hooks, see:
11
+ # https://github.com/brigade/overcommit/tree/master/lib/overcommit/hook
12
+ #
13
+ # For a complete list of options that you can use to customize hooks, see:
14
+ # https://github.com/brigade/overcommit#configuration
15
+ #
16
+ # Uncomment the following lines to make the configuration take effect.
17
+
18
+ PreCommit:
19
+ RuboCop:
20
+ enabled: true
21
+ on_warn: fail # Treat all warnings as failures
22
+ #
23
+ # TrailingWhitespace:
24
+ # enabled: true
25
+ # exclude:
26
+ # - '**/db/structure.sql' # Ignore trailing whitespace in generated files
27
+ #
28
+ #PostCheckout:
29
+ # ALL: # Special hook name that customizes all hooks of this type
30
+ # quiet: true # Change all post-checkout hooks to only display output on failure
31
+ #
32
+ # IndexTags:
33
+ # enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at isaacdnorman@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in renderror.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Isaac Norman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,242 @@
1
+ # Renderror
2
+
3
+ Renderror makes it easy to render JSON:Api compliant error messages from your Rails API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'renderror'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install renderror
20
+
21
+ Then run the installer:
22
+
23
+ $ rails g renderror:install
24
+
25
+ ## Basic Usage
26
+
27
+ Out of the box Renderror allows you to call `render_errors(errors)` from anywhere in your controllers to render a simple json array of those errors. The only requirement is that the errors respond to `.to_json` and to `.status`. Out of the box they will not be converted to JSON:Api Syntax, to do that see the **Custom Errors** section of this Readme.
28
+
29
+ ## Auto Rescue
30
+
31
+ Renderror has the ability to automatically rescue from common exceptions and render the correct error messages. It just requires a small amount of setup.
32
+
33
+ ```ruby
34
+ class ApiController < ApplicationController
35
+ renderror_auto_rescue :bad_request, :not_found, :invalid_document,
36
+ :cancan
37
+ end
38
+ ```
39
+
40
+ We recommend including this in a base controller of your API to ensure that these exceptions are rescued everywhere. You can turn on an off as many of the auto rescue flags as you wish. More details on each of these options can be found below.
41
+
42
+ #### `:bad_request`
43
+
44
+ Including this will automatically rescue from `ActionController::BadRequest` and give the following response.
45
+
46
+ ```json
47
+ {
48
+ "errors": [
49
+ {
50
+ "title": "Bad Request",
51
+ "status": "400",
52
+ "detail": "Bad Request"
53
+ }
54
+ ]
55
+ }
56
+ ```
57
+
58
+ #### `:not_found`
59
+
60
+ Including this will automatically rescue from `ActiveRecord::RecordNotFound` and render a response formatted similiar to:
61
+
62
+ ```json
63
+ {
64
+ "errors": [
65
+ {
66
+ "title": "Not Found",
67
+ "status": "404",
68
+ "detail": "Could not find User with id 1"
69
+ }
70
+ ]
71
+ }
72
+ ```
73
+
74
+ #### `:invalid_document`
75
+
76
+ Ties into [Active Model Serializers](https://github.com/rails-api/active_model_serializers) `JsonApi::Deserialization::InvalidDocument` method, which is used to deserialize request params. If the request params are determined to be invalid, Renderror will rescue and respond with a `Bad Request` status and error.
77
+
78
+ #### `:cancan`
79
+
80
+ Ties into the [CanCanCan](https://github.com/CanCanCommunity/cancancan) gem to allow your app to auto rescue from `CanCan::AccessDenied` and respond with a `Forbidden` error.
81
+
82
+ ```json
83
+ {
84
+ "errors": [
85
+ {
86
+ "title": "Forbidden",
87
+ "status": "403",
88
+ "detail": "You are not authorized to access this resource"
89
+ }
90
+ ]
91
+ }
92
+ ```
93
+
94
+ ## Additional Methods
95
+
96
+ There is also a number of situations where Renderror can be triggered manually, such as when wanting to render model errors or invalid authentication errors. You can do this using the following methods.
97
+
98
+ #### `render_unprocessable(resource)`
99
+
100
+ This can be called inside a controller to render validation errors
101
+
102
+ ```ruby
103
+ def create
104
+ user = User.new(user_params)
105
+
106
+ if user.save
107
+ respond_with user
108
+ else
109
+ render_unprocessable user
110
+ end
111
+ end
112
+ ```
113
+
114
+ This would return the following JSON if the user failed validation
115
+
116
+ ```json
117
+ {
118
+ "errors": [
119
+ {
120
+ "status": "422",
121
+ "title": "Unprocessable Entity",
122
+ "detail": "Name can't be blank",
123
+ "source": {
124
+ "pointer": "/data/attributes/name"
125
+ }
126
+ },
127
+ {
128
+ "status": "422",
129
+ "title": "Unprocessable Entity",
130
+ "detail": "Email can't be blank",
131
+ "source": {
132
+ "pointer": "/data/attributes/email"
133
+ }
134
+ }
135
+ ]
136
+ }
137
+ ```
138
+
139
+ #### `render_invalid_authentication`
140
+
141
+ This addon can be used to render errors when a user fails signin validation (either from Devise or a custom signin method)
142
+
143
+ ```ruby
144
+ def create
145
+ use_case = ::Sessions::CreateSession.perform(session_attributes)
146
+
147
+ if use_case.success?
148
+ respond_with use_case.session
149
+ else
150
+ raise_invalid_authentication User
151
+ end
152
+ end
153
+ ```
154
+
155
+
156
+ #### `render_reform_unprocessable(form)`
157
+
158
+ Renderror also works with the [Reform](https://github.com/trailblazer/reform) gem from Trailblazer.
159
+ Similar to `render_unprocessable`, however takes an instance of a `Reform::Form` and renders the errors from there:
160
+
161
+ ```ruby
162
+ def create
163
+ form = UserForm.new(User.new)
164
+
165
+ if form.validate(form_attributes) && form.save
166
+ respond_with form.model
167
+ else
168
+ render_reform_unprocessable form
169
+ end
170
+ end
171
+ ```
172
+
173
+ ## Customising Responses
174
+
175
+ After running the `install` command you can simply open the `renderror.en.yml` file to change any of the default titles and details for each message.
176
+
177
+ ## Custom Errors
178
+
179
+ If you with to make your own errors, you can do so by simply inheriting from the `Renderror::BaseError` class.
180
+
181
+ ```ruby
182
+ class ImATeapot < Renderror::BaseError
183
+
184
+ def status
185
+ '418'
186
+ end
187
+
188
+ private
189
+
190
+ def default_title
191
+ "I'm a teapot"
192
+ end
193
+
194
+ def default_detail
195
+ "Short and Stout"
196
+ end
197
+ end
198
+ ```
199
+
200
+ From there you can either choose to `raise` the error, or `render` it. Both these methods can take `title:`, `detail:`, and `pointer:` keyword arguments if you wish to override the defaults
201
+
202
+ #### Example Raise
203
+ ```ruby
204
+ raise ImATeapot, title: "I'm a little teapot", detail: "Tip me over, pour me out"
205
+ ```
206
+
207
+ #### Example Render
208
+
209
+ ```ruby
210
+ render_errors(ImATeapot.new)
211
+ ```
212
+
213
+ ```json
214
+ {
215
+ "errors": [
216
+ {
217
+ "title": "I'm a teapot",
218
+ "status": "418",
219
+ "detail": "Tip me over, pour me out"
220
+ }
221
+ ]
222
+ }
223
+ ```
224
+
225
+
226
+ ## Development
227
+
228
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
229
+
230
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
231
+
232
+ ## Contributing
233
+
234
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Papercloud/renderror. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
235
+
236
+ ## License
237
+
238
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
239
+
240
+ ## Code of Conduct
241
+
242
+ Everyone interacting in the Renderror project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Papercloud/renderror/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'renderror'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,11 @@
1
+ require 'rails/generators'
2
+
3
+ module Renderror
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_initializer
8
+ template 'en.yml', 'config/locales/renderror.en.yml'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ en:
2
+ renderror:
3
+ base_error:
4
+ detail: Bad Request
5
+ status: '400'
6
+ title: Bad Request
7
+ bad_request:
8
+ title: Bad Request
9
+ detail: Bad Request
10
+ unauthorized:
11
+ title: Unauthorized
12
+ detail: You need to sign in or sign up before continuing.
13
+ invalid_auth: Invalid email or password.
14
+ forbidden:
15
+ title: Forbidden
16
+ detail: Forbidden
17
+ not_found:
18
+ title: Not Found
19
+ detail: Not Found
20
+ unprocessable_entity:
21
+ title: Unprocessable Entity
22
+ detail: Unprocessable Entity
@@ -0,0 +1,57 @@
1
+ module Renderror
2
+ module AutoRescue
3
+ extend ActiveSupport::Concern
4
+
5
+ PERMITTED_EXCEPTIONS = %i[
6
+ bad_request not_found invalid_document cancan
7
+ ].freeze
8
+
9
+ included do
10
+ end
11
+
12
+ module ClassMethods
13
+ def renderror_auto_rescue(*exceptions)
14
+ sanitized_exceptions(exceptions).each do |e|
15
+ send("rescue_#{e}")
16
+ end
17
+ end
18
+
19
+ def sanitized_exceptions(exception_list)
20
+ exception_list.select { |e| PERMITTED_EXCEPTIONS.include? e }
21
+ end
22
+
23
+ def rescue_bad_request
24
+ rescue_from ActionController::BadRequest do |exception|
25
+ render_errors(
26
+ [Renderror::BadRequest.new(detail: exception.message)]
27
+ )
28
+ end
29
+ end
30
+
31
+ def rescue_not_found
32
+ rescue_from ActiveRecord::RecordNotFound do |exception|
33
+ render_errors(
34
+ [Renderror::NotFound.new(detail: exception.message)]
35
+ )
36
+ end
37
+ end
38
+
39
+ def rescue_invalid_document
40
+ rescue_from ActiveModelSerializers::Adapter::
41
+ JsonApi::Deserialization::InvalidDocument do |exception|
42
+ render_errors(
43
+ [Renderror::BadRequest.new(detail: exception.message)]
44
+ )
45
+ end
46
+ end
47
+
48
+ def rescue_cancan
49
+ rescue_from CanCan::AccessDenied do |exception|
50
+ render_errors(
51
+ [Renderror::Forbidden.new(detail: exception.message)]
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ module Renderror
2
+ class BadRequest < BaseError
3
+ def status
4
+ '400'
5
+ end
6
+
7
+ private
8
+
9
+ def default_title
10
+ I18n.t(:"renderror.bad_request.title")
11
+ end
12
+
13
+ def default_detail
14
+ I18n.t(:"renderror.bad_request.detail")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,44 @@
1
+ module Renderror
2
+ class BaseError < RuntimeError
3
+ def initialize(title: default_title, detail: default_detail, pointer: nil)
4
+ @title = title
5
+ @detail = detail
6
+ @pointer = pointer
7
+ end
8
+
9
+ def to_json
10
+ {
11
+ 'status' => status,
12
+ 'title' => title,
13
+ 'detail' => detail,
14
+ 'pointer' => pointer
15
+ }
16
+ end
17
+
18
+ def status
19
+ @status ||= I18n.t(:"renderror.base_error.status")
20
+ end
21
+
22
+ def title
23
+ @title ||= I18n.t(:"renderror.base_error.title")
24
+ end
25
+
26
+ def detail
27
+ @detail ||= I18n.t(:"renderror.base_error.detail")
28
+ end
29
+
30
+ def pointer
31
+ @pointer ||= nil
32
+ end
33
+
34
+ private
35
+
36
+ def default_title
37
+ I18n.t(:"renderror.base_error.title")
38
+ end
39
+
40
+ def default_detail
41
+ I18n.t(:"renderror.base_error.detail")
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ module Renderror
2
+ module Configuration
3
+ mattr_accessor :controller_adapters
4
+
5
+ @@controller_adapters = %w[rails rails_api rails_metal]
6
+
7
+ # Allow the default configuration to be overwritten from initializers
8
+ def configure
9
+ yield self if block_given?
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support/concern'
2
+
3
+ module Renderror
4
+ class Forbidden < BaseError
5
+ def status
6
+ '403'
7
+ end
8
+
9
+ private
10
+
11
+ def default_title
12
+ I18n.t(:"renderror.forbidden.title")
13
+ end
14
+
15
+ def default_detail
16
+ I18n.t(:"renderror.forbidden.detail")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Renderror
2
+ class NotFound < BaseError
3
+ def status
4
+ '404'
5
+ end
6
+
7
+ private
8
+
9
+ def default_title
10
+ I18n.t(:"renderror.not_found.title")
11
+ end
12
+
13
+ def default_detail
14
+ I18n.t(:"renderror.not_found.detail")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module Renderror
2
+ module ReformUnprocessable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def render_reform_unprocessable(form)
7
+ render_errors(reform_errors(form))
8
+ end
9
+
10
+ def reform_errors(form)
11
+ form.errors.messages.map do |(field, errors)|
12
+ errors.map do |error|
13
+ Renderror::UnprocessableEntity.new(
14
+ detail: "#{humanize_field(field)} #{error}",
15
+ pointer: "/data/attributes/#{field.to_s.dasherize}"
16
+ )
17
+ end
18
+ end.flatten
19
+ end
20
+
21
+ def humanize_field(field)
22
+ field.to_s.split('.').join(' ').humanize
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module Renderror
2
+ module RenderInvalidAuthentication
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def render_invalid_authentication
7
+ render_errors(
8
+ [
9
+ Renderror::Unauthorized.new(
10
+ detail: I18n.t(:"renderror.unauthorized.invalid_auth")
11
+ )
12
+ ]
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ module Renderror
2
+ module RenderUnprocessable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def render_unprocessable(resource)
7
+ render_errors(entity_errors(resource))
8
+ end
9
+
10
+ private
11
+
12
+ def entity_errors(resource)
13
+ resource.errors.map do |field, error|
14
+ Renderror::UnprocessableEntity.new(
15
+ detail: resource.errors.full_message(field, error),
16
+ pointer: "/data/attributes/#{field.to_s.dasherize}"
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ require 'renderror/reform_unprocessable'
2
+ require 'renderror/render_unprocessable'
3
+ require 'renderror/render_invalid_authentication'
4
+
5
+ module Renderror
6
+ module Renderer
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include RenderUnprocessable
11
+ include ReformUnprocessable
12
+ include RenderInvalidAuthentication
13
+
14
+ def render_errors(errors)
15
+ render json: { errors: errors.map(&:to_json) }, status: errors[0].status
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Renderror
2
+ class Unauthorized < BaseError
3
+ def status
4
+ '401'
5
+ end
6
+
7
+ private
8
+
9
+ def default_detail
10
+ I18n.t(:"renderror.unauthorized.detail")
11
+ end
12
+
13
+ def default_title
14
+ I18n.t(:"renderror.unauthorized.title")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Renderror
2
+ class UnprocessableEntity < BaseError
3
+ def status
4
+ '422'
5
+ end
6
+
7
+ private
8
+
9
+ def default_title
10
+ I18n.t(:"renderror.unprocessable_entity.title")
11
+ end
12
+
13
+ def default_detail
14
+ I18n.t(:"renderror.unprocessable_entity.detail")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Renderror
2
+ VERSION = '0.1.1'.freeze
3
+ end
data/lib/renderror.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'active_support'
2
+ require 'renderror/configuration'
3
+ require 'renderror/auto_rescue'
4
+ require 'renderror/renderer'
5
+
6
+ require 'renderror/base_error'
7
+ require 'renderror/bad_request'
8
+ require 'renderror/unauthorized'
9
+ require 'renderror/forbidden'
10
+ require 'renderror/not_found'
11
+ require 'renderror/unprocessable_entity'
12
+
13
+ module Renderror
14
+ extend Configuration
15
+
16
+ I18n.load_path << File.expand_path('./lib/generators/renderror/install/templates/en.yml')
17
+ end
18
+
19
+ module ActionController
20
+ class Base
21
+ include Renderror::AutoRescue
22
+ include Renderror::Renderer
23
+ end
24
+ end
data/renderror.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'renderror/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'renderror'
9
+ spec.version = Renderror::VERSION
10
+ spec.authors = ['Isaac Norman']
11
+ spec.email = ['isaacdnorman@gmail.com']
12
+
13
+ spec.summary = 'Render Errors Easily'
14
+ spec.description = 'Adds easy renderring to your JSON API'
15
+ spec.homepage = 'https://github.com/Papercloud/renderror'
16
+ spec.license = 'MIT'
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'rails', '> 4.2', '< 6.0'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.15'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'pry'
37
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: renderror
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Isaac Norman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.15'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.15'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: Adds easy renderring to your JSON API
90
+ email:
91
+ - isaacdnorman@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".overcommit.yml"
98
+ - ".rspec"
99
+ - ".travis.yml"
100
+ - CODE_OF_CONDUCT.md
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - bin/console
106
+ - bin/setup
107
+ - lib/generators/renderror/install/install_generator.rb
108
+ - lib/generators/renderror/install/templates/en.yml
109
+ - lib/renderror.rb
110
+ - lib/renderror/auto_rescue.rb
111
+ - lib/renderror/bad_request.rb
112
+ - lib/renderror/base_error.rb
113
+ - lib/renderror/configuration.rb
114
+ - lib/renderror/forbidden.rb
115
+ - lib/renderror/not_found.rb
116
+ - lib/renderror/reform_unprocessable.rb
117
+ - lib/renderror/render_invalid_authentication.rb
118
+ - lib/renderror/render_unprocessable.rb
119
+ - lib/renderror/renderer.rb
120
+ - lib/renderror/unauthorized.rb
121
+ - lib/renderror/unprocessable_entity.rb
122
+ - lib/renderror/version.rb
123
+ - renderror.gemspec
124
+ homepage: https://github.com/Papercloud/renderror
125
+ licenses:
126
+ - MIT
127
+ metadata:
128
+ allowed_push_host: https://rubygems.org
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.6.12
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Render Errors Easily
149
+ test_files: []