Browse Source

Don't print out stack traces upon user abort (#544)

* remove `instanceof` check

Use duck-type checking instead.

* handle USER_ABORT errors in `handleError()`

* don't print out stack traces upon user abort

Also eliminates the usage of `async-retry` since it was
pretty much overkill for this use case. Instead just extract
the logic out into its own function and call it recursively
when we have to do the user prompt.

* Print `Aborted` upon user about
master
Nathan Rajlich 8 years ago
committed by Matheus Fernandes
parent
commit
bc26a57d7c
  1. 168
      bin/now-deploy.js
  2. 8
      lib/error.js
  3. 3
      lib/utils/output/error.js

168
bin/now-deploy.js

@ -12,7 +12,6 @@ const minimist = require('minimist')
const ms = require('ms') const ms = require('ms')
const flatten = require('arr-flatten') const flatten = require('arr-flatten')
const dotenv = require('dotenv') const dotenv = require('dotenv')
const retry = require('async-retry')
const { eraseLines } = require('ansi-escapes') const { eraseLines } = require('ansi-escapes')
const { write: copy } = require('clipboardy') const { write: copy } = require('clipboardy')
@ -195,7 +194,7 @@ if (Array.isArray(autoAliases)) {
} }
const stopDeployment = msg => { const stopDeployment = msg => {
error(msg) handleError(msg)
process.exit(1) process.exit(1)
} }
@ -238,7 +237,7 @@ async function main() {
try { try {
await sync({ token, config }) await sync({ token, config })
} catch (err) { } catch (err) {
return stopDeployment(`Unknown error: ${err}\n${err.stack}`) return stopDeployment(err)
} }
} }
@ -322,7 +321,6 @@ async function sync({ token, config: { currentTeam, user } }) {
} }
} }
let nowConfig
let deploymentType let deploymentType
// CLI deployment type explicit overrides // CLI deployment type explicit overrides
@ -347,88 +345,12 @@ async function sync({ token, config: { currentTeam, user } }) {
} }
let meta let meta
await retry( ;({ meta, deploymentName, deploymentType } = await readMeta(
async () => { path,
try { deploymentName,
meta = await readMetaData(path, { deploymentType
deploymentType, ))
deploymentName, const nowConfig = meta.nowConfig
quiet: true
})
nowConfig = meta.nowConfig
if (!deploymentType) {
deploymentType = meta.type
if (debug) {
console.log(
`> [debug] Detected \`deploymentType\` = \`${deploymentType}\``
)
}
}
if (!deploymentName) {
deploymentName = meta.name
if (debug) {
console.log(
`> [debug] Detected \`deploymentName\` = "${deploymentName}"`
)
}
}
} catch (err) {
if (err.code === 'MULTIPLE_MANIFESTS') {
if (debug) {
console.log('> [debug] Multiple manifests found, disambiguating')
}
if (isTTY) {
console.log(
`> Two manifests found. Press [${chalk.bold('n')}] to deploy or re-run with --flag`
)
try {
deploymentType = await promptOptions([
[
'npm',
`${chalk.bold('package.json')}\t${chalk.gray(' --npm')} `
],
[
'docker',
`${chalk.bold('Dockerfile')}\t${chalk.gray('--docker')} `
]
])
} catch (err) {
if (err.code === 'USER_ABORT') {
if (debug) {
console.log(`> [debug] Got Ctrl+C, aborting`)
}
return exit(1)
}
throw err
}
if (debug) {
console.log(
`> [debug] Selected \`deploymentType\` = "${deploymentType}"`
)
}
// Invoke async-retry and try again with the explicit deployment type
throw err
}
} else {
return stopDeployment(err)
}
}
},
{
retries: 1,
minTimeout: 0,
maxTimeout: 0,
onRetry: console.log
}
)
const now = new Now({ apiUrl, token, debug, currentTeam }) const now = new Now({ apiUrl, token, debug, currentTeam })
@ -573,8 +495,7 @@ async function sync({ token, config: { currentTeam, user } }) {
console.log(`> [debug] error: ${err}\n${err.stack}`) console.log(`> [debug] error: ${err}\n${err.stack}`)
} }
handleError(err) return stopDeployment(err)
process.exit(1)
} }
const { url } = now const { url } = now
@ -646,10 +567,10 @@ async function sync({ token, config: { currentTeam, user } }) {
return exit() return exit()
} }
} else if (!wantsPublic) { } else if (!wantsPublic) {
let msg = '\nYou are on the OSS plan. Your code will be made public.' const msg =
msg += ' If you agree with that, please run again with --public.' '\nYou are on the OSS plan. Your code will be made public.' +
console.log(msg) ' If you agree with that, please run again with --public.'
return exit(1) return stopDeployment(msg)
} }
} }
@ -677,8 +598,7 @@ async function sync({ token, config: { currentTeam, user } }) {
now.on('error', err => { now.on('error', err => {
error('Upload failed') error('Upload failed')
handleError(err) return stopDeployment(err)
process.exit(1)
}) })
} else { } else {
if (!quiet) { if (!quiet) {
@ -697,6 +617,66 @@ async function sync({ token, config: { currentTeam, user } }) {
} }
} }
async function readMeta(path, deploymentName, deploymentType) {
try {
const meta = await readMetaData(path, {
deploymentType,
deploymentName,
quiet: true
})
if (!deploymentType) {
deploymentType = meta.type
if (debug) {
console.log(
`> [debug] Detected \`deploymentType\` = \`${deploymentType}\``
)
}
}
if (!deploymentName) {
deploymentName = meta.name
if (debug) {
console.log(
`> [debug] Detected \`deploymentName\` = "${deploymentName}"`
)
}
}
return {
meta,
deploymentName,
deploymentType
}
} catch (err) {
if (isTTY && err.code === 'MULTIPLE_MANIFESTS') {
if (debug) {
console.log('> [debug] Multiple manifests found, disambiguating')
}
console.log(
`> Two manifests found. Press [${chalk.bold('n')}] to deploy or re-run with --flag`
)
deploymentType = await promptOptions([
['npm', `${chalk.bold('package.json')}\t${chalk.gray(' --npm')} `],
['docker', `${chalk.bold('Dockerfile')}\t${chalk.gray('--docker')} `]
])
if (debug) {
console.log(
`> [debug] Selected \`deploymentType\` = "${deploymentType}"`
)
}
return readMeta(path, deploymentName, deploymentType)
}
throw err
}
}
function printLogs(host, token, currentTeam, user) { function printLogs(host, token, currentTeam, user) {
// Log build // Log build
const logger = new Logger(host, token, { debug, quiet }) const logger = new Logger(host, token, { debug, quiet })

8
lib/error.js

@ -3,8 +3,14 @@ const ms = require('ms')
const chalk = require('chalk') const chalk = require('chalk')
const error = require('./utils/output/error') const error = require('./utils/output/error')
const info = require('./utils/output/info')
function handleError(err) { function handleError(err) {
// Coerce Strings to Error instances
if (typeof err === 'string') {
err = new Error(err)
}
if (err.status === 403) { if (err.status === 403) {
error( error(
'Authentication error. Run `now -L` or `now --login` to log-in again.' 'Authentication error. Run `now -L` or `now --login` to log-in again.'
@ -26,6 +32,8 @@ function handleError(err) {
error(err.message) error(err.message)
} else if (err.status === 500) { } else if (err.status === 500) {
error('Unexpected server error. Please retry.') error('Unexpected server error. Please retry.')
} else if (err.code === 'USER_ABORT') {
info('Aborted')
} else { } else {
error(`Unexpected error. Please try later. (${err.message})`) error(`Unexpected error. Please try later. (${err.message})`)
} }

3
lib/utils/output/error.js

@ -2,9 +2,8 @@ const chalk = require('chalk')
// Prints an error message // Prints an error message
module.exports = msg => { module.exports = msg => {
if (msg instanceof Error) { if (msg.message) {
msg = msg.message msg = msg.message
} }
console.error(`${chalk.red('> Error!')} ${msg}`) console.error(`${chalk.red('> Error!')} ${msg}`)
} }

Loading…
Cancel
Save