practical_errors 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/practical_errors.rb +9 -0
- data/lib/practical_errors/error_advisers/active_record/loader.rb +9 -0
- data/lib/practical_errors/error_advisers/active_record/record_not_found.rb +21 -0
- data/lib/practical_errors/error_advisers/active_record/record_not_unique.rb +20 -0
- data/lib/practical_errors/error_advisers/loader.rb +5 -0
- data/lib/practical_errors/practicable.rb +53 -0
- data/lib/practical_errors/version.rb +3 -0
- data/practical_errors.gemspec +27 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1de337bcc6877c1cb9de094109359c9a96cd8fe2
|
4
|
+
data.tar.gz: bd7048b4efbeaa3bb4fe16437ef182535e37e699
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a22e290946b7f849abdecedf204e388e979cc9ee700f3a55272b5123558be1e9e2229b40eae3c7fbb0b21fe5ff6dcdf35aa33e5362adcc7213f45f52d7b6e869
|
7
|
+
data.tar.gz: a8aee17c92cdf2ac5c7c58b59b0e9597333869ac5940de8cf8b5f29918d9e62a8ea35e8849b532c42782301f12f41606701fe0c3473deaee36ad9663166831b3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at nserihiro@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Kazuhiro Serizawa
|
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,83 @@
|
|
1
|
+
# PracticalErrors
|
2
|
+
|
3
|
+
Make rails error messages more practical.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'practical_errors'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install practical_errors
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add `include PracticalErrors::Practicable` to your controller.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class ApplicationController < ActionController::Base
|
27
|
+
# Prevent CSRF attacks by raising an exception.
|
28
|
+
# For APIs, you may want to use :null_session instead.
|
29
|
+
protect_from_forgery with: :exception
|
30
|
+
|
31
|
+
include PracticalErrors::Practicable
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
And then, error message will be like this:
|
36
|
+
|
37
|
+
```
|
38
|
+
ActiveRecord::RecordNotFound (
|
39
|
+
========== Practical Errors appends message from here ==========
|
40
|
+
|
41
|
+
Rails says, "Couldn't find Post with 'id'=1000".
|
42
|
+
|
43
|
+
You might have called ActiveRecord::FinderMethods#find, but the record for provided id was not found.
|
44
|
+
If you DO NOT want to raise ActiveRecord::RecordNotFound even if the record was not found,
|
45
|
+
you should use ActiveRecord::FinderMethods#find_by instead.
|
46
|
+
|
47
|
+
Post.find(1000) # raise ActiveRecord::RecordNotFound
|
48
|
+
Post.find_by(id: 1000) # just returns nill
|
49
|
+
|
50
|
+
|
51
|
+
========== Practical Errors appends message to here ==========
|
52
|
+
See more detail about Practical Errors: https://github.com/serihiro/practical_errors
|
53
|
+
|
54
|
+
):
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
### Supported error list
|
59
|
+
- `ActiveRecord::RecordNotFound`
|
60
|
+
- `ActiveRecord::RecordNotUnique`
|
61
|
+
|
62
|
+
Do you think that there is little error that support?
|
63
|
+
Please add !! (I will continue to increase supported errors, of course.)
|
64
|
+
|
65
|
+
## Dependency
|
66
|
+
- Rails '>= 4.0.0', '< 5.0'
|
67
|
+
- [error_arranger](https://github.com/serihiro/error_arranger) '0.1.0'
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
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.
|
72
|
+
|
73
|
+
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).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/practical_errors. 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.
|
78
|
+
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
83
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "practical_errors"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module PracticalErrors
|
2
|
+
module ErrorAdvisers
|
3
|
+
module ActiveRecord
|
4
|
+
class RecordNotFound
|
5
|
+
def self.advise(exception)
|
6
|
+
values = %r{\ACouldn't find (.*?) with '(.*?)'=(.*)\Z}.match(exception.message)
|
7
|
+
|
8
|
+
<<-"EOS".strip_heredoc
|
9
|
+
You might have called ActiveRecord::FinderMethods#find, but the record for provided id was not found.
|
10
|
+
If you DO NOT want to raise ActiveRecord::RecordNotFound even if the record was not found,
|
11
|
+
you should use ActiveRecord::FinderMethods#find_by instead.
|
12
|
+
|
13
|
+
#{values[1]}.find(#{values[3]}) # raise ActiveRecord::RecordNotFound
|
14
|
+
#{values[1]}.find_by(#{values[2]}: #{values[3]}) # just returns nill
|
15
|
+
|
16
|
+
EOS
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module PracticalErrors
|
2
|
+
module ErrorAdvisers
|
3
|
+
module ActiveRecord
|
4
|
+
class RecordNotUnique
|
5
|
+
def self.advise(exception)
|
6
|
+
<<-EOS.strip_heredoc
|
7
|
+
The column included in the record that you were about to insert or update, is not allowed duplicated value.
|
8
|
+
This error was detected by database, not Rails.
|
9
|
+
|
10
|
+
You might have configured uniqueness validation to this column, but uniqueness validation is not panacea.
|
11
|
+
In race condition, uniqueness validation was passed easily.
|
12
|
+
|
13
|
+
For more detail, see rails document
|
14
|
+
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of
|
15
|
+
EOS
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# error list
|
2
|
+
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/errors.rb
|
3
|
+
module PracticalErrors
|
4
|
+
module Practicable
|
5
|
+
def arrange_exception!(exception)
|
6
|
+
@customised_message = ''
|
7
|
+
|
8
|
+
append_header
|
9
|
+
|
10
|
+
abstract_original_message(exception)
|
11
|
+
|
12
|
+
if PracticalErrors::ErrorAdvisers::const_defined?(exception.class.to_s)
|
13
|
+
@customised_message << PracticalErrors::ErrorAdvisers::const_get(exception.class.to_s).advise(exception)
|
14
|
+
else
|
15
|
+
@customised_message << <<-"EOS".strip_heredoc
|
16
|
+
hmm... I don't know this error :(
|
17
|
+
Tell me => https://github.com/serihiro/practical_errors
|
18
|
+
EOS
|
19
|
+
end
|
20
|
+
|
21
|
+
append_footer
|
22
|
+
|
23
|
+
exception.message.gsub!(/.+/, @customised_message)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def append_header
|
29
|
+
@customised_message << <<-EOS.strip_heredoc
|
30
|
+
|
31
|
+
========== Practical Errors appends message from here ==========
|
32
|
+
EOS
|
33
|
+
end
|
34
|
+
|
35
|
+
def append_footer
|
36
|
+
@customised_message << <<-EOS.strip_heredoc
|
37
|
+
|
38
|
+
========== Practical Errors appends message to here ==========
|
39
|
+
See more detail about Practical Errors: https://github.com/serihiro/practical_errors
|
40
|
+
|
41
|
+
EOS
|
42
|
+
end
|
43
|
+
|
44
|
+
def abstract_original_message(exception)
|
45
|
+
original_message = exception.message
|
46
|
+
@customised_message << <<-"EOS".strip_heredoc
|
47
|
+
|
48
|
+
Rails says, "#{original_message}".
|
49
|
+
|
50
|
+
EOS
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'practical_errors/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'practical_errors'
|
8
|
+
spec.version = PracticalErrors::VERSION
|
9
|
+
spec.authors = ['Kazuhiro Serizawa']
|
10
|
+
spec.email = ['nserihiro@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Make rails error messages more practical.'
|
13
|
+
spec.description = 'Make rails error messages more practical.'
|
14
|
+
spec.homepage = 'https://github.com/serihiro/practical_errors'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'activesupport', '>= 4.0', '<5.0'
|
26
|
+
spec.add_runtime_dependency 'error_arranger', '~> 0.1'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: practical_errors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kazuhiro Serizawa
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '5.0'
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '4.0'
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '5.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: error_arranger
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0.1'
|
82
|
+
type: :runtime
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0.1'
|
89
|
+
description: Make rails error messages more practical.
|
90
|
+
email:
|
91
|
+
- nserihiro@gmail.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- ".gitignore"
|
97
|
+
- ".rspec"
|
98
|
+
- ".travis.yml"
|
99
|
+
- CODE_OF_CONDUCT.md
|
100
|
+
- Gemfile
|
101
|
+
- LICENSE.txt
|
102
|
+
- README.md
|
103
|
+
- Rakefile
|
104
|
+
- bin/console
|
105
|
+
- bin/setup
|
106
|
+
- lib/practical_errors.rb
|
107
|
+
- lib/practical_errors/error_advisers/active_record/loader.rb
|
108
|
+
- lib/practical_errors/error_advisers/active_record/record_not_found.rb
|
109
|
+
- lib/practical_errors/error_advisers/active_record/record_not_unique.rb
|
110
|
+
- lib/practical_errors/error_advisers/loader.rb
|
111
|
+
- lib/practical_errors/practicable.rb
|
112
|
+
- lib/practical_errors/version.rb
|
113
|
+
- practical_errors.gemspec
|
114
|
+
homepage: https://github.com/serihiro/practical_errors
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.4.5.1
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Make rails error messages more practical.
|
138
|
+
test_files: []
|