1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 10:19:30 +02:00

Make sure that check actually catches and handles errors, even if synchronous.

This commit is contained in:
Simo Kinnunen 2017-04-29 12:55:15 +09:00
parent 6bf300e29d
commit 2b836f6382

View file

@ -54,64 +54,64 @@ module.exports.handler = function() {
} }
function check(label, fn) { function check(label, fn) {
return Promise.try(function() { function Check() {
function Check() { }
}
Check.prototype.call = function(command, args, options) { Check.prototype.call = function(command, args, options) {
return call(command, args, options).catch(function(err) { return call(command, args, options).catch(function(err) {
if (err.code === 'ENOENT') { 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.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( throw new CheckError(
'%s is not installed (`%s` is missing)' '%s is currently %s but needs to be %s'
, label , label
, command , currentVersion
, wantedVersion
) )
} }
throw err
}) })
} }
}
Check.prototype.extract = function(what, re) { return Promise.try(function() {
return function(input) { return fn(new Check())
return Promise.try(function() { })
var match = re.exec(input) .catch(CheckError, function(err) {
if (!match) { log.error(err.message)
throw new CheckError(util.format('%s %s cannot be detected', label, what)) })
} .catch(function(err) {
return match[1] log.error('Unexpected error checking %s: %s', label, err)
}) })
}
}
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)
})
})
} }
function checkOSArch() { function checkOSArch() {