标签(空格分隔): php

自定义异常类

<?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(),
        ]);
    }
}

修改app/exceptions/Handler 类中的render方法

getenv() 获取env配置信息

Laravel异常处理

抛出异常

throw new ApiException(500);

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-05
  • 2021-11-13
  • 2021-06-12
  • 2021-12-12
猜你喜欢
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2020-12-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案