Skip to content

Commit

Permalink
fix: handle errors and existing better
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Mar 24, 2022
1 parent 9534a6c commit 51ff30a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/daemon/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ function checkValidConfig (ipfsd) {
}

try {
const stats = fs.statSync(ipfsd.path)
if (!stats.isDirectory()) {
throw new Error('IPFS_PATH must be a directory')
}

if (!configExists(ipfsd)) {
// Config is generated automatically if it doesn't exist.
return true
}

// This should catch errors such having no configuration file,
// IPFS_DIR not being a directory, or the configuration file
// being corrupted.
Expand Down
19 changes: 14 additions & 5 deletions src/daemon/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ async function spawn ({ flags, path }) {
args: flags
})

if (!checkValidConfig(ipfsd)) {
throw new Error(`repository at ${ipfsd.path} is invalid`)
}

if (configExists(ipfsd)) {
migrateConfig(ipfsd)
return { ipfsd, isRemote: false }
Expand Down Expand Up @@ -166,12 +170,17 @@ async function startIpfsWithLogs (ipfsd) {
}

module.exports = async function (opts) {
const { ipfsd, isRemote } = await spawn(opts)
if (!isRemote) {
if (!checkValidConfig(ipfsd)) {
return { err: 'Invalid or corrupted IPFS repository or configuration' }
}
let ipfsd, isRemote

try {
const res = await spawn(opts)
ipfsd = res.ipfsd
isRemote = res.isRemote
} catch (err) {
return { err: err.toString() }
}

if (!isRemote) {
await checkPorts(ipfsd)
}

Expand Down

0 comments on commit 51ff30a

Please sign in to comment.