-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from tlaanemaa/err_refactor
Refactored how error handling works
- Loading branch information
Showing
10 changed files
with
99 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,19 @@ | ||
#!/usr/bin/env node | ||
require('..')(); | ||
|
||
const logAndExit = (err) => { | ||
// eslint-disable-next-line no-console | ||
console.error(err instanceof Error ? err.message : err); | ||
process.exit(1); | ||
}; | ||
|
||
// Global error handlers | ||
process.on('uncaughtException', logAndExit); | ||
process.on('unhandledRejection', logAndExit); | ||
|
||
(async () => { | ||
// eslint-disable-next-line global-require | ||
await require('..')(); | ||
|
||
// Safeguard against hanging application | ||
process.exit(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { containers: containerNames } = require('./modules/options'); | ||
const { getContainers, restoreContainer, backupContainer } = require('./modules/docker'); | ||
const { getInspectFilesSync, logAndReturnErrors } = require('./modules/utils'); | ||
|
||
// Main backup function | ||
const backup = async () => { | ||
// Get all container names if needed | ||
const containers = containerNames.length | ||
? containerNames | ||
: await getContainers(); | ||
|
||
// Backup containers | ||
return Promise.all(containers.map( | ||
logAndReturnErrors(backupContainer), | ||
)); | ||
}; | ||
|
||
// Main restore function | ||
const restore = async () => { | ||
// Get all container names if needed | ||
const containers = containerNames.length | ||
? containerNames | ||
: getInspectFilesSync(); | ||
|
||
// Restore containers | ||
return Promise.all(containers.map( | ||
logAndReturnErrors(restoreContainer), | ||
)); | ||
}; | ||
|
||
module.exports = { | ||
backup, | ||
restore, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,19 @@ | ||
const { operation, containers: containerNames } = require('./modules/options'); | ||
const { getContainers, restoreContainer, backupContainer } = require('./modules/docker'); | ||
const { getInspectFilesSync, asyncTryLog } = require('./modules/utils'); | ||
|
||
// Main backup function | ||
const backup = async () => { | ||
const containers = containerNames.length | ||
? containerNames | ||
: await asyncTryLog(() => getContainers(), true); | ||
|
||
return Promise.all(containers.map( | ||
container => asyncTryLog(() => backupContainer(container)), | ||
)); | ||
}; | ||
|
||
// Main restore function | ||
const restore = async () => { | ||
const containers = containerNames.length | ||
? containerNames | ||
: getInspectFilesSync(); | ||
|
||
return Promise.all(containers.map( | ||
container => asyncTryLog(() => restoreContainer(container)), | ||
)); | ||
}; | ||
const commands = require('./commands'); | ||
const { operation } = require('./modules/options'); | ||
|
||
// Main method to run the tool | ||
module.exports = async () => { | ||
const operations = { backup, restore }; | ||
const results = await operations[operation](); | ||
const results = await commands[operation](); | ||
// eslint-disable-next-line no-console | ||
console.log('== Done =='); | ||
|
||
// Check if we had any errors and log them again if there are | ||
// Check if we had any errors and throw them if we did | ||
const errors = results.filter(result => result instanceof Error); | ||
if (errors.length) { | ||
// eslint-disable-next-line no-console | ||
console.error('\nThe following errors occurred during the run (this does not include errors from the tar command used for volume backup/restore):'); | ||
// eslint-disable-next-line no-console | ||
errors.map(err => console.error(err.message)); | ||
process.exit(1); | ||
const errorHeader = '\nThe following errors occurred during the run (this does not include errors from the tar command used for volume backup/restore):\n'; | ||
const errorMessages = errors.map(e => e.message).join('\n'); | ||
throw new Error(errorHeader + errorMessages); | ||
} | ||
|
||
process.exit(0); | ||
return results; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.