-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw custom errors from spawn (#32)
Ref: npm/cli#3149
- Loading branch information
1 parent
ef5cfcc
commit 766de2f
Showing
9 changed files
with
169 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
const maxRetry = 3 | ||
|
||
class GitError extends Error { | ||
shouldRetry () { | ||
return false | ||
} | ||
} | ||
|
||
class GitConnectionError extends GitError { | ||
constructor (message) { | ||
super('A git connection error occurred') | ||
} | ||
|
||
shouldRetry (number) { | ||
return number < maxRetry | ||
} | ||
} | ||
|
||
class GitPathspecError extends GitError { | ||
constructor (message) { | ||
super('The git reference could not be found') | ||
} | ||
} | ||
|
||
class GitUnknownError extends GitError { | ||
constructor (message) { | ||
super('An unknown git error occurred') | ||
} | ||
} | ||
|
||
module.exports = { | ||
GitConnectionError, | ||
GitPathspecError, | ||
GitUnknownError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { | ||
GitConnectionError, | ||
GitPathspecError, | ||
GitUnknownError | ||
} = require('./errors.js') | ||
|
||
const connectionErrorRe = new RegExp([ | ||
'remote error: Internal Server Error', | ||
'The remote end hung up unexpectedly', | ||
'Connection timed out', | ||
'Operation timed out', | ||
'Failed to connect to .* Timed out', | ||
'Connection reset by peer', | ||
'SSL_ERROR_SYSCALL', | ||
'The requested URL returned error: 503' | ||
].join('|')) | ||
|
||
const missingPathspecRe = /pathspec .* did not match any file\(s\) known to git/ | ||
|
||
function makeError (er) { | ||
const message = er.stderr | ||
let gitEr | ||
if (connectionErrorRe.test(message)) { | ||
gitEr = new GitConnectionError(message) | ||
} else if (missingPathspecRe.test(message)) { | ||
gitEr = new GitPathspecError(message) | ||
} else { | ||
gitEr = new GitUnknownError(message) | ||
} | ||
return Object.assign(gitEr, er) | ||
} | ||
|
||
module.exports = makeError |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const _makeError = require('../lib/make-error.js') | ||
const errors = require('../lib/errors.js') | ||
const t = require('tap') | ||
|
||
const makeError = (message) => | ||
// Create the error with properties like it came from promise-spawn | ||
_makeError(Object.assign(new Error(), { stderr: message })) | ||
|
||
t.test('throw matching error for missing pathspec', (t) => { | ||
const missingPathspec = makeError('error: pathspec \'foo\' did not match any file(s) known to git') | ||
t.ok(missingPathspec instanceof errors.GitPathspecError, 'error is a pathspec error') | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('only transient connection errors are retried', (t) => { | ||
const sslError = makeError('SSL_ERROR_SYSCALL') | ||
t.ok(sslError.shouldRetry(1), 'transient error, not beyond max') | ||
t.ok(sslError instanceof errors.GitConnectionError, 'error is a connection error') | ||
|
||
const unknownError = makeError('asdf') | ||
t.notOk(unknownError.shouldRetry(1), 'unknown error, do not retry') | ||
t.ok(unknownError instanceof errors.GitUnknownError, 'error is an unknown error') | ||
|
||
const connectError = makeError('Failed to connect to fooblz Timed out') | ||
t.notOk(connectError.shouldRetry(69), 'beyond max retries, do not retry') | ||
t.ok(connectError instanceof errors.GitConnectionError, 'error is a connection error') | ||
|
||
t.end() | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters