Exception: StandardError
- Inherits:
-
Exception
- Object
- Exception
- StandardError
- Defined in:
- lib/maveric/extensions.rb
Overview
Extensions to StandardError to enable their usage in Maveric as responses.
Constant Summary collapse
- DEFAULT_STATUS =
500
- DEFAULT_HEADERS =
{'Content-Type'=>'text/plain'}
Instance Attribute Summary collapse
-
#body ⇒ Object
Unless @body is defined, a self inspect and backtrace is output.
- #headers ⇒ Object
- #status ⇒ Object
Instance Method Summary collapse
-
#initialize(data = nil) ⇒ StandardError
constructor
After a bit of experimenting with Exception and raise I determinee a fun and simple way to generate fast HTTP response error thingies.
- #old_init ⇒ Object
-
#to_http ⇒ Object
See ::Maveric::Controller#to_http.
Constructor Details
#initialize(data = nil) ⇒ StandardError
After a bit of experimenting with Exception and raise I determinee a fun and simple way to generate fast HTTP response error thingies. As long as you’re within a call to dispatch (with no further rescue clauses) the StandardError will propogate up and be returned. As a StandardError has similar accessors as a Controller, they should be compatible with any outputting implimentation.
raise StandardErrpr; # 500 error
raise StandardError, 'Crap!'; # 500 error with message set to 'Crap!'
raise StandardError, [status, headers, body, *other_data] # Magic!
In the final example line an Array is passed to raise as a second argument rather than a String. This is taken as the HTTP status code. The next element is tested to be a Hash, if so then it’s values are merged into the HTTP headers. Then next item is tested to be a String, if so it is appended to the response body. All remaining elements are appended to the response body in inspect format.
A simple way of only including data that might be interpreted to being the status, headers, or body is to place a nil previous to the data you want apended to the response body.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/maveric/extensions.rb', line 77 def initialize data=nil #consider autosettign @status, like 503 for NoMethodError if data.is_a? Array and data[0].is_a? Integer @body = '' @status = data.shift if Integer === data.first @headers = DEF_HEADERS.merge data.shift if Hash === data.first @body << data.shift if String === data.first msg = @body.dup @body << "\n\n#{data.compact.map{|e|e.inspect}*"\n"}" unless data.empty? data = msg end old_init data end |
Instance Attribute Details
#body ⇒ Object
Unless @body is defined, a self inspect and backtrace is output.
47 |
# File 'lib/maveric/extensions.rb', line 47 def body; @body or [inspect, *backtrace]*"\n"; end |
#headers ⇒ Object
45 |
# File 'lib/maveric/extensions.rb', line 45 def headers; @headers or DEFAULT_HEADERS; end |
#status ⇒ Object
44 |
# File 'lib/maveric/extensions.rb', line 44 def status; @status or DEFAULT_STATUS; end |
Instance Method Details
#old_init ⇒ Object
54 |
# File 'lib/maveric/extensions.rb', line 54 alias_method :old_init, :initialize |
#to_http ⇒ Object
See ::Maveric::Controller#to_http
49 50 51 52 53 |
# File 'lib/maveric/extensions.rb', line 49 def to_http response = "Status: #{status}"+::Maveric::EOL response << headers.map{|(k,v)| "#{k}: #{v}" }*::Maveric::EOL response << ::Maveric::EOL*2 + body end |