nut 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19def3f8c723375cf6a2710b6d6dc1fdd5ce0e5b
4
- data.tar.gz: d70138e71b04c33cae1bab25ca8c771157e17aaf
3
+ metadata.gz: 2874b58315b705be099b8b9da8ff696994df0cd6
4
+ data.tar.gz: ea95d1b9adcb54b33a6ce4e6b5e379471a9c96c5
5
5
  SHA512:
6
- metadata.gz: 6945558f671b19235c41f31ea5ead0099655f3cd265e71c50d87a83ee05d86c019fab9b372fd314833e8361d3f396f4d10100d21e65255e405b1e653ef4f3092
7
- data.tar.gz: 5187aa6fba2b0e888956c2ef2876d98d5b2e3aec2ac0e65f9ec1ca9216d8fea86ddd5af8c919d601208300203e6f81fb503aaeb0f7f034da00ec7326a6f73a5c
6
+ metadata.gz: e4dde8c5cdd60bc7fea8789efc11129dddc3759ec339b209620d5e267839b62df3b7b2c23500c740b95b2d33f43373d7d3d37d2dfe434f4502e929bf7f730cd2
7
+ data.tar.gz: 00dfe351ef2919bc747e8b37c1ec6b7237f721603576594bac9d5ab3fd28dce10170476e69119a1d9d3bd097cfbc545dce8ceaabc3e3ceaadfc135f78103ea80
data/README.md CHANGED
@@ -159,6 +159,26 @@ end
159
159
  n = Nut::Service.new ExampleRequestHandler
160
160
  ```
161
161
 
162
+ ### Handling Errors
163
+
164
+ When an exception is raised inside the *handle* method, Nut will catch it and immediately throw up a *500 Internal Server Error*, with a description of the exception in the response body.
165
+ While this is suitable for testing / debugging, production apps should overload this behaviour to serve a less 'verbose' error page.
166
+ To overload the behaviour, simply define an *on_err* method in the Request Handler Module:
167
+
168
+ ```ruby
169
+ # Request Handler Module
170
+ module ExampleRequestHandler
171
+ def handle request, response
172
+ # ...
173
+ end
174
+
175
+ def on_err exception, request, response
176
+ response[:code] = 500
177
+ response[:body] = "Something went wrong: #{exception}"
178
+ end
179
+ end
180
+ ```
181
+
162
182
  ### Best Practice
163
183
 
164
184
  While anything is possible, the recommended pattern for using Nut is to extend the Nut Service class, and embed the Request Handler Module inside the new Service Class.
data/lib/nut/codes.rb CHANGED
@@ -20,6 +20,8 @@ module Nut
20
20
  301 => 'Moved',
21
21
  302 => 'Found',
22
22
  404 => 'Not Found',
23
- 501 => 'Not Implemented'
23
+ 500 => 'Internal Server Error',
24
+ 501 => 'Not Implemented',
25
+ 505 => 'HTTP Version Not Supported'
24
26
  }
25
27
  end
data/lib/nut/handler.rb CHANGED
@@ -158,8 +158,21 @@ module Nut
158
158
  # Prepare Response
159
159
  response = { body: '' }
160
160
 
161
- # Pass to Request Handler
162
- client[:serv].request_handler.handle request, response
161
+ # Acquire Request Handler
162
+ rh = client[:serv].request_handler
163
+
164
+ # Begin
165
+ begin
166
+
167
+ # Pass to Request Handler
168
+ rh.handle request, response
169
+
170
+ # Rescue (Catch Exceptions raised during request handling)
171
+ rescue Exception => e
172
+
173
+ # Handle Error
174
+ (rh.respond_to?(:on_err) ? rh : self).on_err e, request, response
175
+ end
163
176
 
164
177
  # Respond to Client
165
178
  serve_response client, response
@@ -173,5 +186,21 @@ module Nut
173
186
  # Send out Response String
174
187
  write client, Response.build_response(response)
175
188
  end
189
+
190
+ # Default Error Handler:
191
+ # Throws up a 500 Internal Server Error, with a description of the exception in the response body.
192
+ # @param [Exception] ex An exception that was raised inside the handle method
193
+ # @param [Hash] request Request Hash
194
+ # @param [Hash] response Response Hash
195
+ def self.on_err ex, request, response
196
+
197
+ # Throw up a 500 Internal Server Error
198
+ response[:code] = 500
199
+
200
+ # Spit out Exception in Response Body
201
+ response[:body] << '<h1>Internal Server Error</h1>'
202
+ response[:body] << "<h2>#{ex}</h2>"
203
+ ex.backtrace.each { |b| response[:body] << " -> #{b}<br/>" }
204
+ end
176
205
  end
177
206
  end
data/lib/nut/version.rb CHANGED
@@ -5,5 +5,5 @@
5
5
  module Nut
6
6
 
7
7
  # Version
8
- VERSION = '1.0.0'
8
+ VERSION = '1.1.0'
9
9
  end
data/nut.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency 'bundler'
21
21
  spec.add_development_dependency 'rake'
22
22
  spec.add_runtime_dependency 'minitest'
23
+ spec.add_runtime_dependency 'rest-client'
23
24
  spec.add_runtime_dependency 'aromat'
24
25
  spec.add_runtime_dependency 'typor'
25
26
  spec.add_runtime_dependency 'rxio'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nut
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eresse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-02 00:00:00.000000000 Z
11
+ date: 2017-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: aromat
57
71
  requirement: !ruby/object:Gem::Requirement