File tree Expand file tree Collapse file tree 4 files changed +22
-16
lines changed
Expand file tree Collapse file tree 4 files changed +22
-16
lines changed Original file line number Diff line number Diff line change @@ -26,13 +26,13 @@ RestfulError::BaseError === ex # => true
2626
2727RestfulError [404 ] == RestfulError ::NotFound # => true # same class
2828
29- ex.restful # returns Data about status code
29+ ex.status_data # returns Data about status code
3030# => #<data RestfulError::Status
3131# code=404,
3232# reason_phrase="Not Found",
3333# symbol=:not_found,
3434# const_name="NotFound">
35- ex.restful .code # => 404
35+ ex.status_data .code # => 404
3636```
3737
3838#### Custom error by subclassing
@@ -49,7 +49,7 @@ class User::PermissionError < StandardError
4949 include RestfulError ::Helper
5050 def http_status = :unauthorized # or 401
5151end
52- User ::PermissionError .new .restful .reason_phrase # => "Unauthorized"
52+ User ::PermissionError .new .status_data .reason_phrase # => "Unauthorized"
5353```
5454
5555### With I18n
Original file line number Diff line number Diff line change @@ -11,20 +11,20 @@ module RestfulError
1111 autoload :ExceptionsController , "restful_error/exceptions_controller"
1212
1313 module Helper
14- def restful
15- @restful ||= begin
16- raise NotImplementedError , "http_status must be implemented by including class" unless respond_to? ( :http_status )
17- RestfulError . build_status_from_symbol_or_code ( http_status )
18- end
14+ def status_data
15+ @status_data ||= RestfulError . build_status_from_symbol_or_code ( http_status )
1916 end
2017 def response_message
2118 return @response_message unless @response_message . nil?
22- @response_message = RestfulError . localized_phrase ( self . class . name , restful )
19+ @response_message = RestfulError . localized_phrase ( self . class . name , status_data )
2320 end
2421 end
2522
2623 class BaseError < StandardError
2724 include RestfulError ::Helper
25+ def http_status
26+ raise NotImplementedError , "http_status must be implemented"
27+ end
2828 end
2929
3030 @cache = { }
@@ -58,7 +58,7 @@ def localized_phrase(class_name, status)
5858 def build_error_class_for ( status )
5959 klass = Class . new ( BaseError ) do
6060 define_method ( :http_status ) { status . code }
61- define_method ( :restful ) { status }
61+ define_method ( :status_data ) { status }
6262 end
6363 const_set ( status . const_name , klass )
6464 if defined? ActionDispatch ::ExceptionWrapper
Original file line number Diff line number Diff line change 88
99 it do
1010 expect ( subject . http_status ) . to eq 404
11- expect ( subject . restful . reason_phrase ) . to eq "Not Found"
11+ expect ( subject . status_data . reason_phrase ) . to eq "Not Found"
1212 end
1313 end
1414
1717
1818 it do
1919 expect ( subject . http_status ) . to eq 400
20- expect ( subject . restful . reason_phrase ) . to eq "Bad Request"
20+ expect ( subject . status_data . reason_phrase ) . to eq "Bad Request"
2121 end
2222 end
2323
2626
2727 it do
2828 expect ( subject . http_status ) . to eq 403
29- expect ( subject . restful . reason_phrase ) . to eq "Forbidden"
29+ expect ( subject . status_data . reason_phrase ) . to eq "Forbidden"
3030 end
3131 end
3232
@@ -41,7 +41,7 @@ def http_status = 404
4141 end
4242
4343 it do
44- expect ( subject . restful . symbol ) . to eq :not_found
44+ expect ( subject . status_data . symbol ) . to eq :not_found
4545 end
4646 end
4747end
Original file line number Diff line number Diff line change @@ -43,9 +43,15 @@ def app = RestfulError::ExceptionsApp.new
4343 end
4444 end
4545 context 'custom message' do
46- let ( :exception ) { described_class . new ( "custom message" ) }
46+ let ( :klass ) do
47+ Class . new ( described_class ) do
48+ def response_message = "custom message"
49+ end
50+ end
51+ let ( :exception ) { klass . new }
4752 it do
48- expect ( json ) . to eq ( { status_code :, reason_phrase : "Not Found" , response_message : "custom message" } . stringify_keys )
53+ expect ( json ) . to eq ( { status_code : 404 , reason_phrase : "Not Found" , response_message : 'custom message' } . stringify_keys )
54+ expect ( last_response . status ) . to eq status_code
4955 end
5056 end
5157 end
You can’t perform that action at this time.
0 commit comments