|
| 1 | +module JSONAPI |
| 2 | + module Utils |
| 3 | + module Exceptions |
| 4 | + class ActiveRecord < ::JSONAPI::Exceptions::Error |
| 5 | + attr_reader :object, :resource, :relationships, :relationship_names, :foreign_keys |
| 6 | + |
| 7 | + # Construct an error decorator over ActiveRecord objects. |
| 8 | + # |
| 9 | + # @param object [ActiveRecord::Base] Invalid ActiveRecord object. |
| 10 | + # e.g.: User.new(name: nil).tap(&:save) |
| 11 | + # |
| 12 | + # @param resource_klass [JSONAPI::Resource] Resource class to be used for reflection. |
| 13 | + # e.g.: UserResuource |
| 14 | + # |
| 15 | + # @return [JSONAPI::Utils::Exceptions::ActiveRecord] |
| 16 | + # |
| 17 | + # @api public |
| 18 | + def initialize(object, resource_klass, context) |
| 19 | + @object = object |
| 20 | + @resource = resource_klass.new(object, context) |
| 21 | + |
| 22 | + # Need to reflect on resource's relationships for error reporting. |
| 23 | + @relationships = resource_klass._relationships.values |
| 24 | + @relationship_names = @relationships.map(&:name).map(&:to_sym) |
| 25 | + @foreign_keys = @relationships.map(&:foreign_key).map(&:to_sym) |
| 26 | + @resource_key_for = {} |
| 27 | + @formatted_key = {} |
| 28 | + end |
| 29 | + |
| 30 | + # Decorate errors for AR invalid objects. |
| 31 | + # |
| 32 | + # @note That's the method used by formatters to build the response's error body. |
| 33 | + # |
| 34 | + # @return [Array] |
| 35 | + # |
| 36 | + # @api public |
| 37 | + def errors |
| 38 | + object.errors.messages.flat_map do |field, messages| |
| 39 | + messages.map.with_index do |message, index| |
| 40 | + build_error(field, message, index) |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + |
| 47 | + # Turn AR error into JSONAPI::Error. |
| 48 | + # |
| 49 | + # @param field [Symbol] Name of the invalid field |
| 50 | + # e.g.: :title |
| 51 | + # |
| 52 | + # @param message [String] Error message |
| 53 | + # e.g.: "can't be blank" |
| 54 | + # |
| 55 | + # @param index [Integer] Index of the error detail |
| 56 | + # |
| 57 | + # @return [JSONAPI::Error] |
| 58 | + # |
| 59 | + # @api private |
| 60 | + def build_error(field, message, index = 0) |
| 61 | + error = error_base |
| 62 | + .merge( |
| 63 | + id: id_member(field, index), |
| 64 | + title: message, |
| 65 | + detail: detail_member(field, message) |
| 66 | + ).merge(source_member(field)) |
| 67 | + JSONAPI::Error.new(error) |
| 68 | + end |
| 69 | + |
| 70 | + # Build the "id" member value for the JSON API error object. |
| 71 | + # e.g.: for :first_name, :too_short => "first-name#too-short" |
| 72 | + # |
| 73 | + # @note The returned value depends on the key formatter type defined |
| 74 | + # via configuration, e.g.: config.json_key_format = :dasherized_key |
| 75 | + # |
| 76 | + # @param field [Symbol] Name of the invalid field |
| 77 | + # e.g.: :first_name |
| 78 | + # |
| 79 | + # @param index [Integer] Index of the error detail |
| 80 | + # |
| 81 | + # @return [String] |
| 82 | + # |
| 83 | + # @api private |
| 84 | + def id_member(field, index) |
| 85 | + [ |
| 86 | + key_format(field), |
| 87 | + key_format( |
| 88 | + object.errors.details |
| 89 | + .dig(field, index, :error) |
| 90 | + .to_s.downcase |
| 91 | + .split |
| 92 | + .join('_') |
| 93 | + ) |
| 94 | + ].join('#') |
| 95 | + end |
| 96 | + |
| 97 | + # Bring the formatted resource key for a given field. |
| 98 | + # e.g.: for :first_name => :"first-name" |
| 99 | + # |
| 100 | + # @note The returned value depends on the key formatter type defined |
| 101 | + # via configuration, e.g.: config.json_key_format = :dasherized_key |
| 102 | + # |
| 103 | + # @param field [Symbol] Name of the invalid field |
| 104 | + # e.g.: :title |
| 105 | + # |
| 106 | + # @return [Symbol] |
| 107 | + # |
| 108 | + # @api private |
| 109 | + def key_format(field) |
| 110 | + @formatted_key[field] ||= JSONAPI.configuration |
| 111 | + .key_formatter |
| 112 | + .format(resource_key_for(field)) |
| 113 | + .to_sym |
| 114 | + end |
| 115 | + |
| 116 | + # Build the "source" member value for the JSON API error object. |
| 117 | + # e.g.: :title => "/data/attributes/title" |
| 118 | + # |
| 119 | + # @param field [Symbol] Name of the invalid field |
| 120 | + # e.g.: :title |
| 121 | + # |
| 122 | + # @return [Hash] |
| 123 | + # |
| 124 | + # @api private |
| 125 | + def source_member(field) |
| 126 | + resource_key = resource_key_for(field) |
| 127 | + return {} unless field == :base || resource.fetchable_fields.include?(resource_key) |
| 128 | + id = key_format(field) |
| 129 | + |
| 130 | + pointer = |
| 131 | + if field == :base then '/data' |
| 132 | + elsif relationship_names.include?(resource_key) then "/data/relationships/#{id}" |
| 133 | + else "/data/attributes/#{id}" |
| 134 | + end |
| 135 | + |
| 136 | + { source: { pointer: pointer } } |
| 137 | + end |
| 138 | + |
| 139 | + # Build the "detail" member value for the JSON API error object. |
| 140 | + # e.g.: :first_name, "can't be blank" => "First name can't be blank" |
| 141 | + # |
| 142 | + # @param field [Symbol] Name of the invalid field |
| 143 | + # e.g.: :first_name |
| 144 | + # |
| 145 | + # @return [String] |
| 146 | + # |
| 147 | + # @api private |
| 148 | + def detail_member(field, message) |
| 149 | + return message if field == :base |
| 150 | + resource_key = resource_key_for(field) |
| 151 | + [translation_for(resource_key), message].join(' ') |
| 152 | + end |
| 153 | + |
| 154 | + # Return the resource's attribute or relationship key name for a given field name. |
| 155 | + # e.g.: :title => :title, :user_id => :author |
| 156 | + # |
| 157 | + # @param field [Symbol] Name of the invalid field |
| 158 | + # e.g.: :title |
| 159 | + # |
| 160 | + # @return [Symbol] |
| 161 | + # |
| 162 | + # @api private |
| 163 | + def resource_key_for(field) |
| 164 | + @resource_key_for[field] ||= begin |
| 165 | + return field unless foreign_keys.include?(field) |
| 166 | + relationships.find { |r| r.foreign_key == field }.name.to_sym |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + # Turn the field name into human-friendly one. |
| 171 | + # e.g.: :first_name => "First name" |
| 172 | + # |
| 173 | + # @param field [Symbol] Name of the invalid field |
| 174 | + # e.g.: :first_name |
| 175 | + # |
| 176 | + # @return [String] |
| 177 | + # |
| 178 | + # @api private |
| 179 | + def translation_for(field) |
| 180 | + object.class.human_attribute_name(field) |
| 181 | + end |
| 182 | + |
| 183 | + # Return the base data used for all errors of this kind. |
| 184 | + # |
| 185 | + # @return [Hash] |
| 186 | + # |
| 187 | + # @api private |
| 188 | + def error_base |
| 189 | + { |
| 190 | + code: JSONAPI::VALIDATION_ERROR, |
| 191 | + status: :unprocessable_entity |
| 192 | + } |
| 193 | + end |
| 194 | + end |
| 195 | + end |
| 196 | + end |
| 197 | +end |
0 commit comments