Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Merge pull request #592 from Medium/nicks/error
Browse files Browse the repository at this point in the history
Better error handling on the run() method
  • Loading branch information
nicks committed Aug 3, 2016
2 parents e649b58 + dc41be9 commit 644fd3c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/phantomjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,35 @@ exports.run = function () {
try {
var program = exports.exec.apply(null, args)
var isFirst = true
var stderr = ''
program.stdout.on('data', function () {
// This detects PhantomJS instance get ready.
if (!isFirst) return
isFirst = false
resolve(program)
})
program.stderr.on('data', function (data) {
stderr = stderr + data.toString('utf8')
})
program.on('error', function (err) {
if (!isFirst) return
isFirst = false
reject(err)
})
program.on('exit', function (code) {
if (!isFirst) return
isFirst = false
if (code == 0) {
// PhantomJS doesn't use exit codes correctly :(
if (stderr.indexOf('Error:') == 0) {
reject(new Error(stderr))
} else {
resolve(program)
}
} else {
reject(new Error('Exit code: ' + code))
}
})
} catch (err) {
reject(err)
}
Expand Down
11 changes: 11 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,14 @@ exports.testPhantomRun = function (test) {
})
})
}

exports.testPhantomRunError = function (test) {
test.expect(1)
phantomjs.run('--bogus').then(function () {
test.ok(false, 'Expected not to start')
test.done()
}, function (err) {
test.equal('Error: Unknown option: bogus\n', err.message)
test.done()
})
}

0 comments on commit 644fd3c

Please sign in to comment.