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);
}
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

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

 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);
        }
    }
3年前 评论
汪阿浠 (楼主) 3年前
Latent (作者) 3年前
汪阿浠 (楼主) 3年前
Latent (作者) 3年前
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);
        }
    }
3年前 评论
汪阿浠 (楼主) 3年前
Latent (作者) 3年前
汪阿浠 (楼主) 3年前
Latent (作者) 3年前
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());
}
3年前 评论
汪阿浠 (楼主) 3年前
Adachi (作者) 3年前
Latent 3年前
汪阿浠 (楼主) 3年前
Adachi (作者) 3年前
汪阿浠 (楼主) 3年前
汪阿浠 (楼主) 3年前
Latent 3年前
Adachi (作者) 3年前
汪阿浠 (楼主) 3年前

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

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

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

file

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

3年前 评论

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