-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #604 from steveukx/feature/error-detection-plugin
- Loading branch information
Showing
15 changed files
with
220 additions
and
68 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,33 @@ | ||
## Custom Error Detection | ||
|
||
By default, `simple-git` will determine that a `git` task has resulted in an error when the process exit | ||
code is anything other than `0` and there has been some data sent to the `stdErr` stream. Error handlers | ||
will be passed the content of both `stdOut` and `stdErr` concatenated together. | ||
|
||
To change any of this behaviour, configure the `simple-git` with the `errors` plugin with a function to be | ||
called after every task has been run and should return either `undefined` when the task is treated as | ||
a success, or a `Buffer` or `Error` when the task should be treated as a failure. | ||
|
||
When the default error handler (or any other plugin) has thrown an error, the first argument to the error | ||
detection plugin is the original error. Either return that error directly to allow it to bubble up to the | ||
task's error handlers, or implement your own error detection as below: | ||
|
||
```typescript | ||
import simpleGit from 'simple-git'; | ||
|
||
const git = simpleGit({ | ||
errors(error, result) { | ||
// optionally pass through any errors reported before this plugin runs | ||
if (error) return error; | ||
|
||
// customise the `errorCode` values to treat as success | ||
if (result.errorCode === 0) { | ||
return; | ||
} | ||
|
||
// the default error messages include both stdOut and stdErr, but that | ||
// can be changed here, or completely replaced with some other content | ||
return Buffer.concat([...result.stdOut, ...result.stdErr]); | ||
} | ||
}) | ||
``` |
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
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,47 @@ | ||
import { GitError } from '../errors/git-error'; | ||
import { GitExecutorResult, SimpleGitPluginConfig } from '../types'; | ||
import { SimpleGitPlugin } from './simple-git-plugin'; | ||
|
||
type TaskResult = Omit<GitExecutorResult, 'rejection'>; | ||
|
||
function isTaskError (result: TaskResult) { | ||
return !!(result.exitCode && result.stdErr.length); | ||
} | ||
|
||
function getErrorMessage (result: TaskResult) { | ||
return Buffer.concat([...result.stdOut, ...result.stdErr]); | ||
} | ||
|
||
export function errorDetectionHandler (overwrite = false, isError = isTaskError, errorMessage: (result: TaskResult) => Buffer | Error = getErrorMessage) { | ||
|
||
return (error: Buffer | Error | undefined, result: TaskResult) => { | ||
if ((!overwrite && error) || !isError(result)) { | ||
return error; | ||
} | ||
|
||
return errorMessage(result); | ||
}; | ||
} | ||
|
||
export function errorDetectionPlugin(config: SimpleGitPluginConfig['errors']): SimpleGitPlugin<'task.error'> { | ||
|
||
return { | ||
type: 'task.error', | ||
action(data, context) { | ||
const error = config(data.error, { | ||
stdErr: context.stdErr, | ||
stdOut: context.stdOut, | ||
exitCode: context.exitCode | ||
}); | ||
|
||
if (Buffer.isBuffer(error)) { | ||
return {error: new GitError(undefined, error.toString('utf-8'))}; | ||
} | ||
|
||
return { | ||
error | ||
}; | ||
}, | ||
}; | ||
|
||
} |
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
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
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
Oops, something went wrong.