You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Pipeline异常捕获与处理方法咨询

Laravel Pipeline 异常捕获处理方案

首先明确:Laravel 官方的 Pipeline 类没有内置handleException方法,你提供的示例写法无法生效。

以下是几种可行的异常捕获处理方案:

方案1:直接用 try/catch 包裹流水线执行

这是最直观的方式,流水线执行时抛出的异常会向外传递,直接在外层捕获即可:

public function handle(): void
{
    try {
        $filledContainer = app(Pipeline::class)
            ->send($this->container)
            ->through([
                CreateInboundPlanPipeline::class,
            ])
            ->then(function(){
                // 流水线执行完成后的操作
                // 例如填充模型等逻辑
            });
    } catch (\Exception $e) {
        // 在这里处理捕获到的异常
        // 比如记录错误日志、返回错误响应
        logger()->error('流水线执行失败:'.$e->getMessage());
    }
}

方案2:在流水线步骤内部处理异常

如果需要在特定步骤内先做局部处理(比如日志记录),再抛出异常终止流程,可以在步骤的__invoke方法中添加try/catch:

final readonly class CreateInboundPlanPipeline
{
    public function __invoke(DataContainer $container, Closure $next): DataContainer
    {
        try {
            $normalizer = new FBAInboundNormalizer();
            $items = json_decode($container->__get('initialrequest')->input('items'));
            $response = $normalizer->createInboundPlan($items, $container->__get('initialrequest'));

            if(!$response['success']) {
                throw new \Exception($response["message"]);
            }

            $extendContainer = $container;
            $extendContainer->inboundplanid = $response["inboundPlanId"];
            $extendContainer->currentOperationId = $response["operationId"];

            return $next($extendContainer);
        } catch (\Exception $e) {
            // 局部记录异常信息
            logger()->error('创建入库计划失败:'.$e->getMessage());
            // 重新抛出异常,让流水线终止
            throw $e;
        }
    }   
}

方案3:自定义Pipeline扩展异常处理(进阶)

如果需要在多个流水线中复用异常处理逻辑,可以自定义Pipeline类,添加自己的handleException方法:

自定义Pipeline类

use Illuminate\Pipeline\Pipeline;

class CustomPipeline extends Pipeline
{
    protected ?callable $exceptionHandler = null;

    public function handleException(callable $handler): self
    {
        $this->exceptionHandler = $handler;
        return $this;
    }

    public function then(callable $destination)
    {
        try {
            return parent::then($destination);
        } catch (\Exception $e) {
            if ($this->exceptionHandler) {
                return call_user_func($this->exceptionHandler, $e);
            }
            // 没有设置处理器时,重新抛出异常
            throw $e;
        }
    }
}

使用自定义Pipeline

public function handle(): void
{
    $filledContainer = app(CustomPipeline::class)
        ->send($this->container)
        ->through([
            CreateInboundPlanPipeline::class,
        ])
        ->then(function(){
            // 流水线完成后的操作
        })
        ->handleException(function(\Exception $e){
            // 统一处理异常
            logger()->error('流水线执行异常:'.$e->getMessage());
        });
}

内容的提问来源于stack exchange,提问作者MaxWeb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 23:31:02