Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Incoherent error message with the file API specs #1256

Closed
@fsdiogo

Description

@fsdiogo

Hey guys, I stumbled upon an error when trying the ipfs-101 example.

  • Version: 0.28.0
  • Platform: Darwin
  • Subsystem: interface-ipfs-core

Type: Bug

Severity: Low

Description:

I was following the tutorial along and got to the adding a file to IPFS part:

// Create the File to add, a file consists of a path + content. More details on
// https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md
(cb) => node.files.add({
  path: 'hello.txt',
  content: Buffer.from('Hello World')
}, (err, filesAdded) => {
  if (err) { return cb(err) }

  // Once the file is added, we get back an object containing the path, the
  // multihash and the sie of the file
  console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
  fileMultihash = filesAdded[0].hash
  cb()
})

After running the script nothing was being logged because an error occurred. When logging the error I got the following output:

Error: Invalid arguments, data must be an object, Buffer or readable stream

The files API specs states:

JavaScript - ipfs.files.add(data, [options], [callback])
Where data may be:

a Buffer instance
a Readable Stream
a Pull Stream
a Path (caveat: will only work in Node.js)
a URL
an array of objects, each of the form:

So the error message isn't coherent with the files API.

To check which was wrong I looked at the files.js source code and found:

const ok = Buffer.isBuffer(data) ||
        isStream.readable(data) ||
        Array.isArray(data) ||
        OtherBuffer.isBuffer(data)

if (!ok) {
    return callback(new Error('Invalid arguments, data must be an object, Buffer or readable stream'))
}

So the error message is wrong, it should be:

Error: Invalid arguments, data must be an array of objects, Buffer or readable stream

Also, in the example it's missing the array, it's just an object, that's why it doesn't work. Putting the object inside an array works as expected 👍

I can open a PR with the fixes if you want!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions