Skip to content

Commit 4afd11d

Browse files
committed
Add errors throwing and handling
1 parent aa661a2 commit 4afd11d

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

bin/page-loader.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ program
1111
.option('-o, --output [dir]', 'output dir', process.cwd())
1212
.action((url, options) => {
1313
downloadPage(url, options.output)
14-
.then(filePath => console.log(filePath))
14+
.then((filePath) => {
15+
console.log(filePath)
16+
process.exit(0)
17+
})
18+
.catch((err) => {
19+
console.error(err.message)
20+
process.exit(1)
21+
})
1522
})
1623

1724
program.parse()

src/downloadFiles.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const downloadFiles = ($, url, filesDirPath, selector, attr) => {
4242
}
4343
})
4444
})
45+
.catch((err) => {
46+
log('failed to download %s: %s', absLink.href, err.message)
47+
throw new Error(`Cant load file ${absLink.href}`)
48+
})
4549
}),
4650
))
4751
}

src/getData.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ axiosDebugLog({
2424
const getData = url => axios
2525
.get(url, { responseType: 'arraybuffer' })
2626
.then(response => response.data)
27+
.catch((err) => {
28+
log(`Error: ${err.message}`)
29+
throw new Error(`Cant get ${url} - ${err.message}`)
30+
})
2731

2832
export default getData

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ const downloadPage = (url, outputDir) => {
4949
return filePath
5050
})
5151
.catch((err) => {
52-
// console.error('Full error:', err)
5352
log('error: %s', err.message)
54-
throw new Error(`Error: ${err.message}`)
53+
if (err.code === 'ENOENT') {
54+
throw new Error(`ERROR: No such output directory - ${outputDir}`)
55+
}
56+
if (err.code === 'EACCES') {
57+
throw new Error(`ERROR: No access to output directory - ${outputDir}`)
58+
}
59+
throw new Error(`ERROR: ${err.message}`)
5560
})
5661
}
5762

0 commit comments

Comments
 (0)