Laravel Exception Handling

Laravel Exception Handling

Tags (separated by spaces): php

Custom exception class

<?php
namespace App\Exceptions;

use Throwable;
use Exception;

class ApiException extends Exception
{

    public function __construct($code, \Throwable $previous = null)
    {
        parent::__construct(config('jsoncode.code')[(int) $code], $code, $previous);
    }

    /**
     * 报告异常
     *
     * @return void
     */
    public function report()
    {
        //
    }

    /**
     * 转换异常为 HTTP 响应
     *
     * @param  \Illuminate\Http\Request
     * @return \Illuminate\Http\Response
     */
    public function render($request)
    {
         return response()->json([
             'code' => $this->getCode(),
             'message' => $this->getMessage(),
        ]);
    }
}

Modifying the render method app / exceptions / Handler class

getenv() 获取env配置信息

Throw an exception

throw new ApiException(500);

Guess you like

Origin www.cnblogs.com/yanweifeng/p/10955490.html