use try&catch

This commit is contained in:
SrGooglo 2023-07-20 13:43:15 +00:00
parent 5646510a22
commit 8cf613fbe6

View File

@ -4,6 +4,7 @@ import axios from "axios"
export default async (payload) => { export default async (payload) => {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try {
let { url, destination } = payload let { url, destination } = payload
// if destination path is not provided, use cache folder // if destination path is not provided, use cache folder
@ -11,10 +12,7 @@ export default async (payload) => {
destination = path.resolve(global.uploadCachePath, path.basename(url)) destination = path.resolve(global.uploadCachePath, path.basename(url))
} }
const writer = fs.createWriteStream(destination).catch((err) => { const writer = fs.createWriteStream(destination)
reject(err)
return false
})
if (!writer) { if (!writer) {
return false return false
@ -34,5 +32,8 @@ export default async (payload) => {
read: () => fs.readFileSync(destination), read: () => fs.readFileSync(destination),
})) }))
writer.on("error", reject) writer.on("error", reject)
} catch (error) {
reject(error)
}
}) })
} }