Error Handling
wukong-deploy provides a flexible error handling mechanism to help you manage issues that may occur during the deployment process.
Error Handling Configuration
Each command can define its own error handling strategy:
{
cmd: "npm run build",
cwd: '/root/www/test',
description: "Build project",
exitOnStdErr: false, // Whether to exit when an error occurs
errorMatch: [ // Error matching patterns
/Error:/i,
/Failed to compile/i
]
}
Configuration Options
exitOnStdErr
true
: Any content written tostderr
will be treated as an errorfalse
: Allows content to be written tostderr
, used together witherrorMatch
errorMatch
Supports either a single string/regex or an array of regex patterns:
errorMatch: /Error:/; // Single regular expression
errorMatch: [
// Array of regular expressions
/Error:/i,
/Failed/i,
/Exception/i,
];
Error Handling Examples
1. NPM Build Error Handling
{
cmd: "npm run build",
cwd: '/root/www/test',
description: "Build project",
exitOnStdErr: false,
errorMatch: [
/Failed to compile/,
/Error in/,
/Module not found/
]
}
2. Git Operation Error Handling
{
cmd: "git pull",
cwd: '/root/www/test',
description: "Update source code",
exitOnStdErr: false,
errorMatch: [
/Authentication failed/,
/Merge conflict/,
/Please commit your changes/
]
}
3. Database Operation Error Handling
{
cmd: "npm run migrate",
cwd: '/root/www/test',
description: "Database migration",
exitOnStdErr: false,
errorMatch: [
/Connection refused/,
/Duplicate entry/,
/Access denied/
]
}
Best Practices
- Define specific error matching patterns for each command
- Set
exitOnStdErr: false
for non-critical commands - Use regular expressions to match common error messages
- Add clear and descriptive error messages for better debugging