restful_error 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.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +113 -0
- data/Rakefile +7 -0
- data/app/views/restful_error/show.html.haml +4 -0
- data/app/views/restful_error/show.json.jbuilder +2 -0
- data/app/views/restful_error/show.xml.erb +1 -0
- data/config/locales/en.restful_error.yml +12 -0
- data/config/locales/ja.restful_error.yml +12 -0
- data/lib/restful_error.rb +40 -0
- data/lib/restful_error/engine.rb +4 -0
- data/lib/restful_error/version.rb +3 -0
- data/restful_error.gemspec +26 -0
- data/spec/restful_error_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7a0b6ecf7b2b0850d7ba6f851e133a14956e14ba
|
4
|
+
data.tar.gz: e13b73de0f1cd601fa9e9625e88947d6f4a5c778
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c724eaf4017d222802139f57968badb47ddbbf9449e1fa28d274dc1504fd52944286bea4fa984e308664830f0afc69ed377c3c435135c6b5b3e4fe7715b45604
|
7
|
+
data.tar.gz: a97290cf9558f780b32dde51e76ebd01353a1451303a60fd39dc89aec061dd97f32a216f8a2e5d74c8962d82e052579fc9a72da252c473f0cd143e05f63e6c91
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ohkubo KOHEI
|
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,113 @@
|
|
1
|
+
# RestfulError
|
2
|
+
|
3
|
+
Define your error with status code. Raise it and you will get formatted response with i18nized message.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'restful_error'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install restful_error
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Simple
|
22
|
+
#### raise me
|
23
|
+
```ruby
|
24
|
+
class PostsController < ApplicationController
|
25
|
+
before_action do
|
26
|
+
raise RestfulError[401] unless current_user
|
27
|
+
# or
|
28
|
+
raise RestfulError::Unauthorized unless current_user
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
#### Multi format response
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
get '/posts/new'
|
36
|
+
#=> render 'restful_error/show.html' with @status_code and @message
|
37
|
+
|
38
|
+
post '/posts.json'
|
39
|
+
#=> { status: 401, message: "Sign in required"} or write your json at 'restful_error/show.json'
|
40
|
+
|
41
|
+
get '/session.xml'
|
42
|
+
#=> "<error><status type="integer">401</status><message>Sign in required</message></error>" or write your xml at 'restful_error/show.xml'
|
43
|
+
```
|
44
|
+
|
45
|
+
#### I18n
|
46
|
+
|
47
|
+
```yaml
|
48
|
+
ja:
|
49
|
+
restful_error:
|
50
|
+
unauthorized: ログインしてください #401
|
51
|
+
not_found: ページが存在しません #404
|
52
|
+
```
|
53
|
+
|
54
|
+
### Advanced
|
55
|
+
#### your custom error
|
56
|
+
```ruby
|
57
|
+
class ::NoSession < RestfulError[404]; end
|
58
|
+
# or
|
59
|
+
class ::NoSession < RestfulError::NotFound; end
|
60
|
+
```
|
61
|
+
#### duck typing
|
62
|
+
```ruby
|
63
|
+
class OAuthController < ApplicationController
|
64
|
+
|
65
|
+
# all you need is status_code
|
66
|
+
class RequireTwitterLogin < StandardError
|
67
|
+
def status_code; 401; end
|
68
|
+
end
|
69
|
+
# or
|
70
|
+
class RequireTwitterLogin < StandardError
|
71
|
+
def status_code; :unauthorized; end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
#### library defined error
|
77
|
+
``` ruby
|
78
|
+
config.action_dispatch.rescue_responses["CanCan::Unauthorized"] = 401
|
79
|
+
# or
|
80
|
+
config.action_dispatch.rescue_responses["CanCan::Unauthorized"] = :unauthorized
|
81
|
+
```
|
82
|
+
#### I18n
|
83
|
+
```yaml
|
84
|
+
ja:
|
85
|
+
restful_error:
|
86
|
+
no_session: セッションがありません
|
87
|
+
oauth_controller/require_twitter_login: Twitterログインが必要です
|
88
|
+
can_can/unauthorized: 権限がありません
|
89
|
+
active_record/record_not_found: 要求されたリソースが存在しません
|
90
|
+
```
|
91
|
+
#### custom message
|
92
|
+
```ruby
|
93
|
+
class RequireLogin < StandardError
|
94
|
+
def initialize(provider = 'Unknown')
|
95
|
+
@provider = provider
|
96
|
+
end
|
97
|
+
def status_code
|
98
|
+
:unauthorized
|
99
|
+
end
|
100
|
+
def message
|
101
|
+
I18n.t('restful_error.require_login', provider: provider)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
1. Fork it ( https://github.com/kuboon/restful_error/fork )
|
110
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
111
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
112
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
113
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
<error><status_code><%= @status_code %></status_code><message><%= @message %></message></error>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
ja:
|
2
|
+
restful_error:
|
3
|
+
active_record/record_not_found: Requested resource is not found
|
4
|
+
can_can/unauthorized: Unauthorized
|
5
|
+
bad_request: Bad request #400
|
6
|
+
unauthorized: Unauthorized #401
|
7
|
+
forbidden: Forbidden #403
|
8
|
+
not_found: Not found #404
|
9
|
+
method_not_allowed: Method not allowd #405
|
10
|
+
gone: Gone #410
|
11
|
+
internal_server_error: Internal server error #500
|
12
|
+
service_unavailable: Service unavailable #503
|
@@ -0,0 +1,12 @@
|
|
1
|
+
ja:
|
2
|
+
restful_error:
|
3
|
+
active_record/record_not_found: 要求されたリソースが存在しません!
|
4
|
+
can_can/unauthorized: 権限がありません
|
5
|
+
bad_request: 不正なリクエストです #400
|
6
|
+
unauthorized: 権限がありません #401
|
7
|
+
forbidden: そのURLへのアクセスは禁止されています #403
|
8
|
+
not_found: そのURLは存在しません #404
|
9
|
+
method_not_allowed: メソッドは使用できません #405
|
10
|
+
gone: 要求されたリソースは消滅しました #410
|
11
|
+
internal_server_error: サーバエラーです。 #500
|
12
|
+
service_unavailable: サービスが一時的に利用不可能になっています。しばらく時間をおいて、再度ご確認願います。 #503
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'webrick/httpstatus'
|
2
|
+
require 'webrick/accesslog'
|
3
|
+
|
4
|
+
RestfulError = WEBrick::HTTPStatus
|
5
|
+
|
6
|
+
require "restful_error/version"
|
7
|
+
require "restful_error/engine"
|
8
|
+
|
9
|
+
module RestfulError
|
10
|
+
CodeToError.each do |_, klass|
|
11
|
+
code = klass::code
|
12
|
+
reason_phrase = klass::reason_phrase
|
13
|
+
klass.class_exec do
|
14
|
+
define_method(:status_code, ->{ code })
|
15
|
+
define_method(:reason_phrase, ->{ reason_phrase })
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ActionController
|
20
|
+
def self.included(base)
|
21
|
+
base.rescue_from StandardError do |ex|
|
22
|
+
@exception = ex
|
23
|
+
@status_code = Rack::Utils.status_code(@exception.try(:status_code)).nonzero? || ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
|
24
|
+
raise if @status_code == 500 && Rails.configuration.consider_all_requests_local
|
25
|
+
|
26
|
+
@message = @exception.message
|
27
|
+
default_message = @exception.class.new.message rescue nil
|
28
|
+
|
29
|
+
if @message == default_message
|
30
|
+
reason_phrase_key = RestfulError.reason_phrase(@status_code).downcase.gsub(/\s|-/, '_').to_sym
|
31
|
+
@message = I18n.t @exception.class.name.underscore, default: [reason_phrase_key, @exception.class.name], scope: :restful_error
|
32
|
+
end
|
33
|
+
|
34
|
+
respond_to do |format|
|
35
|
+
format.any(:json, :xml, :html){ render 'restful_error/show', status: @status_code }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'restful_error/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "restful_error"
|
8
|
+
spec.version = RestfulError::VERSION
|
9
|
+
spec.authors = ["kuboon"]
|
10
|
+
spec.email = ["ohkubo@magician.jp"]
|
11
|
+
spec.summary = %q{Define your error with status code. Raise it and you will get formatted response with i18nized message.}
|
12
|
+
spec.description = %q{Define your error with status code. Raise it and you will get formatted response with i18nized message.}
|
13
|
+
spec.homepage = ""
|
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_dependency 'rails', "> 3.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: restful_error
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kuboon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-30 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: '3.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Define your error with status code. Raise it and you will get formatted
|
70
|
+
response with i18nized message.
|
71
|
+
email:
|
72
|
+
- ohkubo@magician.jp
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- app/views/restful_error/show.html.haml
|
85
|
+
- app/views/restful_error/show.json.jbuilder
|
86
|
+
- app/views/restful_error/show.xml.erb
|
87
|
+
- config/locales/en.restful_error.yml
|
88
|
+
- config/locales/ja.restful_error.yml
|
89
|
+
- lib/restful_error.rb
|
90
|
+
- lib/restful_error/engine.rb
|
91
|
+
- lib/restful_error/version.rb
|
92
|
+
- restful_error.gemspec
|
93
|
+
- spec/restful_error_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
homepage: ''
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.2.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Define your error with status code. Raise it and you will get formatted response
|
119
|
+
with i18nized message.
|
120
|
+
test_files:
|
121
|
+
- spec/restful_error_spec.rb
|
122
|
+
- spec/spec_helper.rb
|