Skip to content

Commit a54c20c

Browse files
committed
feat(engine): enhance error handling and hook context in action execution
- Add error information to inputs for fail and complete hooks to improve error tracking. - Include hook context details in action inputs to provide better context during plugin execution. - Update the handling of outputs and statuses in the engine to ensure comprehensive error reporting. Signed-off-by: zxypro1 <[email protected]>
1 parent f5fe096 commit a54c20c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

packages/engine/src/actions/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,20 @@ You can still use them now, but we suggest to modify them.`)
246246
const instance = await loadComponent(hook.value, { logger: this.logger });
247247
// Determine the inputs for the plugin based on the record's pluginOutput.
248248
const inputs = isEmpty(this.record.pluginOutput) ? this.inputs : this.record.pluginOutput;
249+
// 添加hook上下文信息
250+
const inputsWithHookContext = {
251+
...inputs,
252+
hookContext: {
253+
hookType: hook.hookType, // 例如: 'pre', 'fail', 'success', 'complete'
254+
hookName: `${hook.hookType}-${this.record.command}`, // 例如: 'fail-deploy', 'complete-deploy'
255+
command: this.record.command, // 例如: 'deploy', 'remove'
256+
actionType: hook.actionType, // 'plugin'
257+
level: hook.level, // 'project' 或 'global'
258+
projectName: this.option.projectName, // 项目名称
259+
},
260+
};
249261
// Execute the plugin with the determined inputs and provided arguments.
250-
this.record.pluginOutput = await instance(inputs, hook.args, this.logger);
262+
this.record.pluginOutput = await instance(inputsWithHookContext, hook.args, this.logger);
251263
// If prop 'replace_output' is true, replace the record's step output with the plugin output.
252264
if (hook.replace_output) {
253265
this.record.step.output = cloneDeep(this.record.pluginOutput);

packages/engine/src/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,12 @@ class Engine {
430430
}
431431
} catch (e) {
432432
this.logger.warn(get(e, 'data'));
433+
// 将error信息添加到inputs中传递给fail hook
434+
set(newInputs, 'error', e);
433435
res = get(await newActionInstance?.start(IHookType.FAIL, newInputs), 'step.output') || {};
434436
}
437+
// complete hook需要知道是否有error
438+
set(newInputs, 'output', res);
435439
const pluginCompleteResult = await newActionInstance?.start(IHookType.COMPLETE, newInputs);
436440
if (!isEmpty(pluginCompleteResult)) {
437441
res = get(pluginCompleteResult, 'step.output');
@@ -530,7 +534,12 @@ class Engine {
530534
} catch (error) {
531535
// On error, attempt to trigger the project's fail hook and update the recorded context.
532536
try {
533-
const res = await this.actionInstance?.start(IHookType.FAIL, this.record.componentProps);
537+
// 将error信息添加到inputs中传递给fail hook
538+
const failInputs = {
539+
...this.record.componentProps,
540+
error: error,
541+
};
542+
const res = await this.actionInstance?.start(IHookType.FAIL, failInputs);
534543
this.recordContext(item, get(res, 'pluginOutput', {}));
535544
} catch (error) {
536545
this.record.status = STEP_STATUS.FAILURE;
@@ -558,10 +567,14 @@ class Engine {
558567

559568
// Attempt to trigger the project's complete hook regardless of status.
560569
try {
561-
const res = await this.actionInstance?.start(IHookType.COMPLETE, {
570+
// complete hook需要包含error信息(如果有的话)
571+
const completeInputs = {
562572
...this.record.componentProps,
563573
output: get(item, 'output', {}),
564-
});
574+
error: get(item, 'error'),
575+
status: this.record.status,
576+
};
577+
const res = await this.actionInstance?.start(IHookType.COMPLETE, completeInputs);
565578
this.recordContext(item, get(res, 'pluginOutput', {}));
566579
} catch (error) {
567580
this.record.status = STEP_STATUS.FAILURE;

0 commit comments

Comments
 (0)