Tweak upload functions for more sensible error logging
This commit is contained in:
parent
b6a8fe4e63
commit
f402fe6195
41
functions.js
41
functions.js
|
@ -353,13 +353,22 @@ const createDirIfNotExist = async (client, folder) => {
|
||||||
// check the folder
|
// check the folder
|
||||||
await client.stat(folder)
|
await client.stat(folder)
|
||||||
.catch(async (err) => {
|
.catch(async (err) => {
|
||||||
if (err.response.status == 404) {
|
if (!err.response) {
|
||||||
|
// no response! Maybe a network error or something :P
|
||||||
|
console.error(`[dav-upload:folder] Error creating folder "${folder}"`)
|
||||||
|
console.error(`[dav-upload:folder] ${err.toJSON().message}`)
|
||||||
|
console.error('[dav-upload:folder] Please check your Internet connection and try again')
|
||||||
|
return false
|
||||||
|
} else if (err.response.status == 404) {
|
||||||
|
|
||||||
// it's a 404, so we'll create the directory
|
// it's a 404, so we'll create the directory
|
||||||
console.debug(`Noting missing subdirectory: ${folder}`)
|
console.debug(`Noting missing subdirectory: ${folder}`)
|
||||||
|
|
||||||
// first, create the parent directory (if required)
|
// first, create the parent directory (if required)
|
||||||
await createDirIfNotExist(client, path.dirname(folder))
|
if (!!await createDirIfNotExist(client, path.dirname(folder))) {
|
||||||
|
// if not created, we fail too :-/
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
console.debug(`Creating missing subdirectory: ${folder}`)
|
console.debug(`Creating missing subdirectory: ${folder}`)
|
||||||
// then, create the current directory
|
// then, create the current directory
|
||||||
|
@ -370,26 +379,29 @@ const createDirIfNotExist = async (client, folder) => {
|
||||||
await client.stat(folder)
|
await client.stat(folder)
|
||||||
.catch((err2) => {
|
.catch((err2) => {
|
||||||
// Bad guess. Panic (and raise the original error)
|
// Bad guess. Panic (and raise the original error)
|
||||||
console.debug(err.toJSON())
|
console.error(`Error: ${err.toJSON().message}`)
|
||||||
|
console.error("We're not sure what went wrong. Help!")
|
||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// what's this? Panic!
|
// what's this? Panic!
|
||||||
console.debug(err.toJSON())
|
console.error(`Error: ${err.toJSON().message}`)
|
||||||
|
console.error("We're not sure what went wrong. Help!")
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// it's not a 404; we don't know how to handle this. Panic!
|
// it's not a 404; we don't know how to handle this. Panic!
|
||||||
console.debug(err.toJSON())
|
console.error(err.toJSON())
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* function [createUser]
|
* function [uploadDav]
|
||||||
* @returns [string] status
|
* @returns [string] status
|
||||||
*/
|
*/
|
||||||
const uploadDav = async (dirPath, filePath) => {
|
const uploadDav = async (dirPath, filePath) => {
|
||||||
|
@ -404,16 +416,21 @@ const uploadDav = async (dirPath, filePath) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// create directory if not exists
|
// create directory if not exists
|
||||||
console.debug(`Loading ${dirPath}`)
|
console.debug(`[dav-upload] Loading ${dirPath}`)
|
||||||
await createDirIfNotExist(client, dirPath)
|
if (!await createDirIfNotExist(client, dirPath)) {
|
||||||
|
console.error(`[dav-upload] Could not upload ${path.basename(filePath)} :(`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// upload a file
|
// upload a file
|
||||||
console.debug('Uploading file')
|
console.debug('Uploading file')
|
||||||
s = fs.createReadStream(filePath)
|
outStream = client.createWriteStream(
|
||||||
.pipe(client.createWriteStream(
|
|
||||||
path.join(dirPath, path.basename(filePath))
|
path.join(dirPath, path.basename(filePath))
|
||||||
))
|
)
|
||||||
.on('finish', () => console.debug('Uploaded successfully.'))
|
outStream.on('finish', () => console.debug('Uploaded successfully.'))
|
||||||
|
|
||||||
|
inStream = fs.createReadStream(filePath)
|
||||||
|
.pipe(outStream)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue