zoukankan      html  css  js  c++  java
  • Laravel自定义错误提示,自定义异常类提示,自定义错误返回信息,自定义错误页面

    方法一

    新增CustomException.php文件

    AppExceptionsCustomException.php

    <?php
    
    namespace AppExceptions;
    
    use Exception;
    /**
     * 王召波自定义异常基类
     * Class BaseException
     * @package AppExceptions
     */
    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文件

    AppExceptionsCustomException.php

    <?php
    
    namespace AppExceptions;
    
    /**
     * 王召波自定义异常基类
     * Class BaseException
     * @package AppExceptionsCustom
     */
    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()方法

    AppExceptionsHandler.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 AppExceptionsCustomException(['msg'=>'王召波自定义错误','code'=>400]);
    
  • 相关阅读:
    Linux启动或禁止SSH用户及IP的登录,只允许密钥验证登录模式
    emacs 入门教程,菜单汉化,配置文件等杂乱文章
    bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集
    NOIP复习篇
    HiHocoder 1036 : Trie图 AC自动机
    (皇后移动类)八数码难题引发的搜索思考及总结
    POJ 水题(刷题)进阶
    [TJOI2010] 中位数
    小球和盒子的问题
    [洛谷P2785] 物理1(phsic1)-磁通量
  • 原文地址:https://www.cnblogs.com/wangzhaobo/p/14437370.html
Copyright © 2011-2022 走看看