rasti-web 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rasti/web.rb +1 -0
- data/lib/rasti/web/controller.rb +12 -5
- data/lib/rasti/web/version.rb +1 -1
- data/rasti-web.gemspec +2 -0
- data/spec/controller_spec.rb +24 -6
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8be22c6b9730ecebfc6deb7449fc33fc580eedfa
|
4
|
+
data.tar.gz: 260d7b82a6a975f7872c0e6d5a2cff49f526272f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2255e469c1232c3f66a83e17aea842e34f55d51e7c23656be60f30b90785c864e08aed79dd6f6a6f7ebc72256ab95bfc218c6a077418ec0c09c3821e16fdc119
|
7
|
+
data.tar.gz: ee292a32f9ec7142d1bb0ab386d635fb128bcc277e44bda50430761b183d361d699bdce02e531766bf5513a4ae08011081ddda445c1258146387e95af144a4c8
|
data/lib/rasti/web.rb
CHANGED
data/lib/rasti/web/controller.rb
CHANGED
@@ -24,8 +24,9 @@ module Rasti
|
|
24
24
|
begin
|
25
25
|
controller.public_send action_name
|
26
26
|
rescue => ex
|
27
|
-
|
28
|
-
|
27
|
+
exception_class = handled_exceptions.detect { |klass| ex.is_a? klass }
|
28
|
+
if exception_class
|
29
|
+
controller.instance_exec ex, &exception_handlers[exception_class]
|
29
30
|
else
|
30
31
|
raise ex
|
31
32
|
end
|
@@ -35,10 +36,16 @@ module Rasti
|
|
35
36
|
|
36
37
|
alias_method :>>, :action
|
37
38
|
|
39
|
+
def exception_handlers
|
40
|
+
@exception_handlers ||= superclass.respond_to?(:exception_handlers) ? superclass.exception_handlers : {}
|
41
|
+
end
|
42
|
+
|
43
|
+
def handled_exceptions
|
44
|
+
@handled_exceptions ||= ClassAncestrySort.desc exception_handlers.keys
|
45
|
+
end
|
46
|
+
|
38
47
|
def rescue_from(exception_class, &block)
|
39
|
-
|
40
|
-
instance_exec ex, &block
|
41
|
-
end
|
48
|
+
exception_handlers[exception_class] = block
|
42
49
|
end
|
43
50
|
end
|
44
51
|
|
data/lib/rasti/web/version.rb
CHANGED
data/rasti-web.gemspec
CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency 'class_config', '~> 0.0.2'
|
25
25
|
spec.add_dependency 'hash_ext', '~> 0.1'
|
26
26
|
spec.add_dependency 'content-type', '~> 0.0'
|
27
|
+
spec.add_dependency 'class_ancestry_sort', '~> 0.1'
|
27
28
|
|
28
29
|
spec.add_development_dependency 'bundler', '~> 1.12'
|
29
30
|
spec.add_development_dependency 'rake', '~> 11.0'
|
@@ -39,5 +40,6 @@ Gem::Specification.new do |spec|
|
|
39
40
|
spec.add_development_dependency 'term-ansicolor', '~> 1.3.0'
|
40
41
|
spec.add_development_dependency 'tins', '~> 1.6.0'
|
41
42
|
spec.add_development_dependency 'json', '~> 1.8'
|
43
|
+
spec.add_development_dependency 'parslet', '~> 1.7.0'
|
42
44
|
end
|
43
45
|
end
|
data/spec/controller_spec.rb
CHANGED
@@ -11,13 +11,22 @@ class TestController < Rasti::Web::Controller
|
|
11
11
|
raise 'Unexpected error'
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
raise CustomError, '
|
14
|
+
def explicit_fail
|
15
|
+
raise CustomError, 'Explicit error'
|
16
|
+
end
|
17
|
+
|
18
|
+
def implicit_fail
|
19
|
+
raise EOFError, 'Implicit error'
|
16
20
|
end
|
17
21
|
|
18
22
|
rescue_from CustomError do |ex|
|
19
23
|
render.status 500, ex.message
|
20
24
|
end
|
25
|
+
|
26
|
+
rescue_from IOError do |ex|
|
27
|
+
render.status 500, ex.message
|
28
|
+
end
|
29
|
+
|
21
30
|
end
|
22
31
|
|
23
32
|
describe Rasti::Web::Controller do
|
@@ -38,13 +47,22 @@ describe Rasti::Web::Controller do
|
|
38
47
|
error.message.must_equal "Undefined action 'invalid' in TestController"
|
39
48
|
end
|
40
49
|
|
41
|
-
it 'Rescue exception' do
|
42
|
-
action = TestController.action :
|
43
|
-
env = Rack::MockRequest.env_for '/
|
50
|
+
it 'Rescue explicit exception' do
|
51
|
+
action = TestController.action :explicit_fail
|
52
|
+
env = Rack::MockRequest.env_for '/explicit_fail'
|
53
|
+
status, headers, response = action.call env
|
54
|
+
|
55
|
+
status.must_equal 500
|
56
|
+
response.body.must_equal ['Explicit error']
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'Rescue implicit exception' do
|
60
|
+
action = TestController.action :implicit_fail
|
61
|
+
env = Rack::MockRequest.env_for '/implicit_fail'
|
44
62
|
status, headers, response = action.call env
|
45
63
|
|
46
64
|
status.must_equal 500
|
47
|
-
response.body.must_equal ['
|
65
|
+
response.body.must_equal ['Implicit error']
|
48
66
|
end
|
49
67
|
|
50
68
|
it 'Unexpected exception' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rasti-web
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: class_ancestry_sort
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.1'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.1'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: bundler
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|