Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: make error messages consistent with go for interop tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <[email protected]>
  • Loading branch information
achingbrain committed Aug 20, 2018
1 parent a69ba91 commit 08f60c3
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 29 deletions.
22 changes: 22 additions & 0 deletions src/core/utils/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

class NonFatalError extends Error {
constructor (message) {
super(message)

this.code = 0
}
}

class FatalError extends Error {
constructor (message) {
super(message)

this.code = 1
}
}

module.exports = {
NonFatalError,
FatalError
}
1 change: 1 addition & 0 deletions src/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
countStreamBytes: require('./count-stream-bytes'),
createLock: require('./create-lock'),
createNode: require('./create-node'),
errors: require('./errors'),
formatCid: require('./format-cid'),
limitStreamBytes: require('./limit-stream-bytes'),
loadNode: require('./load-node'),
Expand Down
15 changes: 7 additions & 8 deletions src/core/utils/traverse-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const {
FILE_SEPARATOR
} = require('./constants')
const createNode = require('./create-node')
const {
NonFatalError
} = require('./errors')

const defaultOptions = {
parents: false,
Expand Down Expand Up @@ -84,15 +87,11 @@ const traverseToMfsObject = (ipfs, path, options, callback) => {
log(`index ${index} pathSegments.length ${pathSegments.length} pathSegment ${pathSegment} lastComponent ${lastComponent}`, options)

if (lastComponent && !options.createLastComponent) {
return done(new Error(`Path ${path.path} does not exist`))
log(`Last segment of ${path} did not exist`)
return done(new NonFatalError('file does not exist'))
} else if (!lastComponent && !options.parents) {
let message = `Cannot find ${path.path} - ${pathSegment} does not exist`

if (options.withCreateHint) {
message += ': Try again with the --parents flag to create it'
}

return done(new Error(message))
log(`Cannot traverse to ${path} - ${pathSegment} did not exist`)
return done(new NonFatalError('file does not exist'))
}

log(`Adding empty directory '${pathSegment}' to parent ${parent.name}`)
Expand Down
2 changes: 1 addition & 1 deletion src/http/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const mfsCp = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const mfsFlush = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const mfsLs = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const mfsMkdir = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/mv.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const mfsMv = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const mfsRead = (api) => {
stream.once('error', (error) => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const mfsRm = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion src/http/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const mfsStat = (api) => {
.catch(error => {
reply({
Message: error.message,
Code: 0,
Code: error.code || 0,
Type: 'error'
}).code(500).takeover()
})
Expand Down
2 changes: 1 addition & 1 deletion test/mkdir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('mkdir', function () {
parents: false
})
.catch(error => {
expect(error.message).to.contain('foo does not exist')
expect(error.message).to.contain('does not exist')
})
})

Expand Down
6 changes: 3 additions & 3 deletions test/mv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('mv', function () {
throw new Error('File was copied but not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${source} does not exist`)
expect(error.message).to.contain('does not exist')
})
})

Expand All @@ -88,7 +88,7 @@ describe('mv', function () {
throw new Error('Directory was copied but not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${source} does not exist`)
expect(error.message).to.contain('does not exist')
})
})

Expand All @@ -115,7 +115,7 @@ describe('mv', function () {
throw new Error('Directory was copied but not removed')
})
.catch(error => {
expect(error.message).to.contain(`Cannot find ${source} - ${directory} does not exist`)
expect(error.message).to.contain('does not exist')
})
})
})
16 changes: 8 additions & 8 deletions test/rm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('rm', function () {
throw new Error('File was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${file} does not exist`)
expect(error.message).to.contain('does not exist')
})
})

Expand All @@ -100,14 +100,14 @@ describe('rm', function () {
throw new Error('File #1 was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${file1} does not exist`)
expect(error.message).to.contain('does not exist')
})
.then(() => mfs.stat(file2))
.then(() => {
throw new Error('File #2 was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${file2} does not exist`)
expect(error.message).to.contain('does not exist')
})
})

Expand All @@ -123,7 +123,7 @@ describe('rm', function () {
throw new Error('Directory was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${directory} does not exist`)
expect(error.message).to.contain('does not exist')
})
})

Expand All @@ -143,14 +143,14 @@ describe('rm', function () {
throw new Error('File was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${subdirectory} does not exist`)
expect(error.message).to.contain('does not exist')
})
.then(() => mfs.ls(directory))
.then(() => {
throw new Error('Directory was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${directory} does not exist`)
expect(error.message).to.contain('does not exist')
})
})

Expand All @@ -170,14 +170,14 @@ describe('rm', function () {
throw new Error('File was not removed')
})
.catch(error => {
expect(error.message).to.contain(`Path ${file} does not exist`)
expect(error.message).to.contain('does not exist')
})
.then(() => mfs.stat(`/${directory}`))
.then(() => {
throw new Error('Directory was not removed')
})
.catch(error => {
expect(error.message).to.contain(`${directory} does not exist`)
expect(error.message).to.contain('does not exist')
})
})
})
2 changes: 1 addition & 1 deletion test/stat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('stat', function () {
throw new Error('No error was thrown for a non-existent file')
})
.catch(error => {
expect(error.message).to.contain('Path /i-do-not-exist does not exist')
expect(error.message).to.contain('does not exist')
})
})

Expand Down

0 comments on commit 08f60c3

Please sign in to comment.