Module: JsonCrudApi::JsonErrors
- Included in:
- API
- Defined in:
- lib/json-crud-api/json_errors.rb
Instance Method Summary collapse
- #add_error(code, message, reference = nil) ⇒ Object
- #clear_errors ⇒ Object
- #fail_forbidden ⇒ Object
- #fail_not_found ⇒ Object
- #fail_unauthorized ⇒ Object
- #fail_with_error(status, code, message, reference = nil) ⇒ Object
- #fail_with_errors(status = 422) ⇒ Object
Instance Method Details
#add_error(code, message, reference = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/json-crud-api/json_errors.rb', line 10 def add_error(code, , reference = nil) @errors = [] if @errors.nil? error = { :code => code, :message => , } error[:reference] = reference unless reference.nil? @errors.push error end |
#clear_errors ⇒ Object
6 7 8 |
# File 'lib/json-crud-api/json_errors.rb', line 6 def clear_errors @errors = [] end |
#fail_forbidden ⇒ Object
41 42 43 |
# File 'lib/json-crud-api/json_errors.rb', line 41 def fail_forbidden fail_with_error 403, 'FORBIDDEN','The user is not allowed to perform this operation on the resource.' end |
#fail_not_found ⇒ Object
33 34 35 |
# File 'lib/json-crud-api/json_errors.rb', line 33 def fail_not_found fail_with_errors 404 end |
#fail_unauthorized ⇒ Object
37 38 39 |
# File 'lib/json-crud-api/json_errors.rb', line 37 def fail_with_error 401, 'UNAUTHORIZED','Authorization is required to perform this operation on the resource.' end |
#fail_with_error(status, code, message, reference = nil) ⇒ Object
21 22 23 24 |
# File 'lib/json-crud-api/json_errors.rb', line 21 def fail_with_error(status, code, , reference = nil) add_error code,,reference fail_with_errors status end |
#fail_with_errors(status = 422) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/json-crud-api/json_errors.rb', line 26 def fail_with_errors(status = 422) halt status, JSON.fast_generate({ :success => false, :errors => @errors }) end |