moonrope 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef945cc15907ff80fe15bc8d5e8cd01c439183e7
4
- data.tar.gz: 29682cb87255200fb7c52dbec08c0e7b18ce6292
3
+ metadata.gz: 029f11de28ff6aad99782e6afa1cd9b63fd5a728
4
+ data.tar.gz: 7534e31e1f74e53908750560a9939a1c1052d43a
5
5
  SHA512:
6
- metadata.gz: 16c45bb324804dc8180a2e71940a952d50cb1c68464df6d619d1bef1b3beb63aa1557647f70a4c5b0294f0f7ba733ac7cf1ec9648e6e37ee1fabe517967c5a57
7
- data.tar.gz: c7b40e7fe75f15ca75894c2df47a3db6d6a6dcc3c672c0d084e9a95711a5ea09c7ee9165d14e5946cad1093a7514448ce1920b536f5d15cd17cc0f507819f0f1
6
+ metadata.gz: e52ac061f8085db1f3ca79dba71a0b33f7e8d5228a3d612b637ea7c8683abdd40ac808f1532469938f17cd6c50745d9161a81968563c6224e122b7434244acc8
7
+ data.tar.gz: 63674f861d4a4d5777df6f94079863c6f5ea4b7a62a39d3dde9a03d7bb6c08698cc4803bf1dc7d082491ffc4682b715918bba3d91ac50d22a61a0128217fe8a2
@@ -49,6 +49,32 @@ module Moonrope
49
49
  end
50
50
  end
51
51
 
52
+ #
53
+ # Execute a block of code and catch approprite Moonrope errors and return
54
+ # a result.
55
+ #
56
+ def convert_errors_to_action_result(&block)
57
+ begin
58
+ yield block
59
+ rescue => exception
60
+ case exception
61
+ when Moonrope::Errors::RequestError
62
+ result = ActionResult.new(self)
63
+ result.status = exception.status
64
+ result.data = exception.data
65
+ result
66
+ else
67
+ if error_block = @controller.base.external_errors[exception.class]
68
+ result = ActionResult.new(self)
69
+ error_block.call(exception, result)
70
+ result
71
+ else
72
+ raise
73
+ end
74
+ end
75
+ end
76
+ end
77
+
52
78
  #
53
79
  # Executes the action and returns a ActionResult object with the result
54
80
  # of the action.
@@ -75,7 +101,7 @@ module Moonrope
75
101
  #
76
102
  eval_environment.action = self
77
103
 
78
- begin
104
+ convert_errors_to_action_result do
79
105
  #
80
106
  # Validate the parameters
81
107
  #
@@ -104,12 +130,6 @@ module Moonrope
104
130
 
105
131
  # Return the result object
106
132
  result
107
-
108
- rescue Moonrope::Errors::RequestError => e
109
- result = ActionResult.new(self)
110
- result.status = e.status
111
- result.data = e.data
112
- result
113
133
  end
114
134
  end
115
135
 
@@ -158,6 +178,6 @@ module Moonrope
158
178
  end
159
179
  true
160
180
  end
161
-
181
+
162
182
  end
163
183
  end
data/lib/moonrope/base.rb CHANGED
@@ -126,5 +126,23 @@ module Moonrope
126
126
  matched_helpers.first
127
127
  end
128
128
 
129
+ #
130
+ # Return all the external errors which are registered for this base
131
+ #
132
+ # @return [Hash] a hash of external errors
133
+ #
134
+ def external_errors
135
+ @external_errors ||= {}
136
+ end
137
+
138
+ #
139
+ # Register a new external error
140
+ #
141
+ # @param error_class [Class] a class which should be caught
142
+ #
143
+ def register_external_error(error_class, &block)
144
+ self.external_errors[error_class] = block
145
+ end
146
+
129
147
  end
130
148
  end
@@ -21,6 +21,14 @@ module Moonrope
21
21
  Moonrope::Request.path_regex = app.config.moonrope_request_path_regex
22
22
  end
23
23
 
24
+ # Catch ActiveRecord::RecordNotFound exception as a standard not-found error
25
+ if defined?(ActiveRecord)
26
+ app.config.moonrope.register_external_error ActiveRecord::RecordNotFound do |exception, result|
27
+ result.status = 'not-found'
28
+ result.data = {:message => exception.message}
29
+ end
30
+ end
31
+
24
32
  # Insert the Moonrope middleware into the application's middleware
25
33
  # stack (at the bottom).
26
34
  app.middleware.use(
@@ -82,7 +82,7 @@ module Moonrope
82
82
  def execute
83
83
  eval_env = EvalEnvironment.new(@base, self)
84
84
  if @base.authenticator
85
- begin
85
+ result = action.convert_errors_to_action_result do
86
86
  @authenticated_user = eval_env.instance_eval(&@base.authenticator)
87
87
  # If we are authenticated, check whether the action permits access to
88
88
  # this user, if not raise an error.
@@ -91,11 +91,11 @@ module Moonrope
91
91
  raise Moonrope::Errors::AccessDenied, "Access to #{controller.name}/#{action.name} is not permitted."
92
92
  end
93
93
  end
94
- rescue Moonrope::Errors::RequestError => e
95
- @authenticated_user ||= false # set authenticated user to false if they don't exist
96
- result = Moonrope::ActionResult.new(self)
97
- result.status = e.status
98
- result.data = e.data
94
+ end
95
+
96
+ if result.is_a?(Moonrope::ActionResult)
97
+ # If we already have a result, we should return it and no longer execute
98
+ # this request.
99
99
  return result
100
100
  end
101
101
  end
@@ -1,3 +1,3 @@
1
1
  module Moonrope
2
- VERSION = '1.1.1'
2
+ VERSION = '1.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonrope
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke