TP5 interface development exception handling takeover

Keywords: PHP JSON

A few days ago, a third-party extension package was used in development. During the use, the third-party extension package threw an exception.

Because this is the interface development, it needs to return error code and prompt information, so it needs to take over exception handling.

This article only takes notes. If you have any wrong or not detailed information, please leave a message.

First, create the directory exception under application.

Then create the BaseException class, which is the base class of custom exception class. You can inherit the framework exception class or php exception class.

 

<?php
/**
 * BaseException.php
 * File Description: custom exception class base class, inheritance framework exception class
 */

namespace app\common\exception;


use think\Exception;
use Throwable;

/**
 * Class BaseException
 * Base class of custom exception class
 * @package app\common\exception
 */
class BaseException extends Exception
{
   public function __construct($message = "", $code = 0, Throwable $previous = null)
   {
       parent::__construct($message, $code, $previous);
   }
}

 

 

This overrides the construction method of the parent class, and then calls the construction method of the parent class. This is a bit of a mystery to me. Welcome to share the message.

It is written in the manual that the exception page supported by the framework is handled by a user-defined class of the developer, and the exception menu handle parameter needs to be configured.

// exception handling handle Class leave blank \think\exception\Handle
'exception_handle'       => '\\app\\common\\exception\\Http',

The user-defined class needs to inherit Handle and implement render method. Refer to the manual: https://www.kancloud.cn/manual/thinkphp5/126075

<?php
/**
 * ExceptionHandler.php
 * File Description: overridden exception handling class
 */

namespace app\common\exception;

use DawnApi\exception\UnauthorizedException;
use Exception;
use think\exception\Handle;
use think\Log;

class ExceptionHandler extends Handle
{
    private $code;//http Status code
    private $msg;//Exception message
    private $errorCode;//Abnormal status code

    public function render(Exception $e)
    {
        if ($e instanceof BaseException)
        {
            //Use instanceof Determine whether the exception is a custom exception
           $this->code = 200;
           $this->msg = $e->getMessage();
           $this->errorCode = $e->getCode();
        } elseif ($e instanceof UnauthorizedException) {
            //Use instanceof Determine whether the exception is a third-party extension package exception
            $this->code =200;
            $this->errorCode = 401;
            $this->msg = 'privilege grant failed';
        } else {
            //Other anomalies
            $this->errorCode = 10003;
            $this->msg = 'operation failed';
        }
        $this->recordErrorLog($e);//Write exceptions to the log for easy viewing
        return json(['code'=>$this->errorCode,'msg'=>$this->msg]);
    }

    /**
     * Notes:Write exception to log
     * @param Exception $e
     */
    private function recordErrorLog(Exception $e) {
        Log::init([
            'type' => 'File',
            'path' => LOG_PATH,
            'level' => ['error'],
        ]);
        Log::record([
            'wrong file' => $e->getFile(),
            'Wrong number of rows' => $e->getLine(),
            'error code' => empty($this->code) ? $e->getCode() : $this->code,
            'Error message' => empty($this->message) ? $e->getMessage() : $this->message,
        ], 'error');
    }
}

Posted by marseille on Wed, 30 Oct 2019 08:55:14 -0700