Sha256: 1dd47cd41010075777b5f713230e5ba0ff3e0bfdc39f98aa6b3ffa84e5869978

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

class ActiveRecordOperationsProcessor < JSONAPI::OperationsProcessor
  private

  def transaction
    if @transactional
      ActiveRecord::Base.transaction do
        yield
      end
    else
      yield
    end
  end

  def rollback
    fail ActiveRecord::Rollback if @transactional
  end

  def process_operation(operation)
    operation.apply
  rescue ActiveRecord::DeleteRestrictionError => e
    record_locked_error = JSONAPI::Exceptions::RecordLocked.new(e.message)
    return JSONAPI::ErrorsOperationResult.new(record_locked_error.errors[0].code, record_locked_error.errors)

  rescue ActiveRecord::RecordNotFound
    record_not_found = JSONAPI::Exceptions::RecordNotFound.new(operation.associated_key)
    return JSONAPI::ErrorsOperationResult.new(record_not_found.errors[0].code, record_not_found.errors)

  rescue JSONAPI::Exceptions::Error => e
    raise e

  rescue => e
    if JSONAPI.configuration.exception_class_whitelist.any? { |k| e.class.ancestors.include?(k) }
      raise e
    else
      internal_server_error = JSONAPI::Exceptions::InternalServerError.new(e)
      Rails.logger.error { "Internal Server Error: #{e.message} #{e.backtrace.join("\n")}" }
      return JSONAPI::ErrorsOperationResult.new(internal_server_error.errors[0].code, internal_server_error.errors)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jsonapi-resources-0.5.8 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.7 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.6 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.5 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.4 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.3 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.2 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.1 lib/jsonapi/active_record_operations_processor.rb