【laravel5.6】 laravel 接口 接管 自定义异常类

本文详细介绍了在Laravel框架中如何自定义API异常处理流程,包括创建自定义异常类、修改异常处理器以及如何在控制器中使用自定义异常。通过实例演示了如何设置错误消息和错误代码,以及如何在API请求中返回JSON格式的异常信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1  app\exceptions 目录下 新建 Apiexception.php

 

<?php 
namespace App\Exceptions; 
 
/***
 * API 自定义异常类
 */
use Exception;

class ApiException extends Exception 
{ 
  
    //自定义异常处理
    public function SetErrorMessage($errorMsg='', $errorCode = '500'){
        $this->errorMsg = $errorMsg;
        $this->errorCode = $errorCode;
        return $this;  

    }

} 

 

2 修改  app\exceptions\handler.php 文件

/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception) 
    { 
        // 如果config配置debug为true ==>debug模式的话让laravel自行处理 
        $debug_status = config('app.debug'); // .env文件配置
        if($debug_status){ 
            return parent::render($request, $exception); 
        } 
        return $this->handle($request, $exception); 
    } 

     /**
      * 异常接管
      * 
      */
     public function handle($request, Exception $exception){ 
        //如果是接口请求,则抛出json
        if($request->is('api/*')) { 
             // 只处理自定义的APIException异常 
            if($exception  instanceof   \APP\Exceptions\ApiException ) {  //此处写ApiException文件所处路径
                $result = [ 
                    "status" => 2 , //操作状态: 1 成功 2 失败
                    "errorCode"=>$exception->errorCode,
                    "msg"    => $exception->errorMsg, 
                    "result"   => '', 
                ]; 
                return response()->json($result);
            } 

            //此处可以写多个自定义异常类
            if($exception  instanceof   \APP\Exceptions\otherException ) { 
                $result = [ 
                    "status" => 2 , //操作状态: 1 成功 2 失败
                    "errorCode"=>$exception->errorCode,
                    "msg"    => $exception->errorMsg, 
                    "result"   => '', 
                ]; 
                return response()->json($result);
            } 

            # 继续写其他自定义异常
            /***
             * code  
             * 
             * ***/
        } 
        return parent::render($request, $exception); 
    } 
    

 

3 使用

<?php

namespace App\Http\Controllers\Test;
use Illuminate\Routing\Controller;
use App\Exceptions\ApiException;

class IndexController extends Controller
{
    public function index(){
        throw (new ApiException)->SetErrorMessage("错了",'500');

    }

}

这就ok了

整体思路: 使用时候,先实例 自定义 异常。把错误信息传过去,  然后会回到 handler.php 里边显示

 

注意事项: 

     1 测试时候。要注册一个路由在访问

     2 注意开启debug 在.env 文件里边

 

转载于:https://www.cnblogs.com/richerdyoung/p/10044760.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值