Skip to content

Commit

Permalink
Make sure that check actually catches and handles errors, even if syn…
Browse files Browse the repository at this point in the history
…chronous.
sorccu committed Apr 29, 2017
1 parent 6bf300e commit 2b836f6
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions lib/cli/doctor/index.js
Original file line number Diff line number Diff line change
@@ -54,64 +54,64 @@ module.exports.handler = function() {
}

function check(label, fn) {
return Promise.try(function() {
function Check() {
function Check() {
}

Check.prototype.call = function(command, args, options) {
return call(command, args, options).catch(function(err) {
if (err.code === 'ENOENT') {
throw new CheckError(
'%s is not installed (`%s` is missing)'
, label
, command
)
}

throw err
})
}

Check.prototype.extract = function(what, re) {
return function(input) {
return Promise.try(function() {
var match = re.exec(input)
if (!match) {
throw new CheckError(util.format('%s %s cannot be detected', label, what))
}
return match[1]
})
}
}

Check.prototype.call = function(command, args, options) {
return call(command, args, options).catch(function(err) {
if (err.code === 'ENOENT') {
Check.prototype.version = function(wantedVersion) {
return function(currentVersion) {
return Promise.try(function() {
log.info('Using %s %s', label, currentVersion)
var sanitizedVersion = currentVersion.replace(/~.*/, '')
return semver.satisfies(sanitizedVersion, wantedVersion)
})
.then(function(satisfied) {
if (!satisfied) {
throw new CheckError(
'%s is not installed (`%s` is missing)'
'%s is currently %s but needs to be %s'
, label
, command
, currentVersion
, wantedVersion
)
}

throw err
})
}
}

Check.prototype.extract = function(what, re) {
return function(input) {
return Promise.try(function() {
var match = re.exec(input)
if (!match) {
throw new CheckError(util.format('%s %s cannot be detected', label, what))
}
return match[1]
})
}
}

Check.prototype.version = function(wantedVersion) {
return function(currentVersion) {
return Promise.try(function() {
log.info('Using %s %s', label, currentVersion)
var sanitizedVersion = currentVersion.replace(/~.*/, '')
return semver.satisfies(sanitizedVersion, wantedVersion)
})
.then(function(satisfied) {
if (!satisfied) {
throw new CheckError(
'%s is currently %s but needs to be %s'
, label
, currentVersion
, wantedVersion
)
}
})
}
}

return fn(new Check())
.catch(CheckError, function(err) {
log.error(err.message)
})
.catch(function(err) {
log.error('Unexpected error checking %s: %s', label, err)
})
})
return Promise.try(function() {
return fn(new Check())
})
.catch(CheckError, function(err) {
log.error(err.message)
})
.catch(function(err) {
log.error('Unexpected error checking %s: %s', label, err)
})
}

function checkOSArch() {

0 comments on commit 2b836f6

Please sign in to comment.