Lumen 8.x 自定义异常请教!

自定义异常文件:App\Exceptions\HandleValidationException

我想拦截App\Exceptions\Handler里的验证异常,然后在HandleValidationException的validationError()处理,请教如何处理?

public function render($request, Throwable $exception)
{ 
    if ($exception instanceof ValidationException){
    //  这里面如何吧$exception参数放到validationError($exception)里面?
}
    return parent::render($request, $exception);
}
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

可以参考我这个 基本可以处理所有异常

 public function fail($message='error',$array=[],$code=500,$http=500)
    {
        return response()->json([
            'code'=>$code,
            'error'=>$message,
            'data'=>$array
        ],$http);
    }
 public function render($request, Throwable $exception)
    {
        if(Str::lower($request->segment(1)) === 'api'){
            if ($exception instanceof ValidationException) {
                return $this->fail($exception->validator->errors()->first(),$exception->errors(),10002,$exception->status);
            } elseif ($exception instanceof ModelNotFoundException) {
                return $this->fail("一不小心数据走丢了~~~",[],10003,500);
            } else if ($exception instanceof NotFoundHttpException) {
                return $this->fail('路由未找到',[],10004,$exception->getStatusCode());
            } else if ($exception instanceof MethodNotAllowedHttpException) {
                return $this->fail('请求方法不存在',[],10005,$exception->getStatusCode());
            } else if ($exception instanceof UnauthorizedHttpException) { //这个在jwt.auth 中间件中抛出
                return $this->fail('无效的访问令牌',null,10006,401);
            } elseif ($exception instanceof AuthenticationException) { //这个异常在 auth:api 中间件中抛出
                return $this->fail('无效的访问令牌',null,10006,401);
            } elseif ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException &&
                $exception->getStatusCode() == 403){
                return $this->fail('没有访问权限,请联系管理员',null,10007,$exception->getStatusCode());
            }
            return $this->fail($exception->getMessage().' '.$exception->getFile(). ' '.$exception->getLine(),null,10001);
        }else{
            return parent::render($request, $exception);
        }
    }
4年前 评论
汪阿浠 (楼主) 4年前
Latent (作者) 4年前
汪阿浠 (楼主) 4年前
Latent (作者) 4年前
wwx101123 3年前
讨论数量: 19

可以参考我这个 基本可以处理所有异常

 public function fail($message='error',$array=[],$code=500,$http=500)
    {
        return response()->json([
            'code'=>$code,
            'error'=>$message,
            'data'=>$array
        ],$http);
    }
 public function render($request, Throwable $exception)
    {
        if(Str::lower($request->segment(1)) === 'api'){
            if ($exception instanceof ValidationException) {
                return $this->fail($exception->validator->errors()->first(),$exception->errors(),10002,$exception->status);
            } elseif ($exception instanceof ModelNotFoundException) {
                return $this->fail("一不小心数据走丢了~~~",[],10003,500);
            } else if ($exception instanceof NotFoundHttpException) {
                return $this->fail('路由未找到',[],10004,$exception->getStatusCode());
            } else if ($exception instanceof MethodNotAllowedHttpException) {
                return $this->fail('请求方法不存在',[],10005,$exception->getStatusCode());
            } else if ($exception instanceof UnauthorizedHttpException) { //这个在jwt.auth 中间件中抛出
                return $this->fail('无效的访问令牌',null,10006,401);
            } elseif ($exception instanceof AuthenticationException) { //这个异常在 auth:api 中间件中抛出
                return $this->fail('无效的访问令牌',null,10006,401);
            } elseif ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException &&
                $exception->getStatusCode() == 403){
                return $this->fail('没有访问权限,请联系管理员',null,10007,$exception->getStatusCode());
            }
            return $this->fail($exception->getMessage().' '.$exception->getFile(). ' '.$exception->getLine(),null,10001);
        }else{
            return parent::render($request, $exception);
        }
    }
4年前 评论
汪阿浠 (楼主) 4年前
Latent (作者) 4年前
汪阿浠 (楼主) 4年前
Latent (作者) 4年前
wwx101123 3年前

lumen文档说看laravel的,你不就是这意思吗,文档应该写的听清楚的,report和render的参数直接拷贝默认的即可

public function render($request, Exception $exception)

file


分割线 抱歉我草率了,下面为重新回答。

自定义异常继承的是PHP默认的Exception,你想要的$exception,其实就是$this,下面的构造函数可写可不写。

public function __construct($message = "", $code = 0, Throwable $previous = null)
{
    parent::__construct($message, $code, $previous);
}


public function render($request)
{
    dd($this->getMessage());
}
4年前 评论
汪阿浠 (楼主) 4年前
Adachi (作者) 4年前
Latent 4年前
汪阿浠 (楼主) 4年前
Adachi (作者) 4年前
汪阿浠 (楼主) 4年前
汪阿浠 (楼主) 4年前
Latent 4年前
Adachi (作者) 4年前
汪阿浠 (楼主) 4年前

你不应该是抛出你的自定义异常然后处理吗?

   $validator = Validator:: make($request->all(), $rules, $messages);

  if ($validator->fails())
    throw new HandleValidationException( $request, $validator );
4年前 评论

file

感谢大家的帮助,最后还是 @AdamTyn 给了解决方案,完美拦截了验证异常。

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!