Module: Railsful::Interceptors::Errors
- Included in:
- Serializer
- Defined in:
- lib/railsful/interceptors/errors.rb
Overview
This interceptor checks the given json object for an ‘errors’ array and checks if any errors are available.
Instance Method Summary collapse
-
#errors(raw_errors) ⇒ Object
Transform error output format into more “jsonapi” like.
-
#errors?(options) ⇒ Boolean
Checks if given renderable is an ActiveModel::Error.
- #errors_options(options) ⇒ Object
- #field_errors(field, array) ⇒ Object
-
#formatted_error(hash, field) ⇒ Object
Format the error by adding additional status and field information.
- #render(options) ⇒ Object
Instance Method Details
#errors(raw_errors) ⇒ Object
Transform error output format into more “jsonapi” like.
25 26 27 28 29 30 31 32 33 |
# File 'lib/railsful/interceptors/errors.rb', line 25 def errors(raw_errors) errors = [] raw_errors.details.each do |field, array| errors += field_errors(field, array) end errors end |
#errors?(options) ⇒ Boolean
Checks if given renderable is an ActiveModel::Error
:reek:UtilityFunction
54 55 56 57 58 |
# File 'lib/railsful/interceptors/errors.rb', line 54 def errors?() return false unless .fetch(:json, nil).is_a?(ActiveModel::Errors) end |
#errors_options(options) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/railsful/interceptors/errors.rb', line 14 def () return unless errors?() # Fetch all the errors from the passed json value. errors = errors(.fetch(:json)) # Overwrite the json value and set the errors array. .merge(json: { errors: errors }) end |
#field_errors(field, array) ⇒ Object
35 36 37 38 39 |
# File 'lib/railsful/interceptors/errors.rb', line 35 def field_errors(field, array) array.map do |hash| formatted_error(hash, field) end end |
#formatted_error(hash, field) ⇒ Object
Format the error by adding additional status and field information.
:reek:UtilityFunction
44 45 46 47 48 49 |
# File 'lib/railsful/interceptors/errors.rb', line 44 def formatted_error(hash, field) { status: '422', field: field }.merge(hash) end |
#render(options) ⇒ Object
10 11 12 |
# File 'lib/railsful/interceptors/errors.rb', line 10 def render() super(()) end |