Tweak upload functions for more sensible error logging

This commit is contained in:
Hippo 2020-01-01 21:38:34 +05:30
parent b6a8fe4e63
commit f402fe6195
1 changed files with 29 additions and 12 deletions

View File

@ -353,13 +353,22 @@ const createDirIfNotExist = async (client, folder) => {
// check the folder
await client.stat(folder)
.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
console.debug(`Noting missing subdirectory: ${folder}`)
// 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}`)
// then, create the current directory
@ -370,26 +379,29 @@ const createDirIfNotExist = async (client, folder) => {
await client.stat(folder)
.catch((err2) => {
// 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
})
} else {
// 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
}
})
} else {
// it's not a 404; we don't know how to handle this. Panic!
console.debug(err.toJSON())
console.error(err.toJSON())
throw err
}
})
return true
}
/**
* function [createUser]
* function [uploadDav]
* @returns [string] status
*/
const uploadDav = async (dirPath, filePath) => {
@ -404,16 +416,21 @@ const uploadDav = async (dirPath, filePath) => {
})
// create directory if not exists
console.debug(`Loading ${dirPath}`)
await createDirIfNotExist(client, dirPath)
console.debug(`[dav-upload] Loading ${dirPath}`)
if (!await createDirIfNotExist(client, dirPath)) {
console.error(`[dav-upload] Could not upload ${path.basename(filePath)} :(`)
return false
}
// upload a file
console.debug('Uploading file')
s = fs.createReadStream(filePath)
.pipe(client.createWriteStream(
outStream = client.createWriteStream(
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
}