mtodd / halcyon

JSON Web App Framework [NOT UNDER ACTIVE DEVELOPMENT]

This URL has Read+Write access

halcyon / lib / halcyon / exceptions.rb
fb1fd7ae » mtodd 2007-12-16 Both server and client oper... 1 module Halcyon
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 2
3 # Included into Halcyon::Application in order to provide exception classes
4 # like NotModified, OK, Forbidden, or NotFound to simplify generating error
5 # methods and handling specific error instances.
6 #
7 # It is not intended for these exceptions to be destructive or process-ending
8 # in the least, only to simplify finishing up processing and relaying
9 # appropriate status to the caller/client.
10 #
11 # These classes inherit from StandardError because it is convenient to raise
12 # a given status and let Halcyon's dispatcher handle sending the message to
13 # the client, but it is possible to just instantiate an object without
14 # throwing an exception if necessary.
517cc5a4 » mtodd 2007-12-16 Fixed some problems with RD... 15 module Exceptions #:nodoc:
c2bbb72e » mtodd 2008-01-06 Reorganized the Base except... 16
17 #--
18 # Base Halcyon Exception
19 #++
20
21 class Base < StandardError #:nodoc:
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 22 attr_accessor :status, :body
23 def initialize(status, body)
c2bbb72e » mtodd 2008-01-06 Reorganized the Base except... 24 @status = status
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 25 @body = body
26 super "[#{@status}] #{@body}"
c2bbb72e » mtodd 2008-01-06 Reorganized the Base except... 27 end
28 end
29
30 #--
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 31 # HTTP Status Codes and Errors
c2bbb72e » mtodd 2008-01-06 Reorganized the Base except... 32 #++
33
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 34 HTTP_STATUS_CODES = {
35 100 => 'Continue',
36 101 => 'Switching Protocols',
45982c67 » mtodd 2008-05-27 Added extended HTTP status ... 37 102 => 'Processing',
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 38 200 => 'OK',
39 201 => 'Created',
40 202 => 'Accepted',
41 203 => 'Non-Authoritative Information',
42 204 => 'No Content',
43 205 => 'Reset Content',
44 206 => 'Partial Content',
45982c67 » mtodd 2008-05-27 Added extended HTTP status ... 45 207 => 'Multi-Status',
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 46 300 => 'Multiple Choices',
47 301 => 'Moved Permanently',
48 302 => 'Moved Temporarily',
49 303 => 'See Other',
50 304 => 'Not Modified',
51 305 => 'Use Proxy',
45982c67 » mtodd 2008-05-27 Added extended HTTP status ... 52 307 => 'Temporary Redirect',
f5bdf423 » mtodd 2007-12-30 Fixed dependency on JSON (o... 53 400 => 'Bad Request',
54 401 => 'Unauthorized',
55 402 => 'Payment Required',
56 403 => 'Forbidden',
57 404 => 'Not Found',
58 405 => 'Method Not Allowed',
59 406 => 'Not Acceptable',
60 407 => 'Proxy Authentication Required',
61 408 => 'Request Time-out',
62 409 => 'Conflict',
63 410 => 'Gone',
64 411 => 'Length Required',
65 412 => 'Precondition Failed',
66 413 => 'Request Entity Too Large',
67 414 => 'Request-URI Too Large',
68 415 => 'Unsupported Media Type',
45982c67 » mtodd 2008-05-27 Added extended HTTP status ... 69 416 => 'Requested Range Not Satisfiable',
70 417 => 'Expectation Failed',
71 422 => 'Unprocessable Entity',
72 423 => 'Locked',
73 424 => 'Failed Dependency',
74 425 => 'No Code',
75 426 => 'Upgrade Required',
f5bdf423 » mtodd 2007-12-30 Fixed dependency on JSON (o... 76 500 => 'Internal Server Error',
77 501 => 'Not Implemented',
78 502 => 'Bad Gateway',
79 503 => 'Service Unavailable',
80 504 => 'Gateway Time-out',
45982c67 » mtodd 2008-05-27 Added extended HTTP status ... 81 505 => 'HTTP Version not supported',
82 506 => 'Variant Also Negotiates',
83 507 => 'Insufficient Storage',
84 510 => 'Not Extended'
fb1fd7ae » mtodd 2007-12-16 Both server and client oper... 85 }
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 86
45982c67 » mtodd 2008-05-27 Added extended HTTP status ... 87 # Added extended HTTP status codes found from
88 # http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html
89
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 90 #--
91 # Classify Status Codes
92 #++
93
94 HTTP_STATUS_CODES.to_a.each do |http_status|
95 status, body = http_status
96 class_eval <<-"end;"
97 class #{body.gsub(/( |\-)/,'')} < Halcyon::Exceptions::Base
b8fbb9e4 » mtodd 2008-06-26 Added a simple way to respo... 98 def initialize(body=nil)
99 body = '#{body}' if body.nil?
78a19edb » mtodd 2008-06-17 Made exceptions that inerit... 100 super(#{status}, body)
25c1301a » mtodd 2008-03-09 Added Exceptions back into ... 101 end
102 end
103 end;
104 end
105
fb1fd7ae » mtodd 2007-12-16 Both server and client oper... 106 end
107 end