Sha256: ce31b2abecde2e492819aec676b80cf9227b2050c6991df601813cd3061e08c1
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 KB
Contents
module Spree class ErrorSerializer attr_accessor :resource, :options, :response def initialize(resource, options = {}) @resource = resource @response = options.delete(:response) @options = options end def as_json { errors: error_messages }.to_json end private def resource_error_objects(messages) messages.map do |title, detail| pointer = "data/attributes/#{title.to_s.split('.').join('/')}" error_object title.to_s.gsub('.', ' '), detail.to_sentence, pointer: pointer end end def error_messages case resource when Symbol title_translation = Spree.t("api.errors.#{resource}.title") detail_translation = Spree.t("api.errors.#{resource}.detail") [error_object(title_translation, detail_translation)] when Array resource.map { |errors| resource_error_objects errors }.flatten else resource_error_objects resource.errors.messages end end def error_object(title, detail, pointer: '') error = { detail: detail.gsub("\n", ' ').strip, meta: options, title: title.to_s.titleize } error.merge!(response_attributes) if response.present? pointer.present? ? error.merge(pointer: pointer) : error end def response_attributes { status: response.status_message, code: response.code } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
solidus_json_api-0.3.1 | app/serializers/spree/error_serializer.rb |
solidus_json_api-0.3.0 | app/serializers/spree/error_serializer.rb |