码迷,mamicode.com
首页 > 其他好文 > 详细

Laravel自定义错误提示,自定义异常类提示

时间:2021-02-24 13:08:11      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:修改   url   message   throw   response   xtend   数组   class   wait   

方法一

新增CustomException.php文件

App\Exceptions\CustomException.php

<?php

namespace App\Exceptions;

use Exception;
/**
 * 王召波自定义异常基类
 * Class BaseException
 * @package App\Exceptions
 */
class CustomException extends Exception
{
    /**
     * 状态码
     * @var int|mixed
     */
    public $code = 200;

    /**
     * 错误具体信息
     * @var mixed|string
     */
    public $message = ‘json‘;

    /**
     * 构造函数,接收关联数组
     * BaseException constructor.
     * @param array $params
     */
    public function __construct($params = [])
    {
        parent::__construct();
        if (!is_array($params)) {
            return ;
        }
        if (array_key_exists(‘code‘, $params)) {
            $this->code = $params[‘code‘];
        }
        if (array_key_exists(‘msg‘, $params)) {
            $this->message = $params[‘msg‘];
        }
    }

    public function report()
    {
        //
    }
    public function render($request)
    {
        $result = [
            ‘code‘  => $this->code,
            ‘msg‘   => $this->message,
        ];
        //记录日志
//        Log::error($this->message);
        if($request->ajax()){
            return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }else{
            $params = [
                ‘msg‘  => $this->message,
                ‘wait‘ => 3,
                ‘url‘  => ‘javascript:history.back(-1);‘,
            ];
            return response()->view(‘common.error‘, $params, 500);
        }
    }
}

方法二

1.新增CustomException.php文件

App\Exceptions\CustomException.php

<?php

namespace App\Exceptions;

/**
 * 王召波自定义异常基类
 * Class BaseException
 * @package App\Exceptions\Custom
 */
class CustomException extends \Exception
{
    /**
     * 状态码
     * @var int|mixed
     */
    public $code = 200;

    /**
     * 错误具体信息
     * @var mixed|string
     */
    public $message = ‘json‘;

    /**
     * 构造函数,接收关联数组
     * BaseException constructor.
     * @param array $params
     */
    public function __construct($params = [])
    {
        parent::__construct();
        if (!is_array($params)) {
            return ;
        }
        if (array_key_exists(‘code‘, $params)) {
            $this->code = $params[‘code‘];
        }
        if (array_key_exists(‘msg‘, $params)) {
            $this->message = $params[‘msg‘];
        }
    }


}

2.修改render()方法

App\Exceptions\Handler.php

public function render($request, Exception $exception)
    {
        if ($exception instanceof CustomException) {
            // 如果是自定义的异常
            $this->code    = $exception->code;
            $this->message = $exception->message;
            $result = [
                ‘code‘  => $this->code,
                ‘msg‘   => $this->message,
            ];
            //记录日期
            Log::error($exception->message);
            if($request->ajax()){
                return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
            }else{
                $params = [
                    ‘msg‘  => $this->message,
                    ‘wait‘ => 3,
                    ‘url‘  => ‘javascript:history.back(-1);‘,
                ];
                return response()->view(‘common.error‘, $params, 500);
            }
        }
        
        return parent::render($request, $exception);
    }

测试

throw  new \App\Exceptions\CustomException([‘msg‘=>‘王召波自定义错误‘,‘code‘=>400]);

Laravel自定义错误提示,自定义异常类提示

标签:修改   url   message   throw   response   xtend   数组   class   wait   

原文地址:https://www.cnblogs.com/wangzhaobo/p/14437370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!