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. The default response is 500 and content-type of text/plain.
Constant Summary collapse
- DEFAULT_STATUS =
500
- DEFAULT_HEADERS =
{'Content-Type'=>'text/plain'}
Instance Method Summary collapse
-
#body ⇒ Object
Unless @body is defined, a self inspect and backtrace is output.
-
#headers ⇒ Object
Returns @headers or DEFAULT_HEADERS if unset.
-
#initialize(data = nil) ⇒ StandardError
constructor
After a bit of experimenting with Exception and raise I determined a fun and simple way to generate fast HTTP response error thingies.
-
#status ⇒ Object
Returns @status or DEFAULT_STATUS if unset.
-
#to_http(raw = true) ⇒ Object
See Maveric::Controller#to_http.
Constructor Details
#initialize(data = nil) ⇒ StandardError
After a bit of experimenting with Exception and raise I determined a fun and simple way to generate fast HTTP response error thingies. As long as you’re within a call to Maveric#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 for Maveric.
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. The 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 appended to the response body.
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/maveric/extensions.rb', line 98 def initialize data=nil # consider autosetting @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 super data end |
Instance Method Details
#body ⇒ Object
Unless @body is defined, a self inspect and backtrace is output.
62 63 64 |
# File 'lib/maveric/extensions.rb', line 62 def body defined?(@body)? @body : [inspect, *backtrace]*"\n" end |
#headers ⇒ Object
Returns @headers or DEFAULT_HEADERS if unset.
57 58 59 |
# File 'lib/maveric/extensions.rb', line 57 def headers defined?(@headers)? @headers : DEFAULT_HEADERS end |
#status ⇒ Object
Returns @status or DEFAULT_STATUS if unset.
52 53 54 |
# File 'lib/maveric/extensions.rb', line 52 def status defined?(@status)? @status : DEFAULT_STATUS end |
#to_http(raw = true) ⇒ Object
See Maveric::Controller#to_http
67 68 69 70 71 72 73 74 |
# File 'lib/maveric/extensions.rb', line 67 def to_http(raw=true) response = if not raw then 'Status:' elsif defined? @env and @env.key? 'HTTP_VERSION' then @env['HTTP_VERSION'] else 'HTTP/1.1'end response += " #{self.status}" + ::Maveric::EOL # Status message? :/ response << self.headers.map{|k,v| "#{k}: #{v}" }*::Maveric::EOL response << ::Maveric::EOL*2 + self.body end |