Skip to content

Commit

Permalink
fix: Restrict try-catch in upload command so build can be finalized (#…
Browse files Browse the repository at this point in the history
…387)

* fix: 🐛 Restrict try-catch so build can be finalized

Rather than try-catching the entire method, the try-catch can be wrapped around
only the section between creating and finalizing a build. The try-catch is used
instead of chaining a `.catch` onto the promise in case the `reduce` callback
throws during argument evaluation.

* fix: 🐛 Exit with non-zero when error occurs in upload command
  • Loading branch information
Wil Wilsman authored Oct 3, 2019
1 parent be754a1 commit eed8798
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/services/image-snapshot-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,22 @@ export default class ImageSnapshotService extends PercyClientService {
}

async snapshotAll() {
try {
// intentially remove '' values from because that matches every file
const globs = this.configuration.files.split(',').filter(Boolean)
const ignore = this.configuration.ignore.split(',').filter(Boolean)
const paths = await globby(globs, { cwd: this.configuration.path, ignore })

if (!paths.length) {
logger.error(`no matching files found in '${this.configuration.path}''`)
logger.info('exiting')
return process.exit(1)
}
// intentially remove '' values from because that matches every file
const globs = this.configuration.files.split(',').filter(Boolean)
const ignore = this.configuration.ignore.split(',').filter(Boolean)
const paths = await globby(globs, { cwd: this.configuration.path, ignore })
let error

if (!paths.length) {
logger.error(`no matching files found in '${this.configuration.path}''`)
logger.info('exiting')
return process.exit(1)
}

await this.buildService.create()
logger.debug('uploading snapshots of static images')
await this.buildService.create()
logger.debug('uploading snapshots of static images')

try {
// wait for snapshots in parallel
await Promise.all(paths.reduce((promises, pathname) => {
logger.debug(`handling snapshot: '${pathname}'`)
Expand All @@ -140,12 +141,15 @@ export default class ImageSnapshotService extends PercyClientService {

return promises
}, [] as any[]))

// finalize build
await this.buildService.finalize()
} catch (error) {
logError(error)
process.exit(1)
} catch (err) {
error = err
logError(err)
}

// finalize build
await this.buildService.finalize()

// if an error occurred, exit with non-zero
if (error) process.exit(1)
}
}

0 comments on commit eed8798

Please sign in to comment.