diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index 5feb36bd..bfc2cb6a 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -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() diff --git a/lib/actions/build.coffee b/lib/actions/build.coffee index d72cf63e..e6dc6175 100644 --- a/lib/actions/build.coffee +++ b/lib/actions/build.coffee @@ -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 ] diff --git a/lib/actions/config.coffee b/lib/actions/config.coffee index a7a821a7..c5ba62fc 100644 --- a/lib/actions/config.coffee +++ b/lib/actions/config.coffee @@ -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: diff --git a/lib/actions/environment-variables.coffee b/lib/actions/environment-variables.coffee index 4c2a0337..64e53691 100644 --- a/lib/actions/environment-variables.coffee +++ b/lib/actions/environment-variables.coffee @@ -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 = diff --git a/lib/actions/help.coffee b/lib/actions/help.coffee index 7a252909..aa0f0325 100644 --- a/lib/actions/help.coffee +++ b/lib/actions/help.coffee @@ -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}") diff --git a/lib/actions/local/common.coffee b/lib/actions/local/common.coffee index 3191e226..db10f1c8 100644 --- a/lib/actions/local/common.coffee +++ b/lib/actions/local/common.coffee @@ -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' diff --git a/lib/actions/local/scan.coffee b/lib/actions/local/scan.coffee index e59b2327..b8fa0fca 100644 --- a/lib/actions/local/scan.coffee +++ b/lib/actions/local/scan.coffee @@ -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 diff --git a/lib/actions/local/ssh.coffee b/lib/actions/local/ssh.coffee index 9c6d6cea..1f6ac370 100644 --- a/lib/actions/local/ssh.coffee +++ b/lib/actions/local/ssh.coffee @@ -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 diff --git a/lib/actions/notes.coffee b/lib/actions/notes.coffee index aceaf315..12ddb702 100644 --- a/lib/actions/notes.coffee +++ b/lib/actions/notes.coffee @@ -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) diff --git a/lib/actions/ssh.coffee b/lib/actions/ssh.coffee index 561be990..96cc9952 100644 --- a/lib/actions/ssh.coffee +++ b/lib/actions/ssh.coffee @@ -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() diff --git a/lib/app.coffee b/lib/app.coffee index adb19d60..6c58476e 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -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 diff --git a/lib/utils/docker.coffee b/lib/utils/docker.coffee index 85afce52..e02e37bc 100644 --- a/lib/utils/docker.coffee +++ b/lib/utils/docker.coffee @@ -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?')