Don't report lots of user input errors

Change-Type: patch
This commit is contained in:
Tim Perry 2018-04-16 13:59:11 +02:00
parent a16ac37625
commit 6a8b947c2e
12 changed files with 31 additions and 15 deletions

View File

@ -52,7 +52,7 @@ exports.create =
# https://github.com/resin-io/resin-cli/issues/30
resin.models.application.has(params.name).then (hasApplication) ->
if hasApplication
throw new Error('You already have an application with that name!')
patterns.expectedError('You already have an application with that name!')
.then ->
return options.type or patterns.selectDeviceType()

View File

@ -95,6 +95,7 @@ module.exports =
# compositions with many services trigger misleading warnings
require('events').defaultMaxListeners = 1000
{ expectedError } = require('../utils/patterns')
helpers = require('../utils/helpers')
Logger = require('../utils/logger')
@ -111,7 +112,7 @@ module.exports =
{ application, arch, deviceType } = options
if (not (arch? and deviceType?) and not application?) or (application? and (arch? or deviceType?))
throw new Error('You must specify either an application or an arch/deviceType pair to build for')
expectedError('You must specify either an application or an arch/deviceType pair to build for')
if arch? and deviceType?
[ undefined, arch, deviceType ]

View File

@ -277,10 +277,12 @@ exports.generate =
form = require('resin-cli-form')
deviceConfig = require('resin-device-config')
prettyjson = require('prettyjson')
{ generateDeviceConfig, generateApplicationConfig } = require('../utils/config')
{ expectedError } = require('../utils/patterns')
if not options.device? and not options.application?
throw new Error '''
expectedError '''
You have to pass either a device or an application.
See the help page for examples:

View File

@ -53,17 +53,19 @@ exports.list =
resin = require('resin-sdk-preconfigured')
visuals = require('resin-cli-visuals')
{ expectedError } = require('../utils/patterns')
Promise.try ->
if options.application?
return resin.models.environmentVariables.getAllByApplication(options.application)
else if options.device?
return resin.models.environmentVariables.device.getAll(options.device)
else
throw new Error('You must specify an application or device')
expectedError('You must specify an application or device')
.tap (environmentVariables) ->
if _.isEmpty(environmentVariables)
throw new Error('No environment variables found')
expectedError('No environment variables found')
if not options.verbose
isSystemVariable = resin.models.environmentVariables.isSystemVariable
environmentVariables = _.reject(environmentVariables, isSystemVariable)
@ -141,6 +143,8 @@ exports.add =
Promise = require('bluebird')
resin = require('resin-sdk-preconfigured')
{ expectedError } = require('../utils/patterns')
Promise.try ->
if not params.value?
params.value = process.env[params.key]
@ -155,7 +159,7 @@ exports.add =
else if options.device?
resin.models.environmentVariables.device.create(options.device, params.key, params.value)
else
throw new Error('You must specify an application or device')
expectedError('You must specify an application or device')
.nodeify(done)
exports.rename =

View File

@ -18,6 +18,7 @@ _ = require('lodash')
capitano = require('capitano')
columnify = require('columnify')
messages = require('../utils/messages')
{ expectedError } = require('../utils/patterns')
parse = (object) ->
return _.fromPairs _.map object, (item) ->
@ -78,7 +79,7 @@ command = (params, options, done) ->
return done(error) if error?
if not command? or command.isWildcard()
return done(new Error("Command not found: #{params.command}"))
expectedError("Command not found: #{params.command}")
console.log("Usage: #{command.signature}")

View File

@ -4,6 +4,7 @@ form = require('resin-cli-form')
chalk = require('chalk')
dockerUtils = require('../../utils/docker')
{ expectedError } = require('../../utils/patterns')
exports.dockerPort = dockerPort = 2375
exports.dockerTimeout = dockerTimeout = 2000
@ -23,7 +24,7 @@ exports.selectContainerFromDevice = Promise.method (deviceIp, filterSupervisor =
filterOutSupervisorContainer(container)
.then (containers) ->
if _.isEmpty(containers)
throw new Error("No containers found in #{deviceIp}")
expectedError("No containers found in #{deviceIp}")
return form.ask
message: 'Select a container'

View File

@ -64,6 +64,7 @@ module.exports =
{ SpinnerPromise } = require('resin-cli-visuals')
{ dockerPort, dockerTimeout } = require('./common')
dockerUtils = require('../../utils/docker')
{ expectedError } = require('../../utils/patterns')
if options.timeout?
options.timeout *= 1000
@ -81,7 +82,7 @@ module.exports =
.catchReturn(false)
.tap (devices) ->
if _.isEmpty(devices)
throw new Error('Could not find any resinOS devices in the local network')
expectedError('Could not find any resinOS devices in the local network')
.map ({ host, address }) ->
docker = dockerUtils.createClient(host: address, port: dockerPort, timeout: dockerTimeout)
Promise.props

View File

@ -67,10 +67,12 @@ module.exports =
Promise = require 'bluebird'
_ = require('lodash')
{ forms } = require('resin-sync')
{ selectContainerFromDevice, getSubShellCommand } = require('./common')
{ expectedError } = require('../../utils/patterns')
if (options.host is true and options.container?)
throw new Error('Please pass either --host or --container option')
expectedError('Please pass either --host or --container option')
if not options.port?
options.port = 22222

View File

@ -45,9 +45,11 @@ exports.set =
_ = require('lodash')
resin = require('resin-sdk-preconfigured')
{ expectedError } = require('../utils/patterns')
Promise.try ->
if _.isEmpty(params.note)
throw new Error('Missing note content')
expectedError('Missing note content')
resin.models.device.note(options.device, params.note)
.nodeify(done)

View File

@ -108,7 +108,7 @@ module.exports =
console.info("Connecting to: #{uuid}")
resin.models.device.get(uuid)
.then (device) ->
throw new Error('Device is not online') if not device.is_online
patterns.expectedError('Device is not online') if not device.is_online
Promise.props
username: resin.auth.whoami()

View File

@ -77,6 +77,7 @@ actions = require('./actions')
errors = require('./errors')
events = require('./events')
update = require('./utils/update')
{ expectedError } = require('./utils/patterns')
# Assign bluebird as the global promise library
# stream-to-promise will produce native promises if not
@ -87,13 +88,13 @@ require('any-promise/register/bluebird')
capitano.permission 'user', (done) ->
resin.auth.isLoggedIn().then (isLoggedIn) ->
if not isLoggedIn
throw new Error '''
expectedError('''
You have to log in to continue
Run the following command to go through the login wizard:
$ resin login
'''
''')
.nodeify(done)
capitano.command

View File

@ -174,5 +174,6 @@ exports.createClient = createClient = do ->
return new Docker(opts)
ensureDockerSeemsAccessible = (docker) ->
{ expectedError } = require('./patterns')
docker.ping().catch ->
throw new Error('Docker seems to be unavailable. Is it installed and running?')
expectedError('Docker seems to be unavailable. Is it installed and running?')