Laravel自定義錯誤提示,自定義異常類提示,自定義錯誤返回信息,自定義錯誤頁面


方法一

新增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]);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM