restful_error 0.0.10 → 1.0.2
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 +5 -5
- data/lib/restful_error/engine.rb +0 -0
- data/lib/restful_error/inflector.rb +26 -0
- data/lib/restful_error/railtie.rb +31 -0
- data/lib/restful_error/status.rb +37 -0
- data/lib/restful_error/version.rb +3 -1
- data/lib/restful_error.rb +36 -52
- data/spec/rack_spec.rb +2 -1
- data/spec/railtie_spec.rb +18 -0
- data/spec/restful_error_spec.rb +38 -15
- data/spec/spec_helper.rb +6 -2
- metadata +23 -73
- data/.gitignore +0 -22
- data/.rspec +0 -2
- data/.travis.yml +0 -3
- data/Gemfile +0 -4
- data/LICENSE.txt +0 -22
- data/README.md +0 -119
- data/Rakefile +0 -7
- data/app/views/restful_error/show.html.erb +0 -4
- data/app/views/restful_error/show.html.haml +0 -5
- data/app/views/restful_error/show.json.jbuilder +0 -3
- data/app/views/restful_error/show.xml.erb +0 -5
- data/config/locales/en.restful_error.yml +0 -12
- data/config/locales/ja.restful_error.yml +0 -12
- data/lib/restful_error/wrapper.rb +0 -23
- data/restful_error.gemspec +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d9c9179f83c80510bc4d7a61c63f3bb73ec7767853a601063f3f407c07e8c12f
|
4
|
+
data.tar.gz: c3b69c8c5aeb55ebcff66c10825af68d4fec44f93af18e9588ae06edaff71542
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93c65efd5ece3f8dffc6760086b17ac682ec7577583023deeedcb45565593d8c31cc9cb465a0de9bbc2c5c8de640655793e6e194138e7ff5423d7055b6c3f561
|
7
|
+
data.tar.gz: '09e8c789c9d89598f66cb00d3b21e8a526e5908c006893f5e920d81be07fb32f7d61d2745a18c5611bd9071dde261884f26f5a4d875422a762d54079ec6c04f3'
|
data/lib/restful_error/engine.rb
CHANGED
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RestfulError
|
4
|
+
module Inflector
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def underscore(word_)
|
8
|
+
return word_.underscore if word_.respond_to?(:underscore)
|
9
|
+
|
10
|
+
word = word_.dup
|
11
|
+
word.gsub!("::", "/")
|
12
|
+
word.gsub!(/(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z\d])(?=[A-Z])/, "_")
|
13
|
+
word.tr!("-", "_")
|
14
|
+
word.downcase!
|
15
|
+
end
|
16
|
+
|
17
|
+
def camelize(word_)
|
18
|
+
return word_.camelize if word_.respond_to?(:camelize)
|
19
|
+
|
20
|
+
word = word_.dup
|
21
|
+
word.sub!(/^[a-z\d]*/) { ::Regexp.last_match(0).capitalize }
|
22
|
+
word.gsub!(%r{(?:_|(/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
|
23
|
+
word.gsub!("/", "::")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "abstract_controller"
|
2
|
+
require "action_controller/metal"
|
3
|
+
|
4
|
+
module RestfulError
|
5
|
+
class ExceptionsController < ::ActionController::Metal
|
6
|
+
include AbstractController::Rendering
|
7
|
+
include ActionView::Layouts
|
8
|
+
|
9
|
+
append_view_path File.join(File.dirname(__FILE__), "../../app/views")
|
10
|
+
|
11
|
+
def show
|
12
|
+
@exception = request.env["action_dispatch.exception"]
|
13
|
+
status = Status.new(request.path_info[1..].to_i)
|
14
|
+
@status_code = status.code
|
15
|
+
@reason_phrase = status.reason_phrase
|
16
|
+
@message = @exception.try(:response_message)
|
17
|
+
unless @message
|
18
|
+
class_name = @exception.class.name
|
19
|
+
class_key = RestfulError::Inflector.underscore(class_name)
|
20
|
+
@message = I18n.t class_key, default: [ status.symbol, @reason_phrase ], scope: :restful_error
|
21
|
+
end
|
22
|
+
|
23
|
+
self.status = status.code
|
24
|
+
render "restful_error/show"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.exceptions_app
|
29
|
+
ExceptionsController.action(:show)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "restful_error/inflector"
|
4
|
+
require "rack/utils"
|
5
|
+
|
6
|
+
module RestfulError
|
7
|
+
STATUS_CODE_TO_SYMBOL = Rack::Utils::SYMBOL_TO_STATUS_CODE.invert
|
8
|
+
def self.code_from(code_or_sym_or_const_name)
|
9
|
+
case code_or_sym_or_const_name
|
10
|
+
when Integer
|
11
|
+
code_or_sym_or_const_name
|
12
|
+
when Symbol
|
13
|
+
str = code_or_sym_or_const_name.to_s
|
14
|
+
sym = /[A-Z]/.match?(str[0]) ? RestfulError::Inflector.underscore(str).to_sym : code_or_sym_or_const_name
|
15
|
+
begin
|
16
|
+
Rack::Utils.status_code(sym)
|
17
|
+
rescue ArgumentError
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
when /\A\d{3}\z/
|
21
|
+
code_from(code_or_sym_or_const_name.to_i)
|
22
|
+
else
|
23
|
+
raise ArgumentError, "Invalid argument: #{code_or_sym_or_const_name.inspect}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Status = Data.define(:code, :reason_phrase, :symbol, :const_name) do
|
28
|
+
def initialize(code:)
|
29
|
+
reason_phrase = Rack::Utils::HTTP_STATUS_CODES[code]
|
30
|
+
raise ArgumentError, "Invalid status code: #{code}" unless reason_phrase
|
31
|
+
|
32
|
+
symbol = STATUS_CODE_TO_SYMBOL[code]
|
33
|
+
const_name = Inflector.camelize(symbol.to_s)
|
34
|
+
super(code:, reason_phrase:, symbol:, const_name:)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/restful_error.rb
CHANGED
@@ -1,70 +1,54 @@
|
|
1
|
-
|
2
|
-
require 'webrick/accesslog'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
3
|
+
require "rack/utils"
|
4
|
+
require "restful_error/railtie" if defined? ActionController
|
5
|
+
require "restful_error/status"
|
5
6
|
|
6
|
-
|
7
|
-
require 'restful_error/engine'
|
8
|
-
require 'restful_error/wrapper'
|
9
|
-
require 'abstract_controller'
|
10
|
-
require 'action_controller/metal'
|
7
|
+
I18n.load_path += Dir["#{File.expand_path("../config/locales")}/*.yml"] if defined? I18n
|
11
8
|
|
12
9
|
module RestfulError
|
13
|
-
|
14
|
-
code = klass::code
|
15
|
-
reason_phrase = klass::reason_phrase
|
16
|
-
klass.class_exec do
|
17
|
-
define_method(:status_code, ->{ code })
|
18
|
-
define_method(:reason_phrase, ->{ reason_phrase })
|
19
|
-
end
|
20
|
-
end
|
10
|
+
class BaseError < StandardError; end
|
21
11
|
|
22
12
|
module Helper
|
23
13
|
def restful
|
24
|
-
|
14
|
+
raise NotImplementedError, "http_status must be implemented by including class" unless respond_to?(:http_status)
|
15
|
+
|
16
|
+
@restful ||= Status.new(RestfulError.code_from(http_status))
|
25
17
|
end
|
26
18
|
end
|
27
19
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
@
|
33
|
-
@reason_phrase = ex.restful.reason_phrase
|
34
|
-
@message = ex.restful.message
|
35
|
-
|
36
|
-
raise if @status_code == 500 && Rails.configuration.consider_all_requests_local
|
37
|
-
|
38
|
-
respond_to do |format|
|
39
|
-
format.any(:json, :xml, :html){ render 'restful_error/show', status: @status_code }
|
40
|
-
end
|
20
|
+
@cache = {}
|
21
|
+
class << self
|
22
|
+
def [](code_like)
|
23
|
+
code = RestfulError.code_from(code_like)
|
24
|
+
@cache[code] ||= build_error_class_for(code)
|
41
25
|
end
|
42
|
-
def self.included(base)
|
43
|
-
base.rescue_from StandardError do |ex|
|
44
|
-
render_exception ex
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
26
|
|
49
|
-
|
50
|
-
|
51
|
-
|
27
|
+
def const_missing(const_name)
|
28
|
+
code = RestfulError.code_from(const_name)
|
29
|
+
return super unless code
|
52
30
|
|
53
|
-
|
31
|
+
@cache[code] ||= build_error_class_for(code)
|
32
|
+
end
|
54
33
|
|
55
|
-
|
56
|
-
ex = env["action_dispatch.exception"]
|
57
|
-
@exception = ex.extend(Helper)
|
58
|
-
ex.restful.set_env(env)
|
59
|
-
@status_code = ex.restful.status_code
|
60
|
-
@reason_phrase = ex.restful.reason_phrase
|
61
|
-
@message = ex.restful.message
|
34
|
+
private
|
62
35
|
|
63
|
-
|
64
|
-
|
36
|
+
def build_error_class_for(code)
|
37
|
+
status = Status.new(code)
|
38
|
+
message = defined?(I18n) ? I18n.t(status.symbol, default: status.reason_phrase, scope: :restful_error) : status.reason_phrase
|
39
|
+
klass = Class.new(BaseError) do
|
40
|
+
define_method(:http_status) { status.code }
|
41
|
+
define_method(:restful) { status }
|
42
|
+
define_method(:message) { message }
|
43
|
+
end
|
44
|
+
const_set(status.const_name, klass)
|
45
|
+
if defined? ActionDispatch::ExceptionWrapper
|
46
|
+
ActionDispatch::ExceptionWrapper.rescue_responses[klass.name] = status.code
|
47
|
+
klass.define_singleton_method(:inherited) do |subclass|
|
48
|
+
ActionDispatch::ExceptionWrapper.rescue_responses[subclass.name] = status.code
|
49
|
+
end
|
50
|
+
end
|
51
|
+
klass
|
65
52
|
end
|
66
53
|
end
|
67
|
-
def self.exceptions_app
|
68
|
-
ExceptionsController.action(:show)
|
69
|
-
end
|
70
54
|
end
|
data/spec/rack_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'action_controller'
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe 'RestfulError' do
|
@@ -11,7 +12,7 @@ describe 'RestfulError' do
|
|
11
12
|
env "action_dispatch.exception", RestfulError[404].new
|
12
13
|
end
|
13
14
|
it do
|
14
|
-
get '/
|
15
|
+
get '/404'
|
15
16
|
expect(last_response.status).to eq 404
|
16
17
|
end
|
17
18
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_controller"
|
4
|
+
require "spec_helper"
|
5
|
+
|
6
|
+
RSpec.describe "RestfulError.exceptions_app" do
|
7
|
+
let(:app) { RestfulError.exceptions_app }
|
8
|
+
let(:env) { {} }
|
9
|
+
let(:request) { Rack::MockRequest.new(app) }
|
10
|
+
|
11
|
+
it "renders 404" do
|
12
|
+
env["action_dispatch.exception"] = ActionController::RoutingError.new("Not Found")
|
13
|
+
env["PATH_INFO"] = "/404"
|
14
|
+
response = request.get("/404", env)
|
15
|
+
expect(response.status).to eq 404
|
16
|
+
expect(response.body).to include "Not Found"
|
17
|
+
end
|
18
|
+
end
|
data/spec/restful_error_spec.rb
CHANGED
@@ -1,24 +1,47 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
2
4
|
|
3
5
|
describe RestfulError do
|
4
|
-
|
5
|
-
|
6
|
+
describe "RestfullError[404]" do
|
7
|
+
subject { described_class[404].new }
|
8
|
+
|
9
|
+
it do
|
10
|
+
expect(subject.http_status).to eq 404
|
11
|
+
expect(subject.restful.reason_phrase).to eq "Not Found"
|
12
|
+
end
|
6
13
|
end
|
7
14
|
|
8
|
-
describe
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
15
|
+
describe "RestfullError[:bad_request]" do
|
16
|
+
subject { described_class[:bad_request].new }
|
17
|
+
|
18
|
+
it do
|
19
|
+
expect(subject.http_status).to eq 400
|
20
|
+
expect(subject.restful.reason_phrase).to eq "Bad Request"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe described_class::Forbidden do
|
25
|
+
subject { described_class.new }
|
26
|
+
|
27
|
+
it do
|
28
|
+
expect(subject.http_status).to eq 403
|
29
|
+
expect(subject.restful.reason_phrase).to eq "Forbidden"
|
16
30
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "custom class" do
|
34
|
+
subject { klass.new }
|
35
|
+
|
36
|
+
let(:klass) do
|
37
|
+
Class.new(StandardError) do
|
38
|
+
include RestfulError::Helper
|
39
|
+
def http_status = 404
|
21
40
|
end
|
22
41
|
end
|
42
|
+
|
43
|
+
it do
|
44
|
+
expect(subject.restful.symbol).to eq :not_found
|
45
|
+
end
|
23
46
|
end
|
24
47
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,105 +1,59 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_error
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kuboon
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: rack
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.1'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
-
-
|
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: '3'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
29
|
+
version: '2.1'
|
30
|
+
- - "<"
|
67
31
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
32
|
+
version: '4'
|
69
33
|
description: Define status_code method on your error. Views and messages are fully
|
70
34
|
customizable on rails way.
|
71
35
|
email:
|
72
|
-
-
|
36
|
+
- o@kbn.one
|
73
37
|
executables: []
|
74
38
|
extensions: []
|
75
39
|
extra_rdoc_files: []
|
76
40
|
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.erb
|
85
|
-
- app/views/restful_error/show.html.haml
|
86
|
-
- app/views/restful_error/show.json.jbuilder
|
87
|
-
- app/views/restful_error/show.xml.erb
|
88
|
-
- config/locales/en.restful_error.yml
|
89
|
-
- config/locales/ja.restful_error.yml
|
90
41
|
- lib/restful_error.rb
|
91
42
|
- lib/restful_error/engine.rb
|
43
|
+
- lib/restful_error/inflector.rb
|
44
|
+
- lib/restful_error/railtie.rb
|
45
|
+
- lib/restful_error/status.rb
|
92
46
|
- lib/restful_error/version.rb
|
93
|
-
- lib/restful_error/wrapper.rb
|
94
|
-
- restful_error.gemspec
|
95
47
|
- spec/rack_spec.rb
|
48
|
+
- spec/railtie_spec.rb
|
96
49
|
- spec/restful_error_spec.rb
|
97
50
|
- spec/spec_helper.rb
|
98
51
|
homepage: https://github.com/kuboon/restful_error
|
99
52
|
licenses:
|
100
53
|
- MIT
|
101
|
-
metadata:
|
102
|
-
|
54
|
+
metadata:
|
55
|
+
rubygems_mfa_required: 'true'
|
56
|
+
post_install_message:
|
103
57
|
rdoc_options: []
|
104
58
|
require_paths:
|
105
59
|
- lib
|
@@ -107,20 +61,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
61
|
requirements:
|
108
62
|
- - ">="
|
109
63
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
64
|
+
version: '3.2'
|
111
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
66
|
requirements:
|
113
67
|
- - ">="
|
114
68
|
- !ruby/object:Gem::Version
|
115
69
|
version: '0'
|
116
70
|
requirements: []
|
117
|
-
|
118
|
-
|
119
|
-
signing_key:
|
71
|
+
rubygems_version: 3.5.22
|
72
|
+
signing_key:
|
120
73
|
specification_version: 4
|
121
74
|
summary: Define your error with status code. Raise it and you will get formatted response
|
122
75
|
with i18nized message.
|
123
|
-
test_files:
|
124
|
-
- spec/rack_spec.rb
|
125
|
-
- spec/restful_error_spec.rb
|
126
|
-
- spec/spec_helper.rb
|
76
|
+
test_files: []
|
data/.gitignore
DELETED
@@ -1,22 +0,0 @@
|
|
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
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
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
DELETED
@@ -1,119 +0,0 @@
|
|
1
|
-
# RestfulError
|
2
|
-
|
3
|
-
[](https://gitter.im/kuboon/restful_error?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
|
-
|
5
|
-
Define your error with status code. Raise it and you will get formatted response with i18nized message.
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
gem 'restful_error'
|
12
|
-
|
13
|
-
And then execute:
|
14
|
-
|
15
|
-
$ bundle
|
16
|
-
|
17
|
-
Load the module in your controller:
|
18
|
-
|
19
|
-
```ruby
|
20
|
-
class ApplicationController < ActionController::Base
|
21
|
-
|
22
|
-
include RestfulError::ActionController
|
23
|
-
```
|
24
|
-
|
25
|
-
## Usage
|
26
|
-
|
27
|
-
### Simple
|
28
|
-
#### raise me
|
29
|
-
```ruby
|
30
|
-
class PostsController < ApplicationController
|
31
|
-
before_action do
|
32
|
-
raise RestfulError[401] unless current_user
|
33
|
-
# or
|
34
|
-
raise RestfulError::Unauthorized unless current_user
|
35
|
-
end
|
36
|
-
end
|
37
|
-
```
|
38
|
-
#### Multi format response
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
get '/posts/new'
|
42
|
-
#=> render 'restful_error/show.html' with @status_code and @message
|
43
|
-
|
44
|
-
post '/posts.json'
|
45
|
-
#=> { status_code: 401, message: "Sign in required"} or write your json at 'restful_error/show.json'
|
46
|
-
|
47
|
-
get '/session.xml'
|
48
|
-
#=> "<error><status_code type="integer">401</status_code><message>Sign in required</message></error>" or write your xml at 'restful_error/show.xml'
|
49
|
-
```
|
50
|
-
|
51
|
-
#### I18n
|
52
|
-
|
53
|
-
```yaml
|
54
|
-
ja:
|
55
|
-
restful_error:
|
56
|
-
unauthorized: ログインしてください #401
|
57
|
-
not_found: ページが存在しません #404
|
58
|
-
```
|
59
|
-
|
60
|
-
### Advanced
|
61
|
-
#### your custom error
|
62
|
-
```ruby
|
63
|
-
class ::NoSession < RestfulError[404]; end
|
64
|
-
# or
|
65
|
-
class ::NoSession < RestfulError::NotFound; end
|
66
|
-
```
|
67
|
-
#### duck typing
|
68
|
-
```ruby
|
69
|
-
class OAuthController < ApplicationController
|
70
|
-
|
71
|
-
# all you need is status_code
|
72
|
-
class RequireTwitterLogin < StandardError
|
73
|
-
def status_code; 401; end
|
74
|
-
end
|
75
|
-
# or
|
76
|
-
class RequireTwitterLogin < StandardError
|
77
|
-
def status_code; :unauthorized; end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
```
|
81
|
-
|
82
|
-
#### library defined error
|
83
|
-
``` ruby
|
84
|
-
config.action_dispatch.rescue_responses["CanCan::Unauthorized"] = 401
|
85
|
-
# or
|
86
|
-
config.action_dispatch.rescue_responses["CanCan::Unauthorized"] = :unauthorized
|
87
|
-
```
|
88
|
-
#### I18n
|
89
|
-
```yaml
|
90
|
-
ja:
|
91
|
-
restful_error:
|
92
|
-
no_session: セッションがありません
|
93
|
-
oauth_controller/require_twitter_login: Twitterログインが必要です
|
94
|
-
can_can/unauthorized: 権限がありません
|
95
|
-
active_record/record_not_found: 要求されたリソースが存在しません
|
96
|
-
```
|
97
|
-
#### custom message
|
98
|
-
|
99
|
-
```ruby
|
100
|
-
class RequireLogin < StandardError
|
101
|
-
def initialize(provider = 'Unknown')
|
102
|
-
@provider = provider
|
103
|
-
end
|
104
|
-
def status_code
|
105
|
-
:unauthorized
|
106
|
-
end
|
107
|
-
def status_message
|
108
|
-
I18n.t('restful_error.require_login', provider: provider)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
```
|
112
|
-
|
113
|
-
## Contributing
|
114
|
-
|
115
|
-
1. Fork it ( https://github.com/kuboon/restful_error/fork )
|
116
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
117
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
118
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
119
|
-
5. Create a new Pull Request
|
data/Rakefile
DELETED
@@ -1,12 +0,0 @@
|
|
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
|
@@ -1,12 +0,0 @@
|
|
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
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module RestfulError
|
2
|
-
class Wrapper
|
3
|
-
def initialize(ex)
|
4
|
-
@ex = ex
|
5
|
-
end
|
6
|
-
def set_env(env)
|
7
|
-
@env = env
|
8
|
-
end
|
9
|
-
def status_code
|
10
|
-
Rack::Utils.status_code(@ex.try(:status_code)).nonzero? || ActionDispatch::ExceptionWrapper.new(@env, @ex).status_code
|
11
|
-
end
|
12
|
-
def reason_phrase
|
13
|
-
RestfulError.reason_phrase(status_code)
|
14
|
-
end
|
15
|
-
def reason_phrase_key
|
16
|
-
reason_phrase.downcase.gsub(/\s|-/, '_').to_sym
|
17
|
-
end
|
18
|
-
def message
|
19
|
-
return message if message = @ex.try(:status_message)
|
20
|
-
I18n.t @ex.class.name.underscore, default: [reason_phrase_key, @ex.class.name], scope: :restful_error
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/restful_error.gemspec
DELETED
@@ -1,26 +0,0 @@
|
|
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 status_code method on your error. Views and messages are fully customizable on rails way.}
|
13
|
-
spec.homepage = "https://github.com/kuboon/restful_error"
|
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', ">= 4.1"
|
22
|
-
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
-
spec.add_development_dependency "rake", '~> 0'
|
25
|
-
spec.add_development_dependency "rspec", "~> 3"
|
26
|
-
end
|