exceptions-resource 0.0.1p01
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 +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +114 -0
- data/Rakefile +2 -0
- data/exceptions.gemspec +24 -0
- data/lib/exceptions/base.rb +29 -0
- data/lib/exceptions/model.rb +88 -0
- data/lib/exceptions/resource.rb +26 -0
- data/lib/exceptions/simple.rb +31 -0
- data/lib/exceptions/unauthorized_application.rb +29 -0
- data/lib/exceptions/version.rb +3 -0
- data/lib/exceptions.rb +7 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2002338e27658b3366f4bd4748244ee7de86dd70
|
4
|
+
data.tar.gz: 429c9c169ca6c43149f66101b89ed7b0e89309c9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd27f05adbee686ebac0dcd257b1f33745142e163c1ad7006ac2996a0bfbc76d3bd23cef3579e82f23d52a189ee403cb97ecd94102b949c6d53375d35e4f857c
|
7
|
+
data.tar.gz: 83e5a14e3a37026d95b1af57ffb488e047e0e2fe82cf9d4de41b2ecb3fd46c15af6688c4923f949a52557d144b77cb46bc5882b5f9d5711a79ffe87d37b56e22
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Douglas Rossignolli
|
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,114 @@
|
|
1
|
+
# Exceptions
|
2
|
+
|
3
|
+
This small lib will play with many kinds of exceptions and return then as json with all needed informations to build awesome errors responses
|
4
|
+
needs `ActiveModel` as dependency if you aren't working with rails.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'exceptions', github: 'xdougx/exceptions'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install exceptions
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Its simple to use for a model
|
25
|
+
|
26
|
+
``` ruby
|
27
|
+
redaction = Redaction.new
|
28
|
+
redaction.valid?
|
29
|
+
exception = Exceptions::Model.build(redaction)
|
30
|
+
ap exception.error # awesome print to help us
|
31
|
+
```
|
32
|
+
|
33
|
+
### Result
|
34
|
+
``` json
|
35
|
+
{
|
36
|
+
"error": {
|
37
|
+
"model": "Redaction",
|
38
|
+
"field": "redaction[subject]",
|
39
|
+
"attribute": "subject",
|
40
|
+
"message": "can't be blank",
|
41
|
+
"full_message": "Subject can't be blank"
|
42
|
+
}
|
43
|
+
}
|
44
|
+
```
|
45
|
+
|
46
|
+
If you need to build a exception just with a message
|
47
|
+
|
48
|
+
``` ruby
|
49
|
+
exception = Exceptions::Simple.build(field: "Name", message: "is needed to be filled")
|
50
|
+
ap exception.error
|
51
|
+
```
|
52
|
+
|
53
|
+
### Result
|
54
|
+
``` json
|
55
|
+
{
|
56
|
+
"error": {
|
57
|
+
"message": "is needed to be filled",
|
58
|
+
"full_message": "Name is needed to be filled",
|
59
|
+
"field": "Name"
|
60
|
+
}
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
Need an application exception?
|
65
|
+
|
66
|
+
``` ruby
|
67
|
+
exception = Exceptions::UnauthorizedApplication.build(message: "You have no permission to access this API")
|
68
|
+
ap exception.error
|
69
|
+
```
|
70
|
+
|
71
|
+
### Result
|
72
|
+
``` json
|
73
|
+
{
|
74
|
+
"error": {
|
75
|
+
"message": "You have no permission to access this API"
|
76
|
+
}
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
### Build your exception
|
81
|
+
|
82
|
+
``` ruby
|
83
|
+
class Exceptions::MyAppException
|
84
|
+
|
85
|
+
def error
|
86
|
+
{
|
87
|
+
error: {
|
88
|
+
error_code: self.error_code,
|
89
|
+
message: self.message,
|
90
|
+
}
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
def message
|
95
|
+
self.object.message
|
96
|
+
end
|
97
|
+
|
98
|
+
def error_code
|
99
|
+
if self.object.error_code
|
100
|
+
end
|
101
|
+
|
102
|
+
def status
|
103
|
+
:unprocessable_entity
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
## Contributing
|
109
|
+
|
110
|
+
1. Fork it ( https://github.com/[my-github-username]/exceptions/fork )
|
111
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
112
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
113
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
114
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/exceptions.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'exceptions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "exceptions-resource"
|
8
|
+
spec.version = Exceptions::VERSION
|
9
|
+
spec.authors = ["Douglas Rossignolli"]
|
10
|
+
spec.email = ["douglas.rossignolli@gmail.com"]
|
11
|
+
spec.summary = %q{This small lib have many exceptions to work with rested exceptions and ActiveModel errors}
|
12
|
+
spec.description = %q{This small lib will play with many kinds of exceptions and return then as json with all needed informations to build awesome errors responses}
|
13
|
+
spec.homepage = "https://github.com/xdougx/exceptions"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Exceptions::Base < StandardError
|
2
|
+
attr_accessor :object, :type
|
3
|
+
|
4
|
+
# starts a new instance with an object
|
5
|
+
# @param [Object] object
|
6
|
+
def initialize object
|
7
|
+
self.object = object
|
8
|
+
end
|
9
|
+
|
10
|
+
# standard error for Models
|
11
|
+
# @param [ActiveRecord::Base] object
|
12
|
+
# @return [Exceptions::Base]
|
13
|
+
def self.build object
|
14
|
+
exception = new object
|
15
|
+
return exception
|
16
|
+
end
|
17
|
+
|
18
|
+
# return if is as simple error
|
19
|
+
# @return [Boolean]
|
20
|
+
def simple?
|
21
|
+
self.class.name.demodulize.tableize.singularize == "simple"
|
22
|
+
end
|
23
|
+
|
24
|
+
# return if is as model error
|
25
|
+
# @return [Boolean]
|
26
|
+
def model?
|
27
|
+
self.class.name.demodulize.tableize.singularize == "model"
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class Exceptions::Model < Exceptions::Base
|
2
|
+
|
3
|
+
# for model errors this method build a hash with all necessary information
|
4
|
+
# @return [String] json string
|
5
|
+
def error
|
6
|
+
self.is_nested? ? self.build_nested : self.build_normal
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_nested
|
10
|
+
{
|
11
|
+
error: {
|
12
|
+
model: self.nested_model.camelcase,
|
13
|
+
field: "#{self.nested_model}[#{self.nested_attr}]",
|
14
|
+
attribute: self.nested_attr,
|
15
|
+
message: self.message,
|
16
|
+
full_message: "#{self.nested_attr_human} #{self.message}"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_normal
|
22
|
+
{
|
23
|
+
error: {
|
24
|
+
model: self.model.camelcase,
|
25
|
+
field: "#{self.model}[#{self.attribute}]",
|
26
|
+
attribute: self.attribute,
|
27
|
+
message: self.message,
|
28
|
+
full_message: "#{self.attribute_human} #{self.message}"
|
29
|
+
}
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
# return what model is
|
34
|
+
# @return [String]
|
35
|
+
def model
|
36
|
+
self.object.class.name.demodulize.tableize.singularize.downcase
|
37
|
+
end
|
38
|
+
|
39
|
+
def attribute
|
40
|
+
self.object.errors.first[0]
|
41
|
+
end
|
42
|
+
|
43
|
+
def model_human
|
44
|
+
self.object.class.model_name.human.demodulize.tableize.singularize.downcase
|
45
|
+
end
|
46
|
+
|
47
|
+
def attribute_human
|
48
|
+
self.object.class.human_attribute_name(self.object.errors.first[0])
|
49
|
+
end
|
50
|
+
|
51
|
+
# return the error message
|
52
|
+
# @return [String]
|
53
|
+
def message
|
54
|
+
"#{self.object.errors.first[1]}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def status
|
58
|
+
:unprocessable_entity
|
59
|
+
end
|
60
|
+
|
61
|
+
def is_nested?
|
62
|
+
attribute = self.object.errors.first[0]
|
63
|
+
|
64
|
+
if self.object.errors.first[0].to_s.split(".").size > 1
|
65
|
+
self.object.respond_to?(attribute) ? false : true
|
66
|
+
else
|
67
|
+
false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def nested_model
|
72
|
+
self.object.errors.first[0].to_s.split(".").first.singularize.downcase
|
73
|
+
end
|
74
|
+
|
75
|
+
def nested_model_human
|
76
|
+
self.nested_model.capitalize.constantize.model_name.human
|
77
|
+
end
|
78
|
+
|
79
|
+
def nested_attr
|
80
|
+
self.object.errors.first[0].to_s.split(".").last
|
81
|
+
end
|
82
|
+
|
83
|
+
def nested_attr_human
|
84
|
+
self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Exceptions::Resource < Exceptions::Base
|
2
|
+
# for standard errors this method build a hash
|
3
|
+
# @return [String] json string
|
4
|
+
def error
|
5
|
+
{
|
6
|
+
error: {
|
7
|
+
model: self.object["model"],
|
8
|
+
attribute: self.object["attribute"],
|
9
|
+
field: self.object["field"],
|
10
|
+
message: self.object["message"],
|
11
|
+
full_message: "#{self.object["attribute"]} #{self.object["message"]}"
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
# return the error message
|
17
|
+
# @return [String]
|
18
|
+
def message
|
19
|
+
self.error[:message]
|
20
|
+
end
|
21
|
+
|
22
|
+
# return the error status
|
23
|
+
def status
|
24
|
+
:not_acceptable
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# represents the simple errors
|
2
|
+
module Exceptions
|
3
|
+
class Simple < Base
|
4
|
+
attr_accessor :status
|
5
|
+
|
6
|
+
|
7
|
+
# for standard errors this method build a hash
|
8
|
+
# @return [String] json string
|
9
|
+
def error
|
10
|
+
{
|
11
|
+
error: {
|
12
|
+
message: self.object[:message],
|
13
|
+
full_message: "#{self.object[:field]} #{self.object[:message]} ",
|
14
|
+
field: self.object[:field]
|
15
|
+
}
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
# return the error message
|
20
|
+
# @return [String]
|
21
|
+
def message
|
22
|
+
self.object[:message]
|
23
|
+
end
|
24
|
+
|
25
|
+
# return the error status
|
26
|
+
def status
|
27
|
+
:not_acceptable
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# represents the simple errors
|
2
|
+
module Exceptions
|
3
|
+
class UnauthorizedApplication < Base
|
4
|
+
attr_accessor :status
|
5
|
+
|
6
|
+
|
7
|
+
# for standard errors this method build a hash
|
8
|
+
# @return [String] json string
|
9
|
+
def error
|
10
|
+
{
|
11
|
+
error: {
|
12
|
+
message: self.object[:message]
|
13
|
+
}
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
# return the error message
|
18
|
+
# @return [String]
|
19
|
+
def message
|
20
|
+
self.object[:message]
|
21
|
+
end
|
22
|
+
|
23
|
+
# return the error status
|
24
|
+
def status
|
25
|
+
:unauthorized
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/exceptions.rb
ADDED
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: exceptions-resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1p01
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Douglas Rossignolli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-06 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: This small lib will play with many kinds of exceptions and return then
|
56
|
+
as json with all needed informations to build awesome errors responses
|
57
|
+
email:
|
58
|
+
- douglas.rossignolli@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- exceptions.gemspec
|
69
|
+
- lib/exceptions.rb
|
70
|
+
- lib/exceptions/base.rb
|
71
|
+
- lib/exceptions/model.rb
|
72
|
+
- lib/exceptions/resource.rb
|
73
|
+
- lib/exceptions/simple.rb
|
74
|
+
- lib/exceptions/unauthorized_application.rb
|
75
|
+
- lib/exceptions/version.rb
|
76
|
+
homepage: https://github.com/xdougx/exceptions
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">"
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.3.1
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: This small lib have many exceptions to work with rested exceptions and ActiveModel
|
100
|
+
errors
|
101
|
+
test_files: []
|