Improve API Error Rendering for Nested/Child/Relations
The following discussion from !66793 (merged) should be addressed:
-
@leetickett started a discussion: I want to raise a follow up to look at error handling. At the moment, if we try and create a timelog with a summary > 255 characters the error looks like:
{ "message": { "timelogs": [ "is invalid" ] } }
I've been doing some messing, and it's definitely possible to get "a better" error. But I feel:
- it's outside the scope of this MR
- it would be best if we could introduce something generic/dynamic
Here are my ramblings for follow up:
(byebug) model.errors #<ActiveModel::Errors:0x0000560a99906bc8 @base=#<Issue id:18 gitlab-org/gitlab-shell#8>, @errors=[#<ActiveModel::Error attribute=timelogs, type=invalid, options={}>]> (byebug) model.errors.errors.each { |error| puts error.full_message } Timelogs is invalid [#<ActiveModel::Error attribute=timelogs, type=invalid, options={}>] (byebug) model.timelogs.last.errors.errors [#<ActiveModel::Error attribute=summary, type=too_long, options={:count=>255}>] def all_error_messages(model) model.errors.each do |column, errors| if model.send(:"#{column}").respond_to?(:each) invalids = model.send(:"#{column}").reject(&:valid?) invalids.each do |invalid| invalid.errors.each do |error| model.errors.add(error.attribute, error.message) end end elsif model.send(:"#{column}").respond_to?(:errors) end end end