Skip to content

Don't return full error messages in error response #13

Description

@nikajukic

We had a case where we needed to add some custom error messages on some attributes.

Since this gem used resource.errors.full_messages, error messages would be weird, e.g. "Shipping address some of the line items can't be delivered to this shipping address" instead of just "Some of the line items can't be delivered to this shipping address".

We monkey patched a solution for this:

def error_render_options
    errors = { errors: [] }

    errors[:errors] << { detail: on_error(:detail) } if on_error(:detail)

    resource.errors.messages.each do |attribute, messages|
      messages.each do |message|
        errors[:errors] << error_response(attribute, message)
      end
    end if resource.respond_to?(:errors)

    errors
  end

  def error_response(attribute, message)
    {
      title: I18n.t("json_api.errors.#{status}.title"),
      detail: message.capitalize,
      source: { parameter: attribute, pointer: "data/attributes/#{attribute}" }
    }
  end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions